Percentage Calculation returns 0

Started by Memor-X, May 19, 2013, 08:25:49 pm

Previous topic - Next topic

Memor-X

i have this event set up with a Call Script


returner = (30/200)*100
p returner


every time it runs, it returns 0 despite the fact it should be returning 15, the purpose of this is supposed to return a percentage

so any know why this doesn't works? it works fine of i just stick 30 or 200 in the variable but the moment i divide them it becomes 0 (and from that, 0 x # will always equal 0)


KK20

May 19, 2013, 08:35:01 pm #1 Last Edit: May 19, 2013, 08:36:18 pm by KK20
The magical world of ints and floats.
Quote
(30/200)*100 = (0)*100 = 0

(30.0/200)*100 = (0.15)*100 = 15

(30/200.0)*100 = (0.15)*100 = 15

Other Projects
RPG Maker XP Ace  Upgrade RMXP to RMVXA performance!
XPA Tilemap  Tilemap rewrite with many features, including custom resolution!

Nintendo Switch Friend Code: 8310-1917-5318
Discord: KK20 Tyler#8901

Join the CP Discord Server!

Memor-X

make......sense, strange how a variable will detect the difference between 15 and "15" being an integer and string but not a float, added .to_f in my original code to the variables 30 and 200 represented and it works now, thanks

KK20

If you tagged a .0 to the end, then the variable will recognize it as a float. The simple act of dividing doesn't instantly make the program think "Oh, my user must want decimal numbers." I mean, you could rewrite the '/' method in Integer if you wanted to, but that would be plain stupid. :P

Other Projects
RPG Maker XP Ace  Upgrade RMXP to RMVXA performance!
XPA Tilemap  Tilemap rewrite with many features, including custom resolution!

Nintendo Switch Friend Code: 8310-1917-5318
Discord: KK20 Tyler#8901

Join the CP Discord Server!

Blizzard

There is also something called a floating point precision error. Due to hardware limitations if you define 2.0, it may actually be 1.999999 and that prevision error propagates further during multiplication and summing. The alternative to avoid that and still have the proper result is to first do all multiplication operations.

Quote
30 * 100 / 200 = 3000 / 200 = 15


I personally avoid using floats whenever possible. Usually I use float only where the lack of precision isn't really a problem in the calculation.
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.