Round() Int() ...

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Round() Int() ...

Post by Psychophanta »

Currently, there are no a native function to perform a rounded number from another.

Now:
1) Round(x,0) is absolutely samething as Int(x)
2) Round() function, doesn't round (i mean what calculators do)

I suggest to modify Round() function to:
Result.f = Round(Number.f, [#Decimals])

So
Round(5.56366) would result in 6
Round(5.5) would result in 6
Round(5.4999999) would result in 5
Round(5.56366,3) would result in 5.564
...and if it could be:
Round(5.56366,7) would result in 0.0556366
...
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Post by Kale »

What about also adding a flag for rounding up and down?

Round(5.5, #PB_Round_Up) would result in 6
Round(5.5, #PB_Round_Down) would result in 5
--Kale

Image
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

Kale wrote:What about also adding a flag for rounding up and down?

Round(5.5, #PB_Round_Up) would result in 6
Round(5.5, #PB_Round_Down) would result in 5
:shock: Are you joking !?

That's how it works now. :!:

1) the second line is ridiculous because is the SAME as Int()
2) That's not to round a number, and that's useless, because Round(5.5, #PB_Round_Up) is SAME as Round(5.5+1, #PB_Round_Down) and SAME as Int(5.5+1)
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
Froggerprogger
Enthusiast
Enthusiast
Posts: 423
Joined: Fri Apr 25, 2003 5:22 pm
Contact:

Post by Froggerprogger »

There's a difference between Round and Int:
Round returns a float, Int returns an int.

But I'm waiting for a simple 'normal' round (= "Round Half Up"), too.
atm we can use:

Code: Select all

Procedure.f RoundHalfUp(x.f)
  If x >= 0.0
    ProcedureReturn Round(x + 0.5, 0)
  Else
    ProcedureReturn Round(x - 0.5, 1)
  EndIf
EndProcedure

A further optional parameter for the number of decimals would be pretty cool!
Round (number.f, roundingMode.l, numberDecimals.l)
roundingMode: #Round_Up, #Round_Down, #Round_HalfUp
%1>>1+1*1/1-1!1|1&1<<$1=1
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

Froggerprogger wrote:A further optional parameter for the number of decimals would be pretty cool!
At the moment i use this :wink:

Code: Select all

Procedure.f RoundHalfUp(x.f,d.l)
  x*Pow(10,d)
  If x >= 0.0 
    ProcedureReturn Round(x + 0.5, 0)/Pow(10,d)
  Else 
    ProcedureReturn Round(x - 0.5, 1)/Pow(10,d)
  EndIf 
EndProcedure
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Post by Kale »

Froggerprogger wrote:There's a difference between Round and Int:
Round returns a float, Int returns an int.

But I'm waiting for a simple 'normal' round (= "Round Half Up"), too.
atm we can use:

Code: Select all

Procedure.f RoundHalfUp(x.f)
  If x >= 0.0
    ProcedureReturn Round(x + 0.5, 0)
  Else
    ProcedureReturn Round(x - 0.5, 1)
  EndIf
EndProcedure

A further optional parameter for the number of decimals would be pretty cool!
Round (number.f, roundingMode.l, numberDecimals.l)
roundingMode: #Round_Up, #Round_Down, #Round_HalfUp
thats what i meant. :wink:
--Kale

Image
Froggerprogger
Enthusiast
Enthusiast
Posts: 423
Joined: Fri Apr 25, 2003 5:22 pm
Contact:

Post by Froggerprogger »

That's what I knew that you meant.

I don't know, if you know, that I know, what you know, you know ? So what do you know now ? That I don't know what you know !

:wink:
%1>>1+1*1/1-1!1|1&1<<$1=1
User avatar
Rescator
Addict
Addict
Posts: 1769
Joined: Sat Feb 19, 2005 5:05 pm
Location: Norway

Post by Rescator »

It definetly would be nice if Round() default behaviour was similar to this!

(yeah yeah, I'm pretty sure my ASM is very wrong, but the example works :)

Code: Select all

Procedure.f froundint(num.f)
Protected result.l
!FLD dword[p.v_num]
!FRNDINT
!FISTP dword[p.v_result] 
ProcedureReturn result
EndProcedure

Debug fround(1.4)
Please note, .0 to .499999something is rounded down,
.5 to .9999999something is rounded up.
DarkDragon
Addict
Addict
Posts: 2344
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Post by DarkDragon »

Standard-Round-Arithmetic:

Int(Value+0.5)

Here's my code:

Code: Select all

Procedure.f Round2(Value.f, Deci.l)
  If Value < 0 : Value * -1 : b = 1 : Else : b = 0 : EndIf
  a.l = Pow(10, Deci)
  Result.f = Int((Value*a)+0.5)/a
  If b = 1 : Result * -1 : EndIf
  ProcedureReturn Result
EndProcedure

Debug Round2( 3.500000, 0)
Debug Round2( 3.499999, 2)
Debug Round2(-3.500000, 0)
Debug Round2(-3.499999, 2)
bye,
Daniel
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Post by ts-soft »

@DarkDragon
what is abou this:

Code: Select all

Debug Round2( 3.499999, 1)
gives 3.5 and not 3.0
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
DarkDragon
Addict
Addict
Posts: 2344
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Post by DarkDragon »

ts-soft wrote:@DarkDragon
what is abou this:

Code: Select all

Debug Round2( 3.499999, 1)
gives 3.5 and not 3.0
second parameter is decimals count

Zweiter parameter bestimmt auf wieviele stellen gerundet wird.
bye,
Daniel
thefool
Always Here
Always Here
Posts: 5875
Joined: Sat Aug 30, 2003 5:58 pm
Location: Denmark

Post by thefool »

Yes, darkdragon i think he knows that. But if you were to round 3.4 what would it give?
DarkDragon
Addict
Addict
Posts: 2344
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Post by DarkDragon »

thefool wrote:Yes, darkdragon i think he knows that. But if you were to round 3.4 what would it give?
No he didn't understand it.
Works, too:

Code: Select all

Debug Round2( 3.444444, 1)
The topic is about Round() makes no sense as it is in PB, because you can specify how to round(up or down).
bye,
Daniel
thefool
Always Here
Always Here
Posts: 5875
Joined: Sat Aug 30, 2003 5:58 pm
Location: Denmark

Post by thefool »

DarkDragon wrote: The topic is about Round() makes no sense as it is in PB, because you can specify how to round(up or down).
Yes it should have a default mode, where it followed the normal behaviour (eh 3.45=3.5 and so on)..
DarkDragon
Addict
Addict
Posts: 2344
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Post by DarkDragon »

thefool wrote:
DarkDragon wrote: The topic is about Round() makes no sense as it is in PB, because you can specify how to round(up or down).
Yes it should have a default mode, where it followed the normal behaviour (eh 3.45=3.5 and so on)..
Yes like my procedure does ;) . So 3.4999 will never get 3.4 :P that would be senseless.
bye,
Daniel
Post Reply