Clamp Randomly Slightly Alters Floating-Point Number

Hey Guys,

Found an issue here that took me a little to see exactly what was happening but I wanted to point it out with some screenshots. If you look at the two following images, you will notice that the value falls within the clamps min and max however slightly changes the value and this kind of thing scares me a little in cases where I need the number exactly as I expect it and I’ve also noticed a problem where values change ever so slightly within the engine and also if lets say I’m entering in 180 degrees into Yaw for a component on a given blueprint, sometimes I get 179.9999999998 and can’t get it right on the dot. Someone mentioned this issue before to me and it is slightly annoying so I am assuming the two could be related. I’d really like to see this cleaned up and would be curious what is actually causing it. Another thing that happens is if I am typing 180 for that value or using the slider, it will make it negative sometimes so that’s confusing. For the meantime, I just Floor’ed both values and then compared them so that’s sufficient for the time being but hope this gets fixed in the near future. Thanks guys and take care.

Here are the screenshots from my Blueprint…

Hey -

What you are seeing is the approximations that floating point numbers use to represent a finite number. There are some cases where floating point numbers are unable to represent specific values (such as .1 or .01). When working with floating point numbers it is usually best to use either ‘greater than’ or ‘less than’ rather than attempting to use ‘equal to’. If you do need to compare to an exact number you may want to include a floor node (which will truncate the value) and then use an int instead of float.

Cheers

Ahhhh, I see. If I do need to see if the value comes out different, would you suggest creating a variable that acts as a threshold with a given range and then going ahead and doing a less than y but greater than x if I need to compare a float for a specific value?

EDIT: To be more specific, let’s say I need to check if Z = 1.001 which is what ‘Value’ is set to. If I create the Variable ‘Threshold’ and make it equal to 0.01, I would do the above like so…

If (Value >= Z - Threshold && Value <= Z + Threshold)
{
// Do Something…
}

For the time being, I already did the Floor that day and in this case, it works great for that specific area of the Blueprint.

Thanks !

The idea of creating a “buffer” variable is actually not a bad idea. That would give you a bit of a safe zone to catch any kind of minor variation in the float value.

Awesome, glad to hear you think so ! I figured if I needed to address the float, then that would be a good method. Anytime in the past that I have encountered issues where I needed to check the floating-point fraction of a value and maybe the precession isn’t reliable or maybe I need some room for error, creating a range and doing the above usually helps in keeping the consistency with a minor loss in precession. Thanks again for your insight on this matter and take care.