Magnificent Ornk Donald Hebb Posted July 19, 2004 Share Posted July 19, 2004 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. ) Link to comment Share on other sites More sharing options...
Garrulous Glaahk spyderbytes Posted July 19, 2004 Share Posted July 19, 2004 (untested pseudo-code) Code: if (my_parm % 2 > 5) { // div remainder greater than 5 my_parm = (my_parm / 2) + 1; // round it up} Link to comment Share on other sites More sharing options...
Magnificent Ornk Kelandon Posted July 19, 2004 Share Posted July 19, 2004 I believe decimals get rounded down. BoA just cuts off the part after the decimal point. Code: 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! Link to comment Share on other sites More sharing options...
Garrulous Glaahk spyderbytes Posted July 19, 2004 Share Posted July 19, 2004 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: Code: 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. Link to comment Share on other sites More sharing options...
Well-Actually War Trall Fort Posted July 20, 2004 Share Posted July 20, 2004 Uhhh, wouldn't it be easier just to do this: Code: 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. Link to comment Share on other sites More sharing options...
Garrulous Glaahk spyderbytes Posted July 20, 2004 Share Posted July 20, 2004 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: Code: 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. Link to comment Share on other sites More sharing options...
Recommended Posts