Int(entional) ?

BlitzMax Forums/BlitzMax Module Tweaks/Int(entional) ?

fredborg(Posted 2006) [#1]
Local entional:Float = 0.51
Print Int(entional) '?
Shouldn't Int() round a float to the nearest whole number?

And if it shouldn't maybe a Round() function would come in handy...


Dreamora(Posted 2006) [#2]
No it just cuts of everything after the . (only the old blitz are behaving against the common behavior)

For having the old Blitz behavior, you can just do:

Print Int(entional + 0.5) which is the same as a rounding function :-)


fredborg(Posted 2006) [#3]
Except if entional is negative :) It's not a big issue, but a native Round function would be handy.


Floyd(Posted 2006) [#4]
BlitzMax really should have a Round() function.

The +0.5 trick doesn't work for negative numbers.


Yan(Posted 2006) [#5]
Function Round(value!)
  Return value! + (0.5 * Sgn(value!))
End Function

...Or...

Adding...
Rem
bbdoc: Nearest integral value to @x
End Rem
Function Round:Double( x:Double )="bbRound"
End Extern
...to 'mod/brl/math/math.bmx'

...and...
double bbRound( double x ){
	return nearbyint( x );  //Should this be rint()?
}
...to 'mod/brl/math/math.c'.

Seems to work (returns a double to stay consistent with floor() and ceil()).


fredborg(Posted 2006) [#6]
Now, it just needs to be added to the official release :)

As far as I understand rint() will work as either floor or ceil depending on the rounding mode used, so it doesn't round to the nearest integral value.