Round() Int() ...
- Psychophanta
- Always Here
- Posts: 5153
- Joined: Wed Jun 11, 2003 9:33 pm
- Location: Anare
- Contact:
Round() Int() ...
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
...
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
...
- Psychophanta
- Always Here
- Posts: 5153
- Joined: Wed Jun 11, 2003 9:33 pm
- Location: Anare
- Contact:
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

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)
-
- Enthusiast
- Posts: 423
- Joined: Fri Apr 25, 2003 5:22 pm
- Contact:
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:
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
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
- Psychophanta
- Always Here
- Posts: 5153
- Joined: Wed Jun 11, 2003 9:33 pm
- Location: Anare
- Contact:
At the moment i use thisFroggerprogger wrote:A further optional parameter for the number of decimals would be pretty cool!

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
thats what i meant.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

-
- Enthusiast
- Posts: 423
- Joined: Fri Apr 25, 2003 5:22 pm
- Contact:
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
Please note, .0 to .499999something is rounded down,
.5 to .9999999something is rounded up.
(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)
.5 to .9999999something is rounded up.
-
- Addict
- Posts: 2344
- Joined: Mon Jun 02, 2003 9:16 am
- Location: Germany
- Contact:
Standard-Round-Arithmetic:
Int(Value+0.5)
Here's my code:
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
Daniel
@DarkDragon
what is abou this:
gives 3.5 and not 3.0
what is abou this:
Code: Select all
Debug Round2( 3.499999, 1)
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.

Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.

-
- Addict
- Posts: 2344
- Joined: Mon Jun 02, 2003 9:16 am
- Location: Germany
- Contact:
second parameter is decimals countts-soft wrote:@DarkDragon
what is abou this:gives 3.5 and not 3.0Code: Select all
Debug Round2( 3.499999, 1)
Zweiter parameter bestimmt auf wieviele stellen gerundet wird.
bye,
Daniel
Daniel
-
- Addict
- Posts: 2344
- Joined: Mon Jun 02, 2003 9:16 am
- Location: Germany
- Contact:
No he didn't understand it.thefool wrote:Yes, darkdragon i think he knows that. But if you were to round 3.4 what would it give?
Works, too:
Code: Select all
Debug Round2( 3.444444, 1)
bye,
Daniel
Daniel
-
- Addict
- Posts: 2344
- Joined: Mon Jun 02, 2003 9:16 am
- Location: Germany
- Contact:
Yes like my procedure doesthefool wrote:Yes it should have a default mode, where it followed the normal behaviour (eh 3.45=3.5 and so on)..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).


bye,
Daniel
Daniel