Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can C# store more precise data than doubles?

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?

like image 630
simonalexander2005 Avatar asked Mar 29 '11 13:03

simonalexander2005


People also ask

Is Can-C good for your eyes?

Users and scientists have reported that Can-C is also great for: Dry eye syndrome, Corneal disorders, Computer vision syndrome, Eye strain, Ocular inflammation, Blurred vision and other systemic diseases, also a benefit for those who wear contact lenses.

Is Can-C good for dogs?

SAFE FOR HUMANS AND DOGS - Can-C is the first and only patented NAC eye drop that uses the exact formula proven effective in both animal and human trials, offering a non-invasive alternative to cataract surgery.

What is the best eye drops for cataracts?

1 Lanosterol eye drops could potentially be a safe, non-invasive, and less costly alternative to cataract surgery for patients who have moderate forms of cataracts.

Do Can-C drops work?

The Can C definitely works. Vision in my left eye is still not up to what it was before I noticed the problem with blurriness. But it's definitely better than when I wasn't using Can C. I was curious about using Can-C eye drops to treat my dogs cataracts after reading numerous reviews.


2 Answers

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.

like image 161
driis Avatar answered Oct 12 '22 11:10

driis


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

like image 29
Dave Van den Eynde Avatar answered Oct 12 '22 12:10

Dave Van den Eynde