14

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?

jgauffin
  • 99,844
  • 45
  • 235
  • 372
simonalexander2005
  • 4,338
  • 4
  • 48
  • 92
  • 10
    If you want to display fractals, zooming should be possible infinitely. You might want to search for an algorithm that returns your values depending on zoom level. – Martin Hennings Mar 29 '11 at 13:21

3 Answers3

22

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 decimal keyword denotes a 128-bit data type. Compared to floating-point types, the decimal type has a greater precision and a smaller range, which makes it suitable for financial and monetary calculations. The approximate range and precision for the decimal type are shown in the following table.

The primary difference between decimal and double is that decimal is fixed-point and double is floating point. That means that decimal stores an exact value, while double represents a value represented by a fraction, and is less precise. A decimalis 128 bits, so it takes the double space to store. Calculations on decimal 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.

Callum Linington
  • 14,213
  • 12
  • 75
  • 154
driis
  • 161,458
  • 45
  • 265
  • 341
  • 1
    Integer, double, float, decimal are all immutable too and arithmetic operations create new instances...so that's a red herring. Decimal and BigInteger are slow because they aren't just simple hardware primitives. – Mark Sowul Apr 08 '11 at 13:27
  • 1
    Well, all right, it's not totally a red herring because they can grow arbitrarily large in contrast to the other fixed-size numeric types, but if that's the case the value wouldn't fit in other types anyway. – Mark Sowul Apr 08 '11 at 13:33
  • `Decimal` is a floating-point type, even though its dynamic range is less than that of `float`. I find the design curious, given that the type takes 16 bytes, and a type with a 68-bit whole-number part and 60-bit fraction could have done almost everything `Decimal` does more efficiently. Not quite as much range on the high end nor precision on the low end, but capable of guaranteeing that addition and subtraction will either be performed with perfect precision or else fail entirely--a guarantee not provided by `double`. – supercat Sep 17 '13 at 19:55
5

The .NET Framework 4 introduces the System.Numerics.BigInteger struct that can hold numbers with an arbitrary large precision.

Dave Van den Eynde
  • 17,020
  • 7
  • 59
  • 90
0

Check out BigInteger (.NET 4) if you need even more precision than Decimal gives you.

Jonas Elfström
  • 30,834
  • 6
  • 70
  • 106