3

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.

Disposer
  • 6,201
  • 4
  • 31
  • 38
WHermann
  • 159
  • 1
  • 8
  • 2
    Is there a question here? Seems like you are expressing a general concern, not asking a specific question, so this probably isn't the place. – Ant P Nov 27 '14 at 20:42
  • @AntP My question is "how do you usually manage these kinds of situations?" – WHermann Nov 27 '14 at 20:44
  • That's a bit vague, though, isn't it? It is a question that doesn't really have an answer, so it is probably better suited to a discussion board. – Ant P Nov 27 '14 at 20:45
  • double is more precise than float right? So this should not be a problem? – hatcyl Nov 27 '14 at 20:50
  • If you want useful answers you might be better served by actually identifying some repeatable "numerical instabilities," demonstrating them and asking for a solution - right now it still feels like a mix between a code review question, a specific coding issue and an open discussion. – Ant P Nov 27 '14 at 20:50
  • If single precision suffices then it's no problem doing some of the calcs to double precision. Personally though, I think I'd be looking for more precision on the engine side. For the graphics then single is universal. But for the numerics I'd usually expect to see double. Can't you find a library that offers double? Of course, you didn't really offer any details as to what sort of numerics you are doing. So it's kind of hard for anyone to offer solid advice. – David Heffernan Nov 27 '14 at 20:53

2 Answers2

5

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.

Jon Hanna
  • 110,372
  • 10
  • 146
  • 251
  • Yes indeed. It looks like I wasn't separating clearly enough these stages in my project and some code was not flowing in the right direction all the time. – WHermann Nov 29 '14 at 02:55
-2

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

.Net is a general platform that attempts to solve a majority of problems, but it can't solve them all. Although it may be extra work in the short term, if the precision is truly a concern, the time invested will be well worthwhile.

competent_tech
  • 44,465
  • 11
  • 90
  • 113
  • You mean like implementing my own System.Math class? I tought nothing could beat library classes performance-wise. What would you say is the best way to do it? – WHermann Nov 27 '14 at 20:53
  • Yes, implementing your own class is what I am suggesting. Although performance is key, it sounds from your post that integrity is more important. To re-implement these items, I would look at the source for the system.math class, which is available from Microsoft or mono. – competent_tech Nov 27 '14 at 20:55
  • Another approach is to search for a high-quality, high-performance third party component (DLL or source). For example, we use CenterSpace's NMath for statistical methods. – competent_tech Nov 27 '14 at 20:57
  • 1
    Hmm, that's not the way I would go, choosing to use single rather than double. Are you sure that's wise? Or even necessary. You realise that every single value is exactly representable as a double. – David Heffernan Nov 27 '14 at 21:01
  • My only other option is to find a graphics engine that uses doubles, and just like @DavidHeffernan said in a comment, float is universal when doing graphics. Do you happen to know a high performance managed DirectX or OpenGL wrapper for .NET that uses doubles? – WHermann Nov 27 '14 at 21:06
  • 2
    You don't need that at all. Single precision engine is fine for graphics. Personally I rather suspect that you are worrying about something that is not a problem. I cannot see anything that you said that is troublesome. If single precision suffices, then it doesn't matter if you have some calcs that pass via double and back. No problem there, – David Heffernan Nov 27 '14 at 21:09