Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numerical inconsistency in .NET

I’m building a CAD-like application in C#. I’m using SlimDX as the graphics engine, and for the number-crunching part, I build custom libraries which ultimately rely on the System.Math class, naturally.

Now, the thing is that the SlimDX libraries use structures composed of the float data type, whereas the Math class contains several methods that only accept and return double objects, for example: Math.Ceiling, and Math.Sin. So I find myself casting my data back and forth, from float to double, constantly.

This doesn’t seem right. I’m not that concerned with the impact on performance the casts might cause (maybe I should be?), but with the numerical instabilities that may arise because of them, which is far more frightening.

So I just wanted to know how you usually manage these kinds of situations, as I’m guessing this must not be an uncommon scenario.

like image 518
WHermann Avatar asked Nov 26 '25 05:11

WHermann


1 Answers

There shouldn't be any numerical inconsistencies.

Of the three floating-point datatypes in .NET (float, double and the F type not available to C# but used internally in many members) there are going to be three times they come up:

  1. The storage and definitions done by your application's mathematical engine.
  2. The calculation done by this engine.
  3. Rendering.

Number 1 would presumably be mostly defined in terms of double. Number 2 will also be defined largely in terms of double though it will make some use of F as well. Number 3 will be defined in terms of single for the reasons you give, but if the boundaries between the layers is well-defined this shouldn't have any impact on what is actually calculated.

like image 67
Jon Hanna Avatar answered Nov 28 '25 19:11

Jon Hanna



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!