Float-Int conversion problem

BlitzMax Forums/BlitzMax Programming/Float-Int conversion problem

JoshK(Posted 2006) [#1]
BlitzMax seems to always round down when converting a float to an integer:

Notify Int(1.9)

This has far-reaching consequences that create a lot of hard-to-find problems. For example, when doing a power-of-two test on textures, my fixed width was 511.99987, which got rounded to 511.

Why has this method of float conversion been chosen?


Dreamora(Posted 2006) [#2]
Because thats how C does and the math stuff is handled by it :-) (GCC has one of the most optimized math code you could use, so it wouldn't be an intelligent idea to work around that just for "niceness" reasons)


The simple fix is Int(val# + 0.5), if you want the old Blitz behavior :-)


TomToad(Posted 2006) [#3]
That's the way most languages work. When converting from Float to Int, the decimal gets dropped regardless of value. Blitz3D is the first programming language that I've used that rounded the numbers. For games, the BMax version is actually prefered. Suppose you have a play area divided into cells where each cell is 32x32 pixels. How can you tell what cell your mouse is pointing to?
CellX = MouseX() / 32
CellY = MouseY() / 32

That would give incorrect results if the numbers were rounded. for example, if MouseX() were to return 15, then MouseX() / 32 would be 0.46875 which would be rounded down to 0, but if MouseX were to return 17, then MouseX()/32 would be 0.53125 which would round up to 1. Except that the mouse would still be pointing at the same cell. The next cell does not start until pixel 32.
If you actually need the decimal rounded instead of just dropped, use Dreamora's suggestion.