Why do my decimal division results return zero mid-call?

Arsist

Veteran
Veteran
Joined
May 16, 2014
Messages
140
Reaction score
10
Primarily Uses
$game_variables[2] = ((131 / (56 * 12)) * 1000).to_ior

$game_variables[2] = ((131 / (56 * 12)) * 1000)becomes zero but

$game_variables[2] = (131000 / (56 * 12))becomes 194.

What's up with that? How do I work past that?
 
Last edited by a moderator:

Yato

(aka Racheal)
Veteran
Joined
Mar 17, 2012
Messages
825
Reaction score
346
Primarily Uses
If you don't specifically tell the program you are working with floats (decimals), it treats everything as integers and cuts off any remainder. Try changing the first value to 131.0 and wrapping the entire thing in (equation).to_i to convert it back to an intergar at the end.

Edit: Though you will probably want to round it before converting it to an int. .round or something lile that.
 
Last edited by a moderator:

Andar

Veteran
Veteran
Joined
Mar 5, 2013
Messages
31,355
Reaction score
7,667
First Language
German
Primarily Uses
RMMV
that's because the default calculations are always integers and always cut off unless specifically changed to float.


You either have to make the divisions last (that will only drop the last decimal) or you have to use float numbers in your calculations (131.00 instead of 131) - the first solution is more practical in my opinion because with floats, the engine only changes to float values after the first use of a float, and you can get type errors if you go out of sequence.
 

Arsist

Veteran
Veteran
Joined
May 16, 2014
Messages
140
Reaction score
10
Primarily Uses
So if I don't know if something will turn into a whole number or not, can I multiply it by 1.0 to tell the game I'm working with more than just decimals?

And go out of sequence?
 
Last edited by a moderator:

Andar

Veteran
Veteran
Joined
Mar 5, 2013
Messages
31,355
Reaction score
7,667
First Language
German
Primarily Uses
RMMV
So if I don't know if something will turn into a whole number or not,
You do know that - because unless you specifically command otherwise, everything will turn into whole integer numbers.
 

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
40,098
Reaction score
13,704
First Language
English
Primarily Uses
RMMV
I've moved this thread to RGSSx Script Support. Please be sure to post your threads in the correct forum next time. Thank you.


Yep, variables are integers unless you explicitly tell it otherwise. And since it follows the normal order of mathematical operations, the first point you introduce that .0 is when it will start treating them as floats.


Your first formula will work if you just change 131 to 131.0
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

How many parameters is 'too many'??
Yay, now back in action Happy Christmas time, coming back!






Back in action to develop the indie game that has been long overdue... Final Fallacy. A game that keeps on giving! The development never ends as the developer thinks to be the smart cookie by coming back and beginning by saying... "Oh bother, this indie game has been long overdue..." How could one resist such? No-one c
So I was playing with filters and this looked interesting...

Versus the normal look...

Kind of gives a very different feel. :LZSexcite:
To whom ever person or persons who re-did the DS/DS+ asset packs for MV (as in, they are all 48x48, and not just x2 the pixel scale) .... THANK-YOU!!!!!!!!! XwwwwX

Forum statistics

Threads
105,848
Messages
1,016,974
Members
137,562
Latest member
visploo100
Top