[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
11
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,822
Reaction score
14,021
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
11
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
328
Reaction score
125
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,822
Reaction score
14,021
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,579
Reaction score
8,642
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

Have you ever gone full 'Ship of Theseus' while frankenspriting? Because I just did.
I saw my new eye doctor today, he reminds me of Satoru Iwata for some reason
I've been using a modified version of a script called Multiple Messages. It makes my text boxes look like a Mario & Luigi game! There is something about it that bothers me a little bit, but I shouldn't worry about that now.

Side note, Mist's walking animation is almost perfectly in sync with Loose Yourself by Eminem.
Starting work on some stuff for the community. Hopefully it will be fun, and useful to somebody.
Searching what to play in my dead times, the nostalgia hit me hard and ended downloading the Patapon trilogy, hope I can avenge my child self and beating all the games hahaha

Forum statistics

Threads
107,769
Messages
1,032,014
Members
139,913
Latest member
EpicLast
Top