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

Status
Not open for further replies.

ZachyTemp

[Insert Title]
Member
Joined
Apr 20, 2020
Messages
8
Reaction score
1
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
40,727
Reaction score
13,997
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]
Member
Joined
Apr 20, 2020
Messages
8
Reaction score
1
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
318
Reaction score
123
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
40,727
Reaction score
13,997
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,549
Reaction score
8,625
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.

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

Latest Threads

Latest Posts

Latest Profile Posts

Who would think putting a power button on a side of a laptop is a good thing? You want to move the laptop and you suddenly press the button.
The second draft of my script is almost done, and I don't think I'm going to write a third draft. I'll make changes to dialogue here and there, but I'm happy with the series of events the way they are.
How you feel about font and dialogue box changing for every main character while the minor cast and regular npc get their own styles? Too much?
Just found an obviously custom effect (i.e. edited by me) in my resources>sounds folder called "rip jaw off". Dear Lord what is wrong with me as a person that at some point I needed a sound *specifically* for THAT??
(...rhetorical question.)

Forum statistics

Threads
107,526
Messages
1,030,279
Members
139,652
Latest member
jasasewamobilbali
Top