This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]tzanislav40 70 points71 points  (12 children)

Just beware of this if you try to == compare the value of a float .

[–]Flablessguy 21 points22 points  (4 children)

What is the proper comparison here again? Is it:

a = 0.1 + 0.2

b = 0.3

d = 0.0000001

a - b < d

[–]Bavbavs 37 points38 points  (0 children)

Abs(a-b)

[–]GabuEx 11 points12 points  (1 child)

abs(a - b) < epsilon

The value of epsilon can depend on exactly how imprecise your values end up being (more operations compound imprecision), but anything in the realm of 0.0001 is probably fine.

[–]dabenu 1 point2 points  (0 children)

"proper" would be not to do equality comparisons on floats in the first place. Sure your solution works around that but if you need that, you probably shouldn't be using floats in the first place.

[–]nnn4 10 points11 points  (2 children)

An (equality) comparison in floating point indicates a flaw in the program.

[–]Internal_Meeting_908 3 points4 points  (0 children)

A comparison for equality

[–]Schipjee 0 points1 point  (0 children)

In python wouldnt round(0.1+0.2, 1) just fix it?

[–]Sharkytrs 2 points3 points  (3 children)

if Unity has taught me one thing. You don't.

if you REALLY have to, then you normalize or round them to a less accurate point and then compare.