int() function

Blitz3D Forums/Blitz3D Beginners Area/int() function

Why on earth does the Int() function round .5 values to the nearest even number?

Int(2.5) = 2
Int(3.5) = 4

(I already found a workaround using the Floor() function, but I'm still wondering what the reason could be for the Int to work that way).

I think this has something to do with it...

Print 1.5+2.5+3.5+4.5+5.5+6.5
Print Floor(1.5)+Floor(2.5)+Floor(3.5)+Floor(4.5)+Floor(5.5)+Floor(6.5)
Print Int(1.5)+Int(2.5)+Int(3.5)+Int(4.5)+Int(5.5)+Int(6.5)
WaitKey()

That's just the way the language was designed. Do you expect it to do something other than what the docs say?


Int: Converts the value to the nearest integer.
Floor: Rounds downward, i.e. in the direction of -Infinity.
Ceil: Rounds upward, i.e. in the direction of Infinity.



The nearest number is always up for .5 or above, down for less than .5.

The proper results for Int() should be
Int(2.5)=3
Int(3.5)=4

The manual explanation should read "Int:Converts the value to the nearest even integer" to accurately describe what Blitz implementation of the function does.

I think it rounds up and down alternatively to decrease floating point error.

It shouldn't though, not according to both the Manual and the traditional implementation of Int() in countless other languages!


It shouldn't though



Why not? Operators have different precedence to their real-world counterparts too; unlike C, calculations return the faster type unless you specify otherwise... not all computational standards are necessarily equal. Blitz is mostly used for games, not scientific simulations - keeping floating point error in check in this manner is totally acceptable for a game-oriented language. There's nothing to stop you rolling your own function to behave in the traditional manner if that's what you want.

It shouldn't though, not according to both the Manual
Um, I don't know what you're smoking, but the manual does state that it works that way:
Int( value )
Parameters:
value = a number, or a string which represents a number
Description:
Converts the value to the nearest integer. 

This is the same as Blitz's automatic type conversion. 
So the two commands... 

n = value 
n = Int( value ) 

... do exactly the same thing when n is an integer variable. 

If Int is applied to a string it converts as much as possible: 

Int( "10" ) ........ result is 10 
Int( "3.7" ) ....... result is 3, stops at "." which can't be part of an integer 
Int( "junk3" ) .... result is 0, stops at "j" 

Int converts floating point numbers by rounding to the nearest integer. 
NOTE: This is not the traditional meaning of Int in Basic. 

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 

See also Floor and Ceil for other types of rounding.