Numerical inconsistency in .NET.

I am creating a CAD application in C #. I use it SlimDX

as a graphics engine, and for the crunching part I create custom libraries that ultimately rely on the System.Math class, naturally.

Now the point is that libraries SlimDX

use structures consisting of the float data type, while the Math class contains several methods that only accept and return double objects, for example: Math.Ceiling and Math.Sin. So I find myself doing my data back and forth, from float to double, all the time.

It doesn't seem right. I don't think it will affect the performance that casts can affect (maybe I should be?), But with the numerical instabilities that can arise from them, which is much scarier.

So, I just wanted to find out how you usually handle situations like this, as I'm guessing this shouldn't be an unusual scenario.

+3


source to share


2 answers


If this was my application and I was worried about a possible loss of integrity, I would recreate the appropriate methods using the required data types.



.Net is a common platform that tries to solve most problems but cannot solve them all. While this may be additional work in the short term, if accuracy is truly a concern, the time spent on the investment will be helpful.

-2


source


There should be no numerical inconsistencies.

Of the three types of data with .NET floating point ( float

, double

and F

the type of reach for C #, but used internally in many Member States), they will be three times higher:



  • The storage and definitions executed by your application math engine.
  • The calculation performed by this engine.
  • Rendering.

Number 1 will presumably be mostly defined in terms double

. Number 2 will also be defined mostly in terms double

, although it will also use F

. The number 3 will be defined in terms single

for the reasons you give, but if the boundaries between the layers are well defined, it shouldn't affect what is actually calculated.

+5


source







All Articles