Rounding to the Nearest Integer

Share your advanced PureBasic knowledge/code with the community.
chris319
Enthusiast
Enthusiast
Posts: 782
Joined: Mon Oct 24, 2005 1:05 pm

Rounding to the Nearest Integer

Post 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
traumatic
PureBasic Expert
PureBasic Expert
Posts: 1661
Joined: Sun Apr 27, 2003 4:41 pm
Location: Germany
Contact:

Re: Rounding to the Nearest Integer

Post 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 
Good programmers don't comment their code. It was hard to write, should be hard to read.
User avatar
Michael Vogel
Addict
Addict
Posts: 2810
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: Rounding to the Nearest Integer

Post 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); !!!
#NULL
Addict
Addict
Posts: 1499
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Post 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 :?:
Matt
Enthusiast
Enthusiast
Posts: 447
Joined: Sat May 21, 2005 1:08 am
Location: USA

Post 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
Fred
Administrator
Administrator
Posts: 18252
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

Yes, it's added for 4.20.
Matt
Enthusiast
Enthusiast
Posts: 447
Joined: Sat May 21, 2005 1:08 am
Location: USA

Post by Matt »

Fred wrote:Yes, it's added for 4.20.
:)
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post 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 :wink:
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
User avatar
Blue
Addict
Addict
Posts: 967
Joined: Fri Oct 06, 2006 4:41 am
Location: Canada

Post by Blue »

Psychophanta wrote:

Code: Select all

Procedure.l Roundoff(number.f)
  ProcedureReturn number
EndProcedure
Simple :wink:
:shock: :shock: :shock:
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 :idea:
User avatar
Michael Vogel
Addict
Addict
Posts: 2810
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Post 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.
citystate
Enthusiast
Enthusiast
Posts: 638
Joined: Sun Feb 12, 2006 10:06 pm

Post by citystate »

what's wrong with good old

Code: Select all

Int(a.f + 0.5)
there is no sig, only zuul (and the following disclaimer)

WARNING: may be talking out of his hat
superadnim
Enthusiast
Enthusiast
Posts: 480
Joined: Thu Jul 27, 2006 4:06 am

Post by superadnim »

nothing is wrong, thats what my macro actually does and what I've been using since forever :lol:
User avatar
Michael Vogel
Addict
Addict
Posts: 2810
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Post by Michael Vogel »

Of course your codes are all fine :)

It's just the problem with 0 and 1 - I'm so negative :oops: ....

Code: Select all

Debug Roundoff (-1.25*2)
traumatic
PureBasic Expert
PureBasic Expert
Posts: 1661
Joined: Sun Apr 27, 2003 4:41 pm
Location: Germany
Contact:

Post 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 :wink:
hehe, good one!
Good programmers don't comment their code. It was hard to write, should be hard to read.
Post Reply