stepify not working on y axis but working on x axis

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

I am making a match statement that compares a vector’s x and y values, however, it only works for my x-axis.

print(distance_to)
print(vector2_to_string_match(distance_to))
var y_axis = stepify(distance_to.y, 0.1)
var x_axis = stepify(distance_to.x, 0.1)
print(str("y axis = ", y_axis, ", x axis = ", x_axis, "\ny_axis == -0.7 which is ", y_axis == -0.7,
	"\nx_axis == -0.8 which is ", x_axis == -0.8))

the results give me

(-0.754222, -0.656619)
y axis = -0.7, x axis = -0.8
y_axis == -0.7 which is False
x_axis == -0.8 which is True

I recreated it in a new godot project and it’s still working like this.

var vector = Vector2(-0.751234, -0.6545)
var y_axis = stepify(vector.y, 0.1)
var x_axis = stepify(vector.x, 0.1)

func _ready() -> void:
	print(x_axis == -0.8) # returns True
	print(y_axis == -0.7)# returns False

should the x_axis not be returning .6?

scrubswithnosleeves | 2021-05-08 15:44

na if its 0.5 it rounds up.
so it’s its 0.65 it would round the 5 up to be 0.7.
I believe that’s how it works at least.

halcyon | 2021-05-08 15:47

:bust_in_silhouette: Reply From: scrubswithnosleeves

Answering your Question LIVE on YouTube: https://www.youtube.com/watch?v=fFQLwxayunI

comparing floats is always a tricky business. I would guess that you have some floating point values from this operation that are causing issues. do you need to use == exactly?

prints(x_axis == -0.8, y_axis, is_equal_approx(y_axis, -0.7))

is_equal_approx() fixes the issue