I need to really precisely calculate the square root of a double. And mathf.sqrt just isnt precise enaugh (It also returns a float…)
If anyone knows how to, I would really appreciate it.
Why? Just tell me one case, where you need such precisity, that a float just wont cut it. I mean, maybe if you have a number so big, that it almost caps out the float. The unity mathf class is very great, you probably wont find a better way.
I’m calculating orbits, and as soon as the orbit pases 10km in diameter the float precision just wont do it.
Are you storing them in meters or something? That’s just too little of a diameter. Float precision can vary from number to number based on how much bytes it actually uses to store the whole part of that number. But if you really need to have an ultra-meg-fantastic calculation, you could look into storing numbers in fractions, that should be more precise
Well, at the end I found a way to do it by simply doing this:
using System;
Math.Sqrt
Apparently all Math functions are done in doubles… what?
In .NET, yes, a lot of the functions use doubles for precision, but Unity’s Mathf uses singles for all of them because of the increased performance gains. Glad you found it!
Have you ever inspected Mathf? Most of it is just a wrapper for System.Math. You’ll find in a lot of cases Mathf simply casts to a double, calls Math, then casts the result back to a float.
The isssue is one of convinience rather then performance.
Well, that’s rather sad. Call me disillusioned now…
Don’t feel too bad. Under the hood, Math (which works with doubles) just calls through to the C library (which also works with doubles), which compiles down to floating-point processor machine instructions, which, on pretty much any modern processor… are optimized for doubles.
Well I think the use of floats everywhere instead of doubles has to do with shaders and low level directX or OpenGL. You need things aligned on 16 byte boundaries. This combined with the fact that you use 4x4 matrices for so much stuff makes float4 (a 16 byte value) a really nice and natural way to manipulate data, and outside of realistic space based maths, I suspect floats are all the precision anyone needs. I think NASA uses PI to 15 digits of precision (doubles basically) for most of its work.