7 ms·
Unity vs. Floating Point
- koolala 4mo agoI wish JavaScript or any open-source Array Language could do Single-precision Floats. It's awesome they were able to show the assembly breakdown.
- dapperdrake 4mo agoFloat32Array Math.fround()
- koolala 4mo agoThose convert, the only way to use f32 in JS is wasm
- dapperdrake 4mo agoDo intermediate values always turn into double floats?
- taeric 4mo agoI wish there was more explicit support for fixed point decimal out there.
- koolala 4mo agoSpeed wise no hardware can do it though.
- taeric 4mo agoThis depends entirely on the sizes needed. For a surprising number of things people would use fixed point for, I would be surprised if you couldn't get good speed with surprisingly little effort.
- vardump 4mo agoI would be surprised if you could get even one third of performance of floats. So why bother?
- taeric 4mo agoIf you don't have to do trig, I'd be surprised if you aren't faster by default, oddly. Indeed, if you are just adding and subtracting, it is just a number. If you are doing multiplication, it is a multiply and shift. So long as you don't try and support massive numbers of different fixed sizes, that shift is almost certainly still cheaper than float hardware. (Indeed, a lot of multiplications wouldn't even need the shift...) Again, I do not mean this as a criticism of floats. For simulations and for numbers where you do have to support completely arbitrary values, there is a reason floats are a thing.
- jcranmer 4mo agoAn integer add, sub, or shift is 1 cycle of latency; integer mul is generally 3 cycles; integer div is lol-that-is-slow. Floating-point adds, subs, muls, and fmas are generally 4 cycles, with div being lol-that-is-slow (but generally faster than integer division because your divisor and dividend have fewer bits). So fixed-point addition and subtraction are definitely faster, multiplication is a wash if you're doing binary-based fixed point (but slower if you're doing decimal-based fixed point), and fixed-point division is definitely slower than floating-point division.
- taeric 4mo agoKudos on providing the numbers. I wasn't confident in the numbers that I remembered and with how pipelined everything is, I didn't know how much to lean on them. Not exactly my standard workflow to care about this level of speed. My gut would still be that it is typically a wash for most everyone as far as speeds go? If default libraries supported it more directly, I would think it would largely be a win for a lot of reasoning. In particular, silly stuff like 1e32 + 1e1 would not be nearly as surprising to most people. And the entire class of bugs around stuff like doing something until it reaches 0.9 would almost certainly go away if we guaranteed precision to a set number decimal places. Alas, default libraries do not support this, though. So the above is admittedly wishful thinking on my end. And I could as easily describe a world where people insist on arbitrary numbers of fixed point values and how that would be its own set of landmines.
- bee_rider 4mo agoDo you mean fixed point using integers, or actually decimal (base-10)? My gut would be to use fixed-point integers with a power of two in the denominator. On the other hand, I’m not sure if modern compilers even bother converting division/multiplications by powers of two to shifts these days, so maybe it isn’t worth it…
- taeric 4mo agoFair that relying on decimal is an interesting choice. My gut is that it would help with a lot of reasoning for folks. This comes up all of the time with stuff like lat/lng values. People are convinced you have to use doubles because of the inaccuracy of floats. Completely skipping over the fact that you could have fixed point accuracy to 6 decimal digits with the same number of bits as a float. You just reduce your max/min value that you can represent. Which, for lat/lng, you are already heavily bounded. I think it is fair to argue that basic libraries don't support trig on common fixed point sizes. But that is ultimately my lament. Floats are amazing for what they need to do. For what many people need, though, it feels overkill because it is overkill.
- addaon 4mo ago> This comes up all of the time with stuff like lat/lng values. People are convinced you have to use doubles because of the inaccuracy of floats. Using any floating point representation for lat/long makes no sense at all to me. Floating point is designed for representing values where expected error scales with magnitude, not for values where error is constant with magnitude; the latter requires fixed point. I truly don't understand why someone would optimize their position representation for maximum accuracy off the coast of Africa, instead of having it uniform across the world.
- taeric 4mo agoAgreed. But I'm assuming we are in the minority on this one? Bad assumption?
- 4mo ago
- adgjlsfhk1 4mo agoWhat do you mean by Array Language? Would Julia qualify here?
- koolala 4mo agoI was thinking like APL, all the open ones are float64 based.
- Zarathruster 4mo agoInteresting. When I first got started with Unity I found really buggy behavior if you do any kind of physics calculations near world origin (0, 0, 0). It has a weird gravity well effect if you get too near it. I never found a satisfactory explanation for it but maybe this is it.
- aras_p 4mo agoIf you mean built-in Unity physics, then unlikely. All built-in physics stuff (either 3D physics which is PhysX, or 2D physics which is Box2D) are done entirely in C++ code and are unaffected by Mono float<->double shenanigans.
- Zarathruster 4mo agoIt was a prototype for a VR game in which you toss objects and I never drilled down far enough to see the implementation, since I just used the sample prefabs that come with the XR Interaction Toolkit. If you ever tossed anything in the vicinity of the origin, the object's trajectory would swerve towards it. Weirdly I couldn't reproduce the behavior when running the scene with a first-person controller in non-VR, so maybe a bug within that toolkit or something.
- gcr 4mo agoHow close to 0,0,0 are you talking? Subnormal floating point shenanigans maybe?
- Zarathruster 4mo agoWithin a meter or so, maybe a little more at times? But yeah I had an intuition that it was subnormal shenanigans, but didn't spend too long trying to diagnose, since it was easy enough to move everything a few meters offset.
- corysama 4mo agoOnly tangentially related. But, I still have not forgiven Unity for the floating point footgun in https://docs.unity3d.com/6000.4/Documentation/ScriptReference/AsyncOperation-progress.html https://docs.unity3d.com/6000.4/Documentation/ScriptReferenc... > The operation is finished when the progress float reaches 1.0 and isDone is called. If you set allowSceneActivation to false, progress is halted at 0.9 until it is set to true. So, I needed to have my UI do other stuff until both this and something else was ready. Obvious thing to do would be to check this for "AsyncOperation.progress >= 0.9" and also check the other thing. Right? Except AsyncOperation.progress is a float, 0.9 is a double and 0.9f < 0.9. Progress is not halted at 0.9. It never reaches 0.9. It's halted at 0.9f! Just a few million ulps short!
- truemotive 4mo agotrigger warning, lol
- addaon 4mo agoThis is insane in so many different ways. 1) Why would you use floating point instead of fixed point or other fractional representation (uh, percent?) for this sort of thing? 2) Why would you design an interface that's specifically set up to make progress bars stall at 90%? This is insanely user hostile -- there are great UI patterns for expressing indeterminate progress when your progress is indeterminate! 3) Why in the example would you prompt user interaction at 90%? (I'll give them credit for at least comparing to 0.9f instead of 0.9 here.) What user expects a prompt to appear before progress is complete?
- garaetjjte 4mo ago>Official Mono releases have switched to do that since then, but (I think) for backwards compatibility reasons Unity never enabled that functionality and kept everything at double precision so far. Probably because Unity uses ancient Mono fork. And that apparently started because they didn't renew their non-copyleft license with Xamarin.
- aras_p 4mo agoThat is not actually true. Unity used to be on ancient version of Mono before year 2017. Then until 2019 the option to use “up to date” version was optional. Since 2019 it has been on more or less current Mono version (Mono itself stopped getting any serious updates around 2020).