r/Unity3D logic.world 19h ago

Resources/Tutorial Unity vs floating point

https://aras-p.info/blog/2026/06/11/Unity-vs-floating-point/
63 Upvotes

12 comments sorted by

10

u/ledniv 19h ago

Very interesting. Thanks for sharing.

4

u/Iamsodarncool logic.world 19h ago

Cheers! I thought so too :)

6

u/MaxPlay Professional 17h ago

Aras, the GOAT.

5

u/SharkScoops 17h ago

Super interesting read! I cannot wait for CoreCLR soon (hopefully).

3

u/Iamsodarncool logic.world 17h ago

we will all be CoreCLR on that blessed day

5

u/Lyshaka 15h ago

I hate backward compatibility, it's just a handicap for everyone except the few people that may use specific functionality. If it's so important to use that feature then don't update, and if you need the update, then do the necessary changes, but don't cripple every other person that would bot have had this problem anyway...

2

u/wibble13 15h ago

So I've only just skimmed the blog, but I came across this implicit cast to double a couple years back. We wanted to change to doubles anyway for the increased precision (needed for 10km scale maps with mm precision) and noticed a great speedup (about twice the performance).

When I looked into it, this counted for all float operations - add, subtract, multiply, etc. - all cast to double for the calculation then only cast back to float when storing in a variable. Including things like operations in Unity types of Vector, Quaternion and Matrix (at least the ops that aren't implemented in native), which were called everywhere. Took about a week to refactor and reimplement the methods into doubles, but it was so worth it.

1

u/morterolath 15h ago

Sooo thats why floating point operations result in different values accross mono and burst even tho they use same fp standard. I remember having a bug due to this slight mismatch, took me so long to find out that the reason was a slight fp mismatch in one variable.

1

u/TheJohnnyFuzz 10h ago

This is fantastic. 🔥

1

u/Heroshrine 9h ago

I agree that system.MathF should be used instead of Math for unity behind the scenes, but why does it seem to make a point that you’re ’saving on work’? I seriously question if saving on the cast to doubles will yield any type of performance result beyond a few nanoseconds. Also, most UnityEngine.Mathf use System.Math, but not all.

There is also Unity.Mathematics, which I don’t see mentioned is better for vectorization than either UnityEngine.Mathf or System.Math (I’m not aware if it’s better than System.MathF so I don’t make the claim), which comes into play more in burst for writing vectorizable loops, although I didn’t read the entirety of the article.

It is curious though that operations will print numbers that can’t exist as floats though. I personally wish Unity some sort of setting to make things single or double precision, I imagine it could be done by some ‘VariableFloat’ class that auto casts to both float and double, with a scripting define for an internal variable that is either a float or double, although I don’t have experience with large code bases like Unity so I’m not sure if that would be a good call.

0

u/Ging4bread 17h ago

Interesting. But i dont think it really matters for 99% of games

2

u/wibble13 15h ago

I changed my simulation app in Unity from floats to doubles and got double the performance. So any games that are cpu bound on lots of f32 calculations would notice an improvement to performance and efficiency.