Page 2 of 2

Posted: Sun Mar 19, 2006 5:22 pm
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

Posted: Sun Mar 19, 2006 9:54 pm
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:

Posted: Sun Mar 19, 2006 10:11 pm
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

Posted: Sun Mar 19, 2006 10:20 pm
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!

Posted: Mon Mar 20, 2006 9:08 am
by Michael Vogel
Psychophanta wrote: In this we are absolutely inacordance.
inacordance? sounds so negative :?, hopefully you meant in accordance :lol:

Posted: Mon Mar 20, 2006 9:24 am
by Psychophanta
Sorry, corrected :)

Posted: Mon Mar 20, 2006 9:50 am
by Fangbeast
I'm glad he didn't say "inanaccordion"!!!

Posted: Tue Mar 21, 2006 9:03 am
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