int() is performing a flooring operation on variables. This is not what casting to int does in any programming language, ever.
Given the example, Variable1 = -0.6, when doing int(Variable1), it should return 0 and there are extremely valid reasons for this. Like when you're doing basic movement operations like "int(sin(270) * speed * dt)". At an angle of 270, for instance, there should be 0 movement from the Y axis. However, int is flooring. Flooring will take a number like -0.6 and make it the next lowest integer, which is, in the case of my example, -1.
The interesting part about this bug is that it only does it in this order. If you do int(-0.6), it will properly integerize it. I assume this is a static evaluation that occurs during compile time.
Variable1 = -0.6
int(Variable1) returns -1
Steps to Reproduce Bug
- Create variable by name of "Variable1"
- Set Variable1 to -0.6
- Print out "int(Variable1)"
- Chrome: YES
- FireFox: YES
- Internet Explorer: YES
Operating System and Service Pack
Construct 2 Version ID
The bug is in expressions.js, with this line of code
ExpValue.prototype.set_int = function (val)
assert2(cr.is_number(val), "Calling expvalue.set_int without number type");
this.type = cr.exptype.Integer;
this.data = Math.floor(val);
To fix it you need to do a typical JS int casting operation using a bitwise or op with 0.
[code:32wnyed3]this.data = val | 0;[/code:32wnyed3]
Alternative fix that doesn't truncate the larger integer values that can be stored as a float.
[code:32wnyed3]this.data = parseInt(val, 10);[/code:32wnyed3]
[b]Clarification for anyone confused by my post[/b]
This is the difference between floor, ceil, round, and int. I provided what results you should get and how it works (and some alternative examples if that helps).
floor(-1.6) should return -2
floor(1.6) should return 1
How it works: int(value) < 0 ? int(value) - 1 : int(value)
Alternative how it works: int(value) - 1 * (value < 0 ? 1 : 0)
ceil(-1.6) should return -1
ceil(1.6) should return 2
How it works: int(value) >= 0 ? int(value) + 1: int(value)
Alternative how it works: int(value) + 1 * (value >= 0 ? 1 : 0)
round(-1.6) should return -2
round(1.6) should return 2
How it works: int(value + 0.5 * (value < 0 ? -1 : 1))
int(-1.6) should return -1
int(1.6) should return 1
If your code currently uses int() and is stable, replace it with floor(), cause that's what int() is doing right now except that you'll have the proper function name.
[b]Definition of "integerizing"[/b]
This is programming jargon that I'm incredibly accustomed to and expect everyone to know, but this was obviously a lousy assumption on my part. So I'll provide the definition here.
To "integerize" is the act of casting, usually, a 32-bit or 64-bit floating point value to a 32-bit integer with the purpose of trimming off the decimal points, leaving only the whole number and its sign (- +).