Float/double comparison (precision issue)

Hey everyone, hope you all doing good.

Here I just encountered a weird issue or maybe something minute I’m missing idk, but I guess dynamo is high :joy:, have a look at the snip below.

I was working on one script and here I came across an exception as I kept task dialogs for the exceptions but in here as you can see output of python node the check states it as false whereas it is true 64.612 == 64.612 isn’t it??? and I’m unable to execute it the way I want coz of this only. Am I missing something here…???

Hi,

comparing floats for equality is a little fiddly due to rounding and precision issues.

Try this workaround

abs(number_float1 - number_float2) < 0.001

more info here

7 Likes

Thankyou @c.poupin


I understood that the reason in this precision only but, The workaround comes as true as i highlighted in the image above the difference is less than 0.001 but it isn’t 0 for sure and also as you can see for the case of 3 string the numbers are given in the same manner, but it works perfectly there.

Check out math.isclose in Python 3

4 Likes

Thankyou both @Mike.Buttery & @c.poupin .

I’m closing the thread marking the solution both of you gave great insights. Kudos, gracias amigos :clap:t2:

3 Likes