Floating point error?

Can someone please explain to me why this is happening and how to fix it?

Here’s the code:

image

Thanks!

Not all numbers can be perfectly represented with floating point arithmetic, there are just not enough combinations in 64 bits. Some precision may also be lost from the arithmetic operations you or roblox do. First I would consider the question: at this level of imprecision is there really any adverse effect on the game experience?

Not all numbers can be perfectly represented with floating point arithmetic

But they can? Look at the print before the function. First and second image. Line 855. Why does it break after the create rod? I assume it’s the welds, but why does that matter?

First I would consider the question: at this level of imprecision is there really any adverse effect on the game experience?

Not really, but it makes coding a lot harder, and maybe there will be a performance drop since I have to now include extra rounding functions that run every heartbeat.

Thanks!

There are an infinite amount of numbers. 64 bits has a finite amount of combinations, therefore not all numbers can be represented. Assuming -0.125 can be represented perfectly by floating point, the imprecision can still come from the arithmetic operations that take place with the welds under the hood or with what you do. The calculations may introduce a number that cannot be perfectly represented therefore it will lose precision. Perfect precision for ANY number is straight up impossible. If you want to minimize the amount lost due to intermediate calculations you can either round like you said or use big number arithmetic that supports decimals, which would be more costly on performance, and convert back to floating point.

I’ve tried rounding it, but then it goes back for some odd reason.
Here’s the new code, with the rounding equations, and still, it’s off.


How come it still doesn’t work?

hinge.Position = Vector3.new(  math.round(hinge.Position.X / GridSize.Value)  ,  math.round(hinge.Position.Y / GridSize.Value)  ,  math.round(hinge.Position.Z / GridSize.Value)  ) * GridSize.Value

GridSize.Value = 0.125 by the way

Thanks!

First of all, math.round should only be returning integers per the documentation so it could just be the welds overriding the position you set assuming they are still active. If it is simply just roblox doing something on their end in the engine then you cannot prevent it from losing precision from that calculation. If you need perfect grid values maybe store the coordinates outside of the actual position property, such as attributes or vector3 value instances, or just calculate the grid value whenever you need to use it. The actual position shouldn’t matter if its off by only a tiny decimal, it won’t even affect the rendering/rasterization most likely.

First of all, math.round should only be returning integers per the documentation

I am aware, that’s why there’s a copious amount of parenthesis :joy:

welds overriding the position you set assuming they are still active

Yep, they’re still active

calculate the grid value whenever you need to use it

I’ll sadly have to go with this solution. This will make my code extremely messy, but, it leaves me no choice. Maybe roblox will fix this in the future, they’ll rewrite the welding mechanics. Who knows.
Thanks!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.