A simple and efficient library for calculating the area of squares with validation and error handling.
$ dotnet add package SquareAreaCalculatorA simple, efficient, and well-tested .NET library for calculating geometric properties of squares.
Install the package via NuGet Package Manager:
dotnet add package SquareAreaCalculator
Or via Package Manager Console in Visual Studio:
Install-Package SquareAreaCalculator
using SquareAreaCalculator;
// Calculate area
double area = SquareCalculator.CalculateArea(5.0);
Console.WriteLine($"Area: {area}"); // Output: Area: 25
// Calculate area with decimal precision
decimal preciseArea = SquareCalculator.CalculateAreaPrecise(5.5m);
Console.WriteLine($"Precise Area: {preciseArea}"); // Output: Precise Area: 30.25
// Calculate perimeter
double perimeter = SquareCalculator.CalculatePerimeter(4.0);
Console.WriteLine($"Perimeter: {perimeter}"); // Output: Perimeter: 16
// Calculate diagonal
double diagonal = SquareCalculator.CalculateDiagonal(5.0);
Console.WriteLine($"Diagonal: {diagonal:F2}"); // Output: Diagonal: 7.07
Calculates the area of a square using double precision.
Parameters:
sideLength (double): The length of the square's side. Must be greater than zero.Returns:
double: The area of the square.Exceptions:
ArgumentException: Thrown when the side length is less than or equal to zero, or is not a valid number (NaN, Infinity).Calculates the area of a square using decimal precision for high-precision calculations.
Parameters:
sideLength (decimal): The length of the square's side. Must be greater than zero.Returns:
decimal: The area of the square with decimal precision.Exceptions:
ArgumentException: Thrown when the side length is less than or equal to zero.Calculates the perimeter of a square.
Parameters:
sideLength (double): The length of the square's side. Must be greater than zero.Returns:
double: The perimeter of the square.Exceptions:
ArgumentException: Thrown when the side length is less than or equal to zero, or is not a valid number (NaN, Infinity).Calculates the diagonal length of a square.
Parameters:
sideLength (double): The length of the square's side. Must be greater than zero.Returns:
double: The diagonal length of the square.Exceptions:
ArgumentException: Thrown when the side length is less than or equal to zero, or is not a valid number (NaN, Infinity).using SquareAreaCalculator;
class Program
{
static void Main()
{
try
{
double sideLength = 6.0;
var area = SquareCalculator.CalculateArea(sideLength);
var perimeter = SquareCalculator.CalculatePerimeter(sideLength);
var diagonal = SquareCalculator.CalculateDiagonal(sideLength);
Console.WriteLine($"Square with side length {sideLength}:");
Console.WriteLine($" Area: {area}");
Console.WriteLine($" Perimeter: {perimeter}");
Console.WriteLine($" Diagonal: {diagonal:F2}");
}
catch (ArgumentException ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
}
}
using SquareAreaCalculator;
// For financial calculations where precision matters
decimal plotSize = 15.75m;
decimal area = SquareCalculator.CalculateAreaPrecise(plotSize);
Console.WriteLine($"Plot area: {area} square meters");
using SquareAreaCalculator;
try
{
// This will throw an ArgumentException
double invalidArea = SquareCalculator.CalculateArea(-5.0);
}
catch (ArgumentException ex)
{
Console.WriteLine($"Invalid input: {ex.Message}");
// Output: Invalid input: Side length must be greater than zero.
}
git clone https://github.com/yourusername/square-area-calculator.git
cd square-area-calculator
dotnet restore
dotnet build
dotnet test
dotnet pack src/SquareAreaCalculator/SquareAreaCalculator.csproj --configuration Release
This project uses GitHub Actions for continuous integration and deployment:
git checkout -b feature/amazing-feature)git commit -m 'Add some amazing feature')git push origin feature/amazing-feature)This project is licensed under the MIT License - see the LICENSE file for details.
If you encounter any issues or have questions, please open an issue on GitHub.