[Poll] Rounding Up/Down?

What is your preferred method of rounding?

  • Math.ceil()

    Votes: 0 0.0%
  • Math.floor()

    Votes: 0 0.0%

  • Total voters
    6

Frostorm

[]D[][]V[][]D aka "Staf00"
Veteran
Joined
Feb 22, 2016
Messages
2,030
Reaction score
1,708
First Language
English
Primarily Uses
RMMV
Just a quick survey. Do you guys typically round up (Math.ceil()), round down (Math.floor()), or round to the nearest integer (Math.round())? Or perhaps you use different methods in various situations. If so, when do you round up? When do you round down? Etc...
 

ATT_Turan

Forewarner of the Black Wind
Veteran
Joined
Jul 2, 2014
Messages
2,211
Reaction score
1,179
First Language
English
Primarily Uses
RMMV
The counter-question is how often would it make any difference? I most often truncate any decimals (which is rounding down, but not because I care about rounding down - it's just the fastest operation).
 

Frostorm

[]D[][]V[][]D aka "Staf00"
Veteran
Joined
Feb 22, 2016
Messages
2,030
Reaction score
1,708
First Language
English
Primarily Uses
RMMV
At the end of the day, you still have to pick one right? Why is rounding down faster than the others? Does it take less processing power or something (not that it would be noticeable)? Wouldn't rounding up be just as fast as rounding down?

Or if you're using small numbers, rounding can be a noticeable difference. For example, I had my units' movement range based on a function of the character's Speed stat, and movement range typically only ranges from 1-5 tiles or so. So rounding up or down can be a difference of up to 20%!
 

Nolonar

Veteran
Veteran
Joined
Feb 18, 2018
Messages
272
Reaction score
375
First Language
French, German
Primarily Uses
RMMZ
Just a quick survey. Do you guys typically round up (Math.ceil()), round down (Math.floor()), or round to the nearest integer (Math.round())? Or perhaps you use different methods in various situations. If so, when do you round up? When do you round down? Etc...
Depends on what I'm trying to do.

If I need integer operations (e.g. 5 / 2 = 2), I use Math.floor(). If I need to display floating numbers, but don't want the number to have a bazillion decimal digits, I may use Math.round() instead.

They each have their use-case, though I don't remember when I ever needed Math.ceil().

Why is rounding down faster than the others? Does it take less processing power or something (not that it would be noticeable)? Wouldn't rounding up be just as fast as rounding down?
Because of how modern computers handle fractional numbers (aka "float" and "double").

Floats (and doubles) are constructed as: 2^x * 1.f (x being the exponent, and f being the fraction). This may look complicated, but simply put: one part represents the number, e.g. 12345, and the other part represents the position of the decimal point, e.g. 2. So using my example, 0.12345 * 10^2 = 12.345. Obviously, the computer will use binary numbers instead (and instead of 0., it will use 1.), but the basic principle is identical and just as simple.

This means that if you're trying to convert a number from fractional to integral, you simply take the first x digits of f (if x is positive) and add 1 to the left end of the number. If x is 0, the number is 1, and if x is negative, the number is 0.

If you wanted to round up, you'd need to additionally look at the digits you don't take, and see if at least one of them is non-zero (and add 1 to the number if needed). This extra work is why rounding up is slower than rounding down.
 

kirbwarrior

Veteran
Veteran
Joined
Nov 13, 2014
Messages
922
Reaction score
674
First Language
English
Primarily Uses
N/A
I prefer to not round if I can help it. But when I have to, I often try to round in the player's favor. Otherwise, I drop fractions (round towards 0) but only at the end of the equation (namely, when the number actually matters, such as the end result of damage).
 

ATT_Turan

Forewarner of the Black Wind
Veteran
Joined
Jul 2, 2014
Messages
2,211
Reaction score
1,179
First Language
English
Primarily Uses
RMMV
At the end of the day, you still have to pick one right? Why is rounding down faster than the others? Does it take less processing power or something (not that it would be noticeable)? Wouldn't rounding up be just as fast as rounding down?
In addition to what Nolonar explained, I typically work with languages other than JavaScript, that use data types or classes. So putting a value into an integer class immediately discards the decimal points, which is faster than running the value through a function that modifies it either way.
Or if you're using small numbers, rounding can be a noticeable difference. For example, I had my units' movement range based on a function of the character's Speed stat, and movement range typically only ranges from 1-5 tiles or so. So rounding up or down can be a difference of up to 20%!
You're right about how much of a difference that is. My experience with board/role-playing game rules puts me in the mindset of thresholds - that is, for example, every 6 points of Speed gives a square of movement, so I'd be using that and not ending up with decimals in the first place. But you should do it whichever way you find better for your game's experience.
 

Frostorm

[]D[][]V[][]D aka "Staf00"
Veteran
Joined
Feb 22, 2016
Messages
2,030
Reaction score
1,708
First Language
English
Primarily Uses
RMMV
@Nolonar Oh cool, I learned something new and interesting today, thx!
for example, every 6 points of Speed gives a square of movement
Hmm, I thought of doing it like that as well. Which would be functionally equivalent to rounding down, right? Like moveRange = Math.floor(a.agi / 6)

Anyways, I brought the subject up because I was going thru Yanfly's sample project and noticed that sometimes things were rounded up and other times rounded down. I just wanted to figure out the reasoning for doing things that way.
 

ATT_Turan

Forewarner of the Black Wind
Veteran
Joined
Jul 2, 2014
Messages
2,211
Reaction score
1,179
First Language
English
Primarily Uses
RMMV
Hmm, I thought of doing it like that as well. Which would be functionally equivalent to rounding down, right?
Functionally, yes, which is what I indicated in my first reply. Although Math.trunc() is more accurate for this usage than Math.floor().
Anyways, I brought the subject up because I was going thru Yanfly's sample project and noticed that sometimes things were rounded up and other times rounded down. I just wanted to figure out the reasoning for doing things that way.
If you're asking in reference to a specific example, and you can't figure it out from reading through the code, then the question would be more usefully asked about that example rather than generally (less "What do you do" and more "Why did Yanfly do this"). That will get you more specifically useful answers.

What spots in this sample project are you curious about?
 

TheoAllen

Self-proclaimed jack of all trades
Veteran
Joined
Mar 16, 2012
Messages
6,326
Reaction score
7,915
First Language
Indonesian
Primarily Uses
RMVXA
For stats, it is rounding down.
For damage done, it is the nearest integer.

The stats are shown in the battle user interface as well as the damage formula.
So, if I use a.atk * modifier, if the attack stat is supposed to be 90.9, it becomes 90. The reason is that the modifier could be massive depends on the buff you have before attacking (or enemy debuff). One integer difference could mean several numbers up. However, rounding up or down to the damage number virtually doesn't make a difference at all.

There is an exception to this.
If you're dealing 8x damage, and your damage is supposed to be 15.6 each hit, then it means you deliver +8 more damage.
 

Latest Threads

Latest Profile Posts

Introducing the premium bagel
I have a micro economics midterm tomorrow. I'm not worried.
Should be a...small exam :kaojoy:
While was working on one of the monsters that will appear in my horor game I may have made harold a Lovecraftian monster. (I used his head to keep things aligned.)
Finally got my website fixed and the security sorted so it's been a productive day. Can I go to sleep now? I mean... I don't need to work right?
It's always a good idea to refresh yourself on what your plugins do. I'm working on a map with opening walls, and I was about to tear my hair out over how much image editing and hand-constructing of autotiles I'd have to do, when I looked again and saw @Shaz 's Tile Changer could copy areas from another map.

Forum statistics

Threads
115,923
Messages
1,094,076
Members
151,182
Latest member
ricansuave
Top