[VXA] "Can't divide by zero" error because of variable getting rounded up.

Status
Not open for further replies.

ZachyTemp

[Insert Title]
Veteran
Joined
Apr 20, 2020
Messages
31
Reaction score
14
First Language
Spanish
Primarily Uses
RMVXA
Hello,

I'm developing a game like Typeracer and I've been having some trouble when calculating CPM (Characters per Minute). Here's the code I made:

Ruby:
distance = $game_variables[2]     # Define distance (all typed letters)
minutes = $game_variables[4] / 3600 # Define time (frames -> minutes)

cpm = distance / minutes            # Speed equals distance divided by time

# Split CPM into separate integers
cpm1 = cpm / 1000
cpm2 = (cpm / 100) % 10
cpm3 = (cpm / 10) % 10
cpm4 = cpm % 10

# Show Numbers as Pictures
  if cpm >= 1000
   screen.pictures[17].show("Word - C_#{cpm1}", 0, 416 + 15*0, 551, 100, 100, 255, 0)
   screen.pictures[18].show("Word - C_#{cpm2}", 0, 416 + 15*1, 551, 100, 100, 255, 0)
   screen.pictures[19].show("Word - C_#{cpm3}", 0, 416 + 15*2, 551, 100, 100, 255, 0)
   screen.pictures[20].show("Word - C_#{cpm4}", 0, 416 + 15*3, 551, 100, 100, 255, 0)
  elsif cpm < 1000 && cpm >= 100
   screen.pictures[17].show("Word - C_#{cpm2}", 0, 416 + 15*0, 551, 100, 100, 255, 0)
   screen.pictures[18].show("Word - C_#{cpm3}", 0, 416 + 15*1, 551, 100, 100, 255, 0)
   screen.pictures[19].show("Word - C_#{cpm4}", 0, 416 + 15*2, 551, 100, 100, 255, 0)
   screen.pictures[20].erase
  elsif cpm < 100 && cpm >= 10
   screen.pictures[17].show("Word - C_#{cpm3}", 0, 416 + 15*0, 551, 100, 100, 255, 0)
   screen.pictures[18].show("Word - C_#{cpm4}", 0, 416 + 15*1, 551, 100, 100, 255, 0)
   screen.pictures[19].erase
   screen.pictures[20].erase
  elsif cpm < 10
   screen.pictures[17].show("Word - C_#{cpm4}", 0, 416 + 15*0, 551, 100, 100, 255, 0)
   screen.pictures[18].erase
   screen.pictures[19].erase
end

As soon as the player presses a key, a timer starts adding 1 to the time variable every frame.

The problem is that when "time" is lesser than 3600, when you convert it to minutes it will return a decimal value that gets rounded up to zero.
Eg: time = (1 minute / 3600 frames) = 2,77*10^-4 minutes

I tried using different time units, but the result is always inaccurate because I basically can't use any decimals. Is there any way to fix this?

Thank you.
 
Last edited:

Shaz

Global Moderators
Global Mod
Joined
Mar 2, 2012
Messages
41,302
Reaction score
14,278
First Language
English
Primarily Uses
RMMV
It is not "rounding up" to 0. It is truncating to 0.

Code:
minutes = $game_variables[4] / 3600.0

will prevent that. But then you'll have to convert all the cpm calculates back to integers prior to showing the pictures.
 

ZachyTemp

[Insert Title]
Veteran
Joined
Apr 20, 2020
Messages
31
Reaction score
14
First Language
Spanish
Primarily Uses
RMVXA
Code:
cpm = (distance / minutes).round

Thank you! I used "cpm.to_i" and it seems to work fine too ^^

It is not "rounding up" to 0. It is truncating to 0.

Code:
minutes = $game_variables[4] / 3600.0

will prevent that. But then you'll have to convert all the cpm calculates back to integers prior to showing the pictures.

Works like a charm. Thank you!
I didn't know about truncation. From what I've been able to research, looks like you need to include a float value in your operation if you want a decimal number as a result.

You may close this thread now :D
 

KK20

Just some XP Scripter
Veteran
Joined
Oct 11, 2018
Messages
347
Reaction score
132
First Language
English
Primarily Uses
RMXP
I used "cpm.to_i" and it seems to work fine too
You should know the difference between them though.

to_i => drops everything after the decimal (1.9 becomes 1, -1.1 becomes -1)
ceil => always rounds up (1.1 becomes 2, -1.9 becomes -1)
floor => always rounds down (1.9 becomes 1, -1.1 becomes -2)
round => rounds to whatever is the closest integer
 

Shaz

Global Moderators
Global Mod
Joined
Mar 2, 2012
Messages
41,302
Reaction score
14,278
First Language
English
Primarily Uses
RMMV
You may close this thread now

You should report your first post and let the mods know your question is answered. Then they will close it for you.
 

mlogan

Global Moderators
Global Mod
Joined
Mar 18, 2012
Messages
15,795
Reaction score
8,764
First Language
English
Primarily Uses
RMMV

This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

 
Status
Not open for further replies.

Latest Threads

Latest Profile Posts

6 more weeks of this nutty semester. Then maybe I'll have time to game make. After I curl up and sleep for a day or two that is.
Wondering how big I can make Dorothy's skill menu before the game just becomes irredeemably bad.
Studio Blue is playing the RPG Maker game Phil Alone live now! Phil may be alone, but you don't have to be, come join us in chat!

I'm in desperate need of environment sprites and tilesets for my game. If anyone is interested please DM me im willing to pay.

Forum statistics

Threads
109,208
Messages
1,043,071
Members
141,725
Latest member
718346584zy
Top