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?
Yes,
decimalis 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
decimalanddoubleis thatdecimalis fixed-point anddoubleis floating point. That means that decimal stores an exact value, whiledoublerepresents a value represented by a fraction, and is less precise. Adecimalis 128 bits, so it takes the double space to store. Calculations ondecimalis also slower (measure !).If you need even larger precision, then
BigIntegercan 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.