Page 1 of 1
Rounding to the Nearest Integer
Posted: Sun Dec 16, 2007 1:20 am
by chris319
Here is a little routine for rounding off numbers to the nearest integer:
Code: Select all
Procedure Roundoff(number.f)
integer.f = Int(number)
frac.f = number - Int(number)
If number >= 0
If frac >= 0.5
rounded.f = integer + 1
Else
rounded = integer
EndIf
Else
If frac <= -0.5
rounded = integer - 1
Else
rounded = integer
EndIf
EndIf
ProcedureReturn rounded
EndProcedure
Re: Rounding to the Nearest Integer
Posted: Sun Dec 16, 2007 1:43 am
by traumatic
Same result but should be faster:
Code: Select all
Procedure.l Roundoff(number.f)
If number - Int(number) >= 0.5
ProcedureReturn number + 0.1
EndIf
ProcedureReturn number
EndProcedure
Re: Rounding to the Nearest Integer
Posted: Mon Dec 17, 2007 10:35 am
by Michael Vogel
Hmm...
I would do it like this :
Code: Select all
Procedure.l Roundoff(number.f)
If number<0
ProcedureReturn Int(number-0.5)
Else
ProcedureReturn Int(number+0.5)
EndIf
EndProcedure
Debug Roundoff(3.1)
Debug Roundoff(3.8)
Debug Roundoff(-3.1); !!!
Posted: Mon Dec 17, 2007 5:25 pm
by #NULL
and as macro
Code: Select all
Macro Roundoff(_number_)
Int((_number_) - (0 Or (_number_)<0) + 0.5)
EndMacro
Debug Roundoff( 3.2) ; 3
Debug Roundoff( 3.8) ; 4
Debug Roundoff(-3.2) ; -3
Debug Roundoff(-3.8) ; -4
Debug Roundoff( 3) ; 3
Debug Roundoff(-3) ; -3
btw:
if i change this
Code: Select all
Int((_number_) - (0 Or (_number_)<0) + 0.5)
to this
Code: Select all
Int((_number_) + 0.5 - (0 Or (_number_)<0))
i get wrong results. why

Posted: Mon Dec 17, 2007 7:24 pm
by Matt
Shouldn't this function be built-in?
I see Round in the manual but it takes an argument to round up or down, there should be one to round up if >=.5 or down otherwise
Posted: Mon Dec 17, 2007 7:30 pm
by Fred
Yes, it's added for 4.20.
Posted: Tue Dec 18, 2007 1:44 am
by Matt
Fred wrote:Yes, it's added for 4.20.

Posted: Sun Jan 20, 2008 1:11 am
by Psychophanta
@Michael Vogel & @Traumatic & @chris319 & @#NULL:
Code: Select all
Procedure.l Roundoff(number.f)
ProcedureReturn number
EndProcedure
Debug Roundoff(3.1)
Debug Roundoff(3.8)
Debug Roundoff(-3.1); !!!
Simple

Posted: Mon Jan 21, 2008 1:28 pm
by Blue
Psychophanta wrote:Code: Select all
Procedure.l Roundoff(number.f)
ProcedureReturn number
EndProcedure
Simple

Bravo, Psychophanta. Very well done.
As you say, so simple...
Yet, I'd never have thought of it all by my lonesome self.
Posted: Mon Jan 21, 2008 4:52 pm
by Michael Vogel
One point seems to be still a problem, the lack of precision with binary periodic numbers...
Code: Select all
a.f=1.250000; b.d=1.250000
c=a*2
Debug c
c=1.4999*2
Debug c
...the same issue is seen also within the Roundoff macro, of course.
Posted: Tue Jan 22, 2008 3:39 am
by citystate
what's wrong with good old
Posted: Tue Jan 22, 2008 11:37 am
by superadnim
nothing is wrong, thats what my macro actually does and what I've been using since forever

Posted: Tue Jan 22, 2008 11:46 am
by Michael Vogel
Of course your codes are all fine
It's just the problem with 0 and 1 - I'm so negative

....
Posted: Tue Jan 22, 2008 2:15 pm
by traumatic
Psychophanta wrote:@Michael Vogel & @Traumatic & @chris319 & @#NULL:
Code: Select all
Procedure.l Roundoff(number.f)
ProcedureReturn number
EndProcedure
Debug Roundoff(3.1)
Debug Roundoff(3.8)
Debug Roundoff(-3.1); !!!
Simple

hehe, good one!