Math with Decimals

mskeel

Senior Contributor
Joined
Oct 30, 2003
Messages
913
I've reached the point in a program where I've decided that I need the extra precision that Decimals give me over doubles. The problem is that Math.PI, Math.Sqrt, Math.Cos, etc.. all take doubles as arguments and return doubles. Is there a way to get around this or am I stuck with doubles? Does anyone know of an open source .Net Math library that might do the trick if The Framework can't handle it?
 
As far as trigonometric functions, in the .Net framework, you are stuck with double precision. I couldn't find any Decimal mathematics libraries on the net. Sorry, I wish I could tell what you could use instead of what you can't.
 
Depending on what you are doing, I think doubles are the way to go, all computational geometry is performed with doubles, all be it in C or C++. But that does not matter.
I guess you are hitting floating point arithmetic errors, I spend a lot of time dealing with this issue! It’s something that has to be dealt with I’m afraid.
 
I think we are going have to stick with doubles becuase of these trig functions. It's just that a few of our more lengthy calcuations are very slightly off and I know the formulas are correct. I'm doing coordinate transformations, converting between LLA (Latitude Longitude Altitude), ECEF (Earth Centered Earth Fixed), and RBE (Range Bearing Elevation).

If anyone has any other advice, I'd be very interested to hear it. Does anyone know if the double types are calculated the same way in .Net and Win32 as far as binary representation and accuracy are conerned?
 
With lengthy equations, the round-off errors are bound to build up and multiply, which is understandably a big issue with very precise math.

I do know that doubles have increased in precision (either in storage, computation, or both, not sure) between .Net 1.x and 2.0. I'm not sure if the .Net and Win32 implementation of doubles are the same, but on the off chance that Win32 doubles are more precise, even if you interop with Double data types, somewhere in the marshalling you are sure to lose the extra precision. You might want to try searching for a 128-bit or arbitrary precision math library, but the big issue is, again, marshalling (unless, perhaps, that library has built in support for parsing values and converting them into strings).
 
In case anyone was curious, it looks like there is an implementation of Java's BigDecimal/BigInt in J#. The drawback is that you must create a dependency with the J# runtime library, which isn't a big deal if you are already working in J# but could be cumbersome for everyone else. I mean, geez, who actually uses J#? ;)
 
Not to bump again, but another interesting tidbit in this vien I have discovered is that NUnit takes into account rounding errors with doubles by giving you a way to compensate when unit testing in one of the overloads for AreEqual:
Code:
Assert.AreEqual(double expectedValue, double actualValue, double delta);
Where delta is the maximum acceptable difference between the expected and actual values. I'm not surprised to find that NUnit does this (as it's awesome) but it's sometimes easy to miss little things like that with the lack of complete documentaion.
 
Back
Top