Round() Int() ...

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
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 »

when i round 1 number from point gives 3.4999999 = 3
when round 2 number from point gives 3.4999999 = 3.5
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
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

Rescator way is definitely the best way to do, i. e. ASM.
But it should be done like this:

Code: Select all

Procedure.f froundint(num.f) 
!fld dword[p.v_num] 
!frndint
ProcedureReturn
EndProcedure 

Debug froundint(1.4)
:wink:
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
User avatar
Michael Vogel
Addict
Addict
Posts: 2797
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Post by Michael Vogel »

Some comments about the "rounding" of PureBasic...

I think, PureBasic has three problems with math:

1) it is unprecise
2) it is inconsistent
3) it is uncomplete

To me more precise, I'll have to give some addition explanations...

ad 1) even new variable type of double can bring surprising results...

Code: Select all

j.d=0
	For i = 1 To 7
	  j=i
	  n=j/2.0
	  Debug n
	Next i
of course, there's a workaround with int (or "round"), but programmers wish to
get expected and predictable results -- what kind of output did YOU expect?

To be honest, PureBasics arithmetic seems to be as good as it can be done with double vars
- the implementation seems to be done very well, but the conversion between types and the
representation of numbers are not so perfect...

ad 2) round and round and round...
why the "Str" function is doing real rounding and the "round" function not? (it's like using
a hide command to show a window or to use disable to... :? )

Code: Select all

; Str is the real Round function...
  Debug StrD(#PI,3)
  Debug StrD(#PI*10000,0)
  Debug Round(#PI*10000,0)
	
; other effects...
  Debug 1e50; also nice: the 1 in the middle and the .0 at the end
  Debug StrD(1e160*1e160); two more jokes, the "unlimited" range of the help text and some zeros at the end
* if no parameter is given to StrD, the resulting string should have a "dynamical" number
of ciphers behind the floating point to get rid of annoying things like that...
(Bin and Hex should work the same way, just with it's leading zeros)

Code: Select all

; Original
  a.d=5.6
  Debug a
  Debug StrD(a)

Code: Select all

; Alternative
  #maxstellen=10

  Procedure.s strx(d.d,stellen.w=0)
     Protected tmp.s
     Protected k.w,n.w
     If stellen=0
        tmp=StrD(d,#maxstellen)
        k=Len(tmp)
        n=#maxstellen+2; #nks+1 läßt Komma stehen...
        While n>1
            k-1
            n-1
            If PeekB(@tmp+k-1)>48; 1,2,...,9 und '.'
              n=0
            EndIf
       Wend
       ProcedureReturn Left(tmp,k)
    Else
       ProcedureReturn StrD(d,stellen)
    EndIf
 EndProcedure

 a.d=5.5
 Debug strx(a)
 Debug strx(a,5)

* of course, the round command should be changed - no parameter, real rounding. If anyone needs
something else, "Int()" can be used (and for floating point results, Floor(), Ceil() and Frac() could do
the rest...)

ad 3) I really miss some things to speed up integer math... (some of them will come for sure, the help
file told me)

- Abs
- Sgn
- Add, Sub, Div, Mul, Scale
- Succ, Pred
- IMin, IMax
- Odd, Even
- Rol, Ror, Shl, Shr
- BitTest, BitSet, BitClr
- HiByte, LoByte
- HiWord, LoWord
- Swap

...and standard algebra functions...

- SinH, ArcSin, ArcSinH, CosH,..., ArcTanH
- SinQ, CosQ
- Degree, Radiant
- Exp
- Fact
- Floor, Ceil, Frac
- Min, Max
- Mod

...and (sometimes) just the possibility to change the result type for my own...

- BYTE()
- WORD()
- LONG()
- FLOAT()
- DOUBLE()


Please, don't think, I don't like PureBasic - I already wrote some tools (AutoIndent, an USB-Backup-Program, a Filechanger to get zip and exe files through E-Mail filters, a clever ping tool, a tool to change statistical values, etc.) - not so bad for about 5 weeks with PureBasic (that's also a compliment for the [creators of the] compiler, the IDE, the help file and last but not least - this forum)... so I do like PureBasic and therefore I hope it will still improve!

Michael
Last edited by Michael Vogel on Mon Mar 20, 2006 9:17 am, edited 5 times in total.
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 wrote:of course, the round command should be changed.
In this we are absolutely in accordance.
And in fact that is the reason of being of this thread.
Hope the PB team think about round means round, and current round() function doesn't round a number.
Intel frndint command do it!
Last edited by Psychophanta on Mon Mar 20, 2006 9:24 am, edited 2 times in total.
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
User avatar
Michael Vogel
Addict
Addict
Posts: 2797
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Post by Michael Vogel »

Psychophanta wrote: In this we are absolutely inacordance.
inacordance? sounds so negative :?, hopefully you meant in accordance :lol:
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

Sorry, corrected :)
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4789
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Post by Fangbeast »

I'm glad he didn't say "inanaccordion"!!!
Amateur Radio/VK3HAF, (D-STAR/DMR and more), Arduino, ESP32, Coding, Crochet
User avatar
Michael Vogel
Addict
Addict
Posts: 2797
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Post by Michael Vogel »

j.d=0
For i = 1 To 7
j=i
n=j/2.0
Debug n
Next i
Does everyone feel comfortable with the results of Pure Basic ( 0 / 1 / 2 / 2 / 2 / 3 / 4) ?

Others Basic output would be 0 / 1 / 1 / 2 / 2 / 3 / 3...

Code: Select all

DEFDBL "j"
DEFINT "i,n"
j = 0
FOR i = 1 TO 7
  j = i
  n = j / 2.0
  PRINT n
NEXT i
Post Reply