GML [Solved] How do I turn an int into a float?

XirmiX

Member
I have come across this issue before, where the program won't trigger a certain event because it's checking a float with an integer value. Is there some way to turn an integer to a floating point number? I've searched for this, but couldn't find what I wanted or in fact anything related to this, not even the manual seems to present any built-in function that I'm looking for.
 

cidwel

Member
Do +0.0 work?
Maybe you can tell us what are you trying exactly to do to avoid float testing in a language like gml
 

XirmiX

Member
All numbers are stored as floats in GML.

There is no need to convert an integer to a float.
No... there is! I've had this issue before where a number doesn't have 0.0, is an integer and needs to be converted to float in order to trigger an event check that involves the same number, but because one of the checked numbers has .00 and the other doesn't, the event doesn't trigger.

Do +0.0 work?
Maybe you can tell us what are you trying exactly to do to avoid float testing in a language like gml
Set the x and y coordinates as a floating point number with .00 at the end. I've tried your method, both during the if statement and upon the object's creation, but neither worked.
 
W

Wraithious

Guest
I believe that GMS treats variables on how they are defined or modified, from the manual:
Code:
Real numbers in GameMaker: Studio are considered double-precision floating-point numbers, that is to say that they have a
decimal point in them with no fixed number of digits either before or after the point. 2, for example, is a binary integer
but 2.0 is a floating point real. This distinction between integers and floats is very important when dealing with cross platform
development as the precision of calculations made on a Windows pc is not the same as the precision of those same
calculations when made on a mobile device. This means that you should pay particular attention when making comparisons, for example:

if image_index == 1 {do something...}

 Now that may appear to work, but due to the way that floating point maths works, it may mean that image_index
actually equals 1.00000000001, in which case the comparison will never result in a true result and the code running.
These types of errors can be quite hard to debug and so it is always good to be aware of them and to plan ahead and use
other means of checking values or to use the appropriate flooring or rounding functions (listed below) to convert the
number to check into an integer. For example the above code could be written as:

if floor(image_index) == 1 {do something...}

NOTE: On the YoYo Compiler target platforms (those marked (YYC)), expressions and functions are evaluated from
left to right, while on all other target platforms they are evaluated from right to left, meaning that this:
val = max(num, ++num, num++);

will give different results depending on the platform.

 We can also use a special function available in GameMaker: Studio to set the epsilon value for floating point maths.
When a real number is rounded to the nearest floating point number, the epsilon (also know as "machine epsilon")
forms an upper bound on the relative error, and you can set the epsilon value using this special function:

◦math_set_epsilon
◦math_get_epsilon
So if you have a number you defined as 1.00 and you later change it such as myVariable=40 then it will treat it as an integer from then on until you later change it to 20.00 for example.
 
Last edited by a moderator:

Dog Slobber

Member
No... there is! I've had this issue before where a number doesn't have 0.0, is an integer and needs to be converted to float in order to trigger an event check that involves the same number, but because one of the checked numbers has .00 and the other doesn't, the event doesn't trigger.
You misdiagnosed what the cause of your problem actually was.. You can't convert a number (what you believe to be stored as an integer) to a float, because it already is a float.

The past problem you experienced was NOT because you were comparing an integer to a float, but because you were comparing a whole number to a number that was very close to a whole number, but not a whole number.

This is because there is a problem that many numbers can't be exactly represented using floating point.

Studio will often display numbers to two decimal points, even though there are more significant digits. i.e. 6.00000028 will be displayed as 6.00.
 
Last edited:

kburkhart84

Firehammer Games
An option could be to go the other direction...instead of do int to float, go float to int. The documentation quoted above mentions the floor() function. I bet that would work for this, as it gets rid of the decimal in case there is a 0.0000001 attached that you can't see.
 

XirmiX

Member
An option could be to go the other direction...instead of do int to float, go float to int. The documentation quoted above mentions the floor() function. I bet that would work for this, as it gets rid of the decimal in case there is a 0.0000001 attached that you can't see.
Yeah, I guess that could work with an extra variable... but in the case of checking precise floating points, which I believe to be the case for me, this will not work. If there is no functionality for changing integers to floats in GM, this should really be implemented in the engine in some form, or at least, have checks between the two types when the numbers are the exact same except some decimal 0s at the end of one, to return true instead of false.

I have solved the situation in a different format, by instead checking the amount of iterations with a variable, that have been done in terms of movement for one of the objects in question for this. I might change this though if the aforementioned mechanic(s) are/is implemented for GM. In fact this would probably be better for both processing and easier in terms of flexibility for changing certain types of objects to something else. The objects in question for me are garages and their respective doors when opening and closing. If I wanted to implement new types of garage doors which take longer or quicker to open, I would need to change the iteration variable code. With the floating point check working or with a way to convert an integer to a floating point, I wouldn't have to do so for every new type of garage.
 

Dog Slobber

Member
Yeah, I guess that could work with an extra variable... but in the case of checking precise floating points, which I believe to be the case for me, this will not work. If there is no functionality for changing integers to floats in GM, this should really be implemented in the engine in some form, or at least, have checks between the two types when the numbers are the exact same except some decimal 0s at the end of one, to return true instead of false.
This is not what's happening.
 

kburkhart84

Firehammer Games
Another thing to consider...you can always just use greater than and less than for many things. I don't details of your game and how you are doing things, but consider...if the variable can't be equal to exactly 1(due to floating point or whatever), you can always say is it greater than 0.999 and less than 1.001, if both of those is true, than the value is equal to 1, or close enough for your purposes.
 

XirmiX

Member
This is not what's happening.
From my testing, yes it is. If not, then what may it be? Before, I had this same issue, but was able to have both numbers to be set as floats and that fixed the issue for me before. This time I can't do that, and I did a counter-system, which again, made it work. Nothing but the changes around the scope of these mechanics were being coded that could be affecting this.
 

Dog Slobber

Member
The cause has been explained to you, and you simply won't accept it.

You keep insisting that Studio needs the functionality to convert integers to floats. This can't be the case. Numbers are already stored as floats, this is well known and documented. The documentation was even provided to you.

You seem to think that comparing a whole number is returning false because the float has an extra .00. Once again this can't be the case. Usig double precision floating point there is no way to diferentiate the storage of 5, 5.0, 5.00, 5.000. It can't be done.

If your theory is correct the expression:
if 5 == 5.00 could return false. It can't.

The problem you are experiencing is a loss of precission with floating point numbers, you could have domonstrated it with the string_format() function. This way you would have seen beyond 2 decimal points and known it was not .00 but instead something along the lines of .00000000000003.

And your problem is a common one, asked by many, especially those who do equality comparisons to floating point non-whole or calculated numbers that use non-whole numbers. Somethng you should never do.

In closing I could easily show code that supports my explanation, I doubt you could show code demonstrating yours.
 
Last edited:

FacesOfMu

Member
Sorry to necro a thread, but I just had this problem and this came up in a search on it. For the OP or others who find this thread, I think I solved it by rounding my numbers like this:
Code:
var dir_r = round(dir * 100)/100; // rounded to 2 decimal places
With this, it multiplies the number to get all the digits I want above the decimal, rounds it to get rid of the remaining decimal places, then divided it to return the decimals I need.
If it happens again, I will probably make a script where I can set the number of digits to round to.

rounded(n ,d)
Code:
var n = argument[0];
var d = 0;
if(argument_count == 2) d = argument[1];
return round(n * power(10, d)) / power(10, d);
I'm a little surprised that the inbuilt round() function doesn't have this option already.
 
Last edited:
Top