double
in C# don't hold enough precision for my needs. I am writing a fractal program, and after zooming in a few times I run out of precision.
I there a data type that can hold more precise floating-point information (i.e more decimal places) than a double?
The .NET Framework 4 introduces the System.Numerics.BigInteger struct that can hold numbers with an arbitrary large precision.
Yes,
decimal
is designed for just that.However, do be aware that the range of the decimal type is smaller than a double. That is double can hold a larger value, but it does so by losing precision. Or, as stated on MSDN:
The primary difference between
decimal
anddouble
is thatdecimal
is fixed-point anddouble
is floating point. That means that decimal stores an exact value, whiledouble
represents a value represented by a fraction, and is less precise. Adecimal
is 128 bits, so it takes the double space to store. Calculations ondecimal
is also slower (measure !).If you need even larger precision, then
BigInteger
can be used from .NET 4. (You will need to handle decimal points yourself). Here you should be aware, that BigInteger is immutable, so any arithmetic operation on it will create a new instance - if numbers are large, this might be crippling for performance.I suggest you look into exactly how precise you need to be. Perhaps your algorithm can work with normalized values, that can be smaller ? If performance is an issue, one of the built in floating point types are likely to be faster.
Check out BigInteger (.NET 4) if you need even more precision than
Decimal
gives you.