Share your advanced PureBasic knowledge/code with the community.
chris319
Enthusiast
Posts: 782 Joined: Mon Oct 24, 2005 1:05 pm
Post
by chris319 » Sun Dec 16, 2007 1:20 am
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
traumatic
PureBasic Expert
Posts: 1661 Joined: Sun Apr 27, 2003 4:41 pm
Location: Germany
Contact:
Post
by traumatic » Sun Dec 16, 2007 1:43 am
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
Good programmers don't comment their code. It was hard to write, should be hard to read.
Michael Vogel
Addict
Posts: 2810 Joined: Thu Feb 09, 2006 11:27 pm
Contact:
Post
by Michael Vogel » Mon Dec 17, 2007 10:35 am
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); !!!
#NULL
Addict
Posts: 1499 Joined: Thu Aug 30, 2007 11:54 pm
Location: right here
Post
by #NULL » Mon Dec 17, 2007 5:25 pm
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
Matt
Enthusiast
Posts: 447 Joined: Sat May 21, 2005 1:08 am
Location: USA
Post
by Matt » Mon Dec 17, 2007 7:24 pm
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
Fred
Administrator
Posts: 18252 Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:
Post
by Fred » Mon Dec 17, 2007 7:30 pm
Yes, it's added for 4.20.
Matt
Enthusiast
Posts: 447 Joined: Sat May 21, 2005 1:08 am
Location: USA
Post
by Matt » Tue Dec 18, 2007 1:44 am
Fred wrote: Yes, it's added for 4.20.
Psychophanta
Always Here
Posts: 5153 Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:
Post
by Psychophanta » Sun Jan 20, 2008 1:11 am
@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
Blue
Addict
Posts: 967 Joined: Fri Oct 06, 2006 4:41 am
Location: Canada
Post
by Blue » Mon Jan 21, 2008 1:28 pm
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.
PB Forums : Proof positive that 2 heads (or more...) are better than one
Michael Vogel
Addict
Posts: 2810 Joined: Thu Feb 09, 2006 11:27 pm
Contact:
Post
by Michael Vogel » Mon Jan 21, 2008 4:52 pm
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.
citystate
Enthusiast
Posts: 638 Joined: Sun Feb 12, 2006 10:06 pm
Post
by citystate » Tue Jan 22, 2008 3:39 am
what's wrong with good old
there is no sig, only zuul (and the following disclaimer)
WARNING: may be talking out of his hat
superadnim
Enthusiast
Posts: 480 Joined: Thu Jul 27, 2006 4:06 am
Post
by superadnim » Tue Jan 22, 2008 11:37 am
nothing is wrong, thats what my macro actually does and what I've been using since forever
Michael Vogel
Addict
Posts: 2810 Joined: Thu Feb 09, 2006 11:27 pm
Contact:
Post
by Michael Vogel » Tue Jan 22, 2008 11:46 am
Of course your codes are all fine
It's just the problem with 0 and 1 - I'm so negative
....
traumatic
PureBasic Expert
Posts: 1661 Joined: Sun Apr 27, 2003 4:41 pm
Location: Germany
Contact:
Post
by traumatic » Tue Jan 22, 2008 2:15 pm
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!
Good programmers don't comment their code. It was hard to write, should be hard to read.