Strange behavior with trigonometric functions

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Sudde

zero returned by trigonometric functions are greater than zero

func _ready():
        print('--------')
        print(sin(PI))
        print(sin(PI)>0)
        print('--------')
        print(cos(PI/2))
        print(cos(PI/2)>0)
        print('--------')

output

--------
0
True
--------
0
True

Is this expected behavior?

:bust_in_silhouette: Reply From: jgodfrey

While I don’t know if it’s expected, I wouldn’t say it’s unexpected. Floating point values are a nasty business under the covers… For example, if I print one of your samples out to a sufficient number of decimals, I do see some positive lean. So…

print("%.20f" % sin(PI))

returns 0.00000000000000010000

Moreover, see the is_equal_approx function if you need some well-behaved comparisons

@GDScript — Godot Engine (stable) documentation in English

alexp | 2022-10-16 17:46