Rounding Numbers / Damage Dealing

Error message

Deprecated function: implode(): Passing glue string after array is deprecated. Swap the parameters in drupal_get_feeds() (line 394 of /var/www/pied-piper.ermarian.net/includes/common.inc).
AuthorTopic: Rounding Numbers / Damage Dealing
BANNED
Member # 4
Profile Homepage #0
How does one round numbers? For instance, a value divided by two. And if that's impossible, can damage be dealt in increments of .5?

(EDIT again: Hah! I found a use for this topic after all. ^_^)

[ Monday, July 19, 2004 10:55: Message edited by: Sathoj ]

--------------------
*
Posts: 6936 | Registered: Tuesday, September 18 2001 07:00
Shock Trooper
Member # 4180
Profile #1
(untested pseudo-code)
if (my_parm % 2 > 5) { // div remainder greater than 5
my_parm = (my_parm / 2) + 1; // round it up
}


--------------------
-spyderbytes
Posts: 200 | Registered: Wednesday, March 31 2004 08:00
Off With Their Heads
Member # 4045
Profile Homepage #2
I believe decimals get rounded down. BoA just cuts off the part after the decimal point.

print_num(25/6);
print_num(29/6);
if (25/6 == 29/6)
print_str("It's true!");
I think I tried this once and it printed

Debug value: 4
Debug value: 4
It's true!

--------------------
Arancaytar: Every time you ask people to compare TM and Kel, you endanger the poor, fluffy kittens.
Smoo: Get ready to face the walls!
Ephesos: In conclusion, yarr.

Kelandon's Pink and Pretty Page!!: the authorized location for all things by me
The Archive of all released BoE scenarios ever
Posts: 7968 | Registered: Saturday, February 28 2004 08:00
Shock Trooper
Member # 4180
Profile #3
quote:
Originally written by Just call me Kel:

I believe decimals get rounded down. BoA just cuts off the part after the decimal point.
Right. That's why if the remainder (x modulo y returns the remainder from integer division) is more than 5 (which means more than .5), you can just do the integer div and add 1 to "round up". :)

EDIT: That's what I get for posting before my "morning" coffee (I keep odd hours :) ). The pseudo-code should be:

if ((x % y) >= (y / 2))
x = (x / y) + 1;
else
x = x / y;
Obviously, the remainder won't always be greater than '5', but instead half or more of the divisor. :D

[ Monday, July 19, 2004 12:28: Message edited by: spyderbytes ]

--------------------
-spyderbytes
Posts: 200 | Registered: Wednesday, March 31 2004 08:00
Agent
Member # 2820
Profile #4
Uhhh, wouldn't it be easier just to do this:

y = y * 10;
if ((y / 2) % 10 == 5)
[odd]
else
[even]
EDIT: I believe this is the same behavior as C and Java, isn't it? When you declare int variables and divide, it just chops off the stuff after the decimal point.

I think this because I vaguely remember doing 5/2=2 in Java.

[ Tuesday, July 20, 2004 07:04: Message edited by: Keep ]

--------------------
Thuryl: I mean, most of us don't go around consuming our own bodily fluids, no matter how delicious they are.
====
Alorael: War and violence would end if we all had each other's babies!
====
Drakefyre: Those are hideous mangos.
Posts: 1415 | Registered: Thursday, March 27 2003 08:00
Shock Trooper
Member # 4180
Profile #5
Not sure why that would be easier. :)

The pseudo-code I came up with once I finally pulled my head out of the dark cavity it was in has the advantage of being able to round up regardless of the divisor (and yes, the original post did mention "like dividing by 2", but bulletproof is always better :) ).

Also, seems to me that if all you're trying to determine is what's even and what's odd, it would be easier to just:

if ((x % 2) == 0)
...I'm even...
else
...I'm odd...
Guess I just missed the point of multiplying by 10 then dividing by 2 and checking for a 5 remainder modulo 10... but it's pre-coffee time for me again. :D

--------------------
-spyderbytes
Posts: 200 | Registered: Wednesday, March 31 2004 08:00