Rounding
BlitzMax Forums/BlitzMax Beginners Area/Rounding
| ||
OK, now I know the topic of roundnig has cropped up a few times but I could not find the answer to my question! Fom what I can tell, max has 3 rounding functions: Int - removes decimal part Floor - rounds down (same as Int?) Ceil - rounds up That's all well and good, but is there a function that rounds down if less than .5, and up if .5 or greater? In previous incarnations of Blitz, the Int function worked this way. Is there an equivilent function for this in Max? I've never used a language before that couldn't do this! |
| ||
That's a bit weird not having a round function at all. Anyway, note that Int in Blitz 3D doesn't actually do what you describe either. From the docs: What about numbers exactly halfway between integers? The rounding is to the nearest even integer: Int( 2.5 ) ... produces 2 Int( 3.5 ) ... produces 4 This seems to be some kind of standard. The .Net Math.Round function does the same and describes it as: The behavior of this method follows IEEE Standard 754, section 4. This kind of rounding is sometimes called rounding to nearest, or banker's rounding. Anyway, short story is that it looks like you'll have to write something yourself. Edit: Suddently I feel really geeky for knowing this - I only know about it 'cos I had to write my own rounding function to do what you describe for a .Net app I wrote recently - honest! :-) |
| ||
Int (2.9) gives 2? What's wrong with rounding to nearest? |
| ||
well... for the lazy here is some code that'll do itFunction Round(number:Double) dPart:Double = number-Int(number) If dPart>=0.5 Return Int(number)+1 Else Return Int(number) EndIf End Function |
| ||
Most programming languages I've worked with just drop the fractional part when converting a Float to Int. BlitzBasic is the first I've used that doesn't work that way. If you want to round to the nearest number, just add .5NumberInt = Int(NumberFloat + .5) All numbers below x.5 will return x and all numkbers equal to or greater than x.5 will return x+1 |
| ||
ooh nice! i hadnt thought of that :P |
| ||
What I meant to say was that other langauges I've used either had an Int function that behaved like older Blitz versions, or they had other functions that provided that functionality. TomToad, nice solution, I didn't think of that, thanks :-) |
| ||
If you want to round to the nearest number, just add .5 Unfortunately that doesn't work for negative numbers. -2.6 should round to -3, but -2.6 + 0.5 = -2.1, and Int( -2.1 ) = -2. |
| ||
Function Round:Double(x:Double) Return Int(x + (Sgn(x) * 0.5)) End Function |
| ||
Nice. |
| ||
d:Double = 123.456 Print Round(d, 1) 'Rounds d to nearest whole number Print Round(d, .01) 'Rounds d to nearest hundreth Print Round(d, 10) 'Rounds d to nearest ten Print Round(d, 25) 'Rounds d to nearest twentiy five Function Round:Double(value:Double, place:Double) Local d:Double = value / place Return Int(d + Sgn(d) * 0.5) * place EndFunction Rounds a number to the nearest of any number. I figured this out when I needed a similar thing on my TI-83+ graphing calculator. I had a math problem that wanted me to find some length to the nearest 25 units. So I figured this out. It uses TwoEyedPete's round function to do the actual rounding in place of the calculator's function. Only problem is encountered with the second one, I think it is just messiness with floating point arithmetic. |