Better Int()?

BlitzPlus Forums/BlitzPlus Programming/Better Int()?

Grey Alien(Posted 2005) [#1]
OK Int rounds numbers that are exactly halfway e.g. 0.5, 1.5 etc towards nearest even number e.g. 0.5 = 0 but 1.5 = 2. I think this is called bankers rounding (for a more fair rounding of money), but it stinks for me! Anyone know a quick way to always round 0.5 down? e.g 1.5 = 1 but 1.6 = 2


Snarty(Posted 2005) [#2]
I'm not sure of any "Quick way" but this is what I came up with, adjust as needed.

Function MyRounding%(Value#)

	Places#=(Value-Floor(Value))
	If Places>0.5
		Return Ceil(Value)
	Else
		Return Floor(Value)
	EndIf
	
End Function



Beaker(Posted 2005) [#3]
Function Round(flot#)
	Return Floor(flot+0.5)
End Function



Snarty(Posted 2005) [#4]
lol, nice one. Just me over doing it as usual.


Arem(Posted 2005) [#5]
Owned.


Grey Alien(Posted 2005) [#6]
Nice one guys! I was just too tired to work it out and it was causing me a massive bug in a level editor.


Russell(Posted 2005) [#7]
Too bad this function doesn't just return the integer part, like most other basic's do....

Russell


Grey Alien(Posted 2005) [#8]
Russell: luckily Floor does that.


Russell(Posted 2005) [#9]
Not by itself. Floor just rounds 'down' toward negative infinity. Floor(-1.7) returns -2.0 for example.

Russell


Russell(Posted 2005) [#10]
Under other basics, Int(-1.7) would return -1.

For the fractional portion, you can use Frac(-1.7), if available or just subtract the Int() portion from the original. Of course, that can be a bit sketchy given the nature of floats (-1.7 could be defined internally as -1.700099 or similar).

Beaker's example does help, though.

Russell


Grey Alien(Posted 2005) [#11]
Russell: I knew that, glad u did too!


WolRon(Posted 2005) [#12]
Too bad this function doesn't just return the integer part, like most other basic's do....

Function ClassicInt(flt#)
  If flt#< 0
    Return Ceil(flt#)
  Else
    Return Floor(flt#)
  EndIf
End Function