Greetings, I have experienced strange problem with integers division while writing a script.
Dividing one integer by another always resulted in a number without fractional part, even when it is supposed to be present.
Let me illustrate this with an example. Here is the code:
local test ={}
test = gg.prompt({'Input number:'},{},{'number'})print(test[1])print(tonumber(test[1]))print(tonumber(test[1])/7)
And the result of it's execution:
As you see, fractional part is not there, athough it should have been.
I have figured out a solution to fix this. Adding ".0" to an integer and passing this to "tonumber" function seems to produce a floating-point number, I guess.
Replacing last line from code above with this one:
print(tonumber(test[1]..'.0')/7)
results in next output (division result with fractional part):
So my questions are next.
Why is this happening?
Are there other ways to deal with this problem?
Thank you in advance!
P.S. This problem does not appear when executing practically same code as above with Lua interpreter.
Code:
local test ={}
test[1]=100print(test[1])print(tonumber(test[1]))print(tonumber(test[1])/7)
We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.
Question
CmP
Greetings, I have experienced strange problem with integers division while writing a script.
Dividing one integer by another always resulted in a number without fractional part, even when it is supposed to be present.
Let me illustrate this with an example. Here is the code:
And the result of it's execution:
As you see, fractional part is not there, athough it should have been.
I have figured out a solution to fix this. Adding ".0" to an integer and passing this to "tonumber" function seems to produce a floating-point number, I guess.
Replacing last line from code above with this one:
results in next output (division result with fractional part):
So my questions are next.
Why is this happening?
Are there other ways to deal with this problem?
Thank you in advance!
P.S. This problem does not appear when executing practically same code as above with Lua interpreter.
Code:
Output:
Link to comment
Share on other sites
1 answer to this question
Recommended Posts
Archived
This topic is now archived and is closed to further replies.