Comparing float numbers dosn't work

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

This is my code:

func draw_epicicles (nbOfSegments, listPositionOfSegments, nbOfEpicicles):

var Position1 = Vector2();
var Position2 = Vector2();
var color = Color(0.0, 1.0, 1.0);
var Times = nbOfEpicicles + 1;
var n = 0;
var m = 0;

for i in range (nbOfSegments):
    Position1 = listPositionOfSegments[i];

    n = floor(i*Times/nbOfSegments);
    m = i*Times/nbOfSegments;

    if (m>=0.9):
        i = i * Times - nbOfSegments * n;
    Position2 = listPositionOfSegments[i*Times];

    draw_line(Position1, Position2, color, 3, false);


As example values to test this programs I have set nbOfEpicicles to 2, which means Times is equal to 3 (2+1). nbOfSegments is equal to 50 in this test. And that’s it for the variables

So it should draw a line for every value of i.
I run this program. It gets an error which says 57 is not a valid value for the array listPositionOFSegments. That is because it’s size is equal to 50, to the number of segments.
I use the mouse to see the value of every variable.
i = 19 when I get this error. That’s why it’s trying to use 57 as the index, because it is 19 * 3.

But, I have set a few lines of code that should prevent this by comparing i * Times/nbOfSegments with 1. So it compares 19 * 3/50 which is equal to 1,14 with 1.
1.14 is clearly greater than 1.
This means it should subtract 50 to 57 so it should now be 7. It doesn’t
The reason is because for some reason i*Times/50 is set to 1 instead of 1.14 which is clearly wrong. I need it to be a float number so that the program knows it has to subtract 50 * 1 to 57 to set it to 7.

Also for some reason I made the program compare m with 0.9 instead of 1, so it is obviously greater than 0.9 but even tho I done that, it still doesn’t subtract 50 to 57.
I also realised I can’t change the value of i all the time to be less than 50 because it would make this an infinite recursion but that is easily solved.

So any way of solving this?
Thank you in advance!

:bust_in_silhouette: Reply From: SIsilicon

The way Godot’s gdscript works is that the type of numbers when doing division is implicit.
Dividing an integer with anything else, or the other way around will give an integer result; effectively flooring it.
Which explains why the comparison isn’t working for you. Times and nbOfSegments are both implicit integers; thus giving you an expected result with the division.

#Instead of doing-
m = i*Times/nbOfSegments

#Cast either Times or nbOfSegments to a float.
m = i*Times/float(nbOfSegments)

Oh, I understand, thank you!
I have changed the program.Deleted the m variable and changed it to just i*Times, aka comparing if 57 is greater than 50, and it now works wonder! I now need to create a menu to change the variables and boom, my first public program will be finished.
Have a nice day!

C:\Flavius | 2018-05-11 14:18

Same to you.

SIsilicon | 2018-05-11 17:14

Thanks for selecting my answer.
I can’t wait to see what your working on.

SIsilicon | 2018-05-17 21:01