nathanpjones / DecimalMath

The math support for Decimal that Microsoft forgot and more.
MIT License
47 stars 13 forks source link

Decimal Math

Portable math support for Decimal that Microsoft forgot and more.

The .NET Decimal data type is included in .NET, but is often overlooked for scientific calculations. It's high precision and exact up to 28 decimal places and it's available in any .NET environment.

You might be in a situation where you just need a lot more precision than Double can provide. I used Decimal for calculating locations in space for CNC manufacturing and pick and place control. I've found the increased precision of Decimal reduces overall errors throughout the set of calculations and it also improves the odds of reversing the calculations if necessary for debugging. In that case it ends up being a kind of oversampling.

Unfortunately, a lot of the usual number functionality is not provided for .NET. For example, you can't calculate a square root or even perform exponentiation. You can cast to Double for these operations, but you can end up with a significant loss of precision.

Note All of this I've used in the "real world", but this is also a hobby. Although the library is performant, I've perferred accuracy and readability to raw performance.

Install

Install via NuGet package manager console:

PM> Install-Package DecimalMath.DecimalEx

Or of course you can clone the repository and pull in the projects directly.

Libraries

This project contains two portable libraries.

DecimalEx

Bridges the gap in .NET support.

Decimal2D

Provides support for high-accuracy geometric calculations. Note: This is still a work in progress. It's been used in production, but it still needs some more grooming (was originally VB code) and unit tests.

License

This project uses the MIT License. See the license file in the same folder as this readme.