[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,149
Reaction score
14,206
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
345
Reaction score
128
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,149
Reaction score
14,206
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,749
Reaction score
8,744
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 Posts

Latest Profile Posts

Does anyone know of NPC sprites for RMMZ? I know of Flirion's RMMV recolours, but RMMZ's sprites are slimmer and more detailed, so I'm a bit on the edge to use Flirion's im RMMZ...
I can't believe that after all those years, KH Union X Dark Road is closing the services.

Due to a situation in which I thought plugin adaptations had stopped for the QPlugins, I "ghetto" converted these 7 over yesterday. They appear to work perfectly as long as you use old style plugin commands. I am uncertain if I will have a lot of free time in the future to work on this, but if you'd like them, here they are.
Was there a thread that you could share a screenshot of your game? I feel like I found it once but can't locate it again.

I was being lazy and just copied a map to test a new parallax, but I forgot to delete all the events. This was the result. :o

Forum statistics

Threads
108,818
Messages
1,039,786
Members
141,277
Latest member
acoolusername
Top