Striving to become a master Go/Cloud developer; Father ๐จโ๐งโ๐ฆ; ๐ค/((Full Stack Web|Unity3D) + Developer)/g; Science supporter ๐ฉโ๐ฌ; https://coder.today
Nice, in Unity3D (C#) we used a function that is builtin in the engine:
// Compares two floating point values if they are similar.publicstaticboolApproximately(floata,floatb){// If a or b is zero, compare that the other is less or equal to epsilon.// If neither a or b are 0, then find an epsilon that is good for// comparing numbers at the maximum magnitude of a and b.// Floating points have about 7 significant digits, so// 1.000001f can be represented while 1.0000001f is rounded to zero,// thus we could use an epsilon of 0.000001f for comparing values close to 1.// We multiply this epsilon by the biggest magnitude of a and b.returnAbs(b-a)<Max(0.000001f*Max(Abs(a),Abs(b)),Epsilon*8);}
"approximately" equal was implemented in all types that had floating precision, including 2D and 3D vectors, you never need the "normal" equality because things were never exactly the same, especially when objects were affected by forces in a physics simulation like a physics engine.
I think epsilon can be calculated in Go using the formula (a very small floating value)
That makes tons of sense to have an approved way to do it in Unity3D, considering how critical floating point precision is to physics simulations! (I'm looking at you, KSP.)
Yeah, there are likely far better comparison functions -- I'm still playing around with one that'll work for me. For this though, I was just demonstrating how to use the comparer option, and simple is always best for blog posts :-)
Striving to become a master Go/Cloud developer; Father ๐จโ๐งโ๐ฆ; ๐ค/((Full Stack Web|Unity3D) + Developer)/g; Science supporter ๐ฉโ๐ฌ; https://coder.today
Definitely, but your post made me curious how that function works, I used it but never saw its implementation.
Now I'm curious what is the most optimal way in Go to do it, to compare the first X decimals in a float, in Go, so your post worked! it made me a better dev.
Nice, in Unity3D (C#) we used a function that is builtin in the engine:
"approximately" equal was implemented in all types that had floating precision, including 2D and 3D vectors, you never need the "normal" equality because things were never exactly the same, especially when objects were affected by forces in a physics simulation like a physics engine.
I think epsilon can be calculated in Go using the formula (a very small floating value)
That makes tons of sense to have an approved way to do it in Unity3D, considering how critical floating point precision is to physics simulations! (I'm looking at you, KSP.)
Yeah, there are likely far better comparison functions -- I'm still playing around with one that'll work for me. For this though, I was just demonstrating how to use the comparer option, and simple is always best for blog posts :-)
Thanks for sharing!
Definitely, but your post made me curious how that function works, I used it but never saw its implementation.
Now I'm curious what is the most optimal way in Go to do it, to compare the first X decimals in a float, in Go, so your post worked! it made me a better dev.
๐