Use Floats

Everything else that doesn't fall into one of the other PB categories.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by geoff.

History
25th March 2003 : Version 3.62
- Fixed: Val(): now accepts any BASIC valid forms

And on testing I see Fred has also fixed ValF() Thanks fred.

a$="-1.234e-19rubbish"
a.f=ValF(a$)

This now gives the right answer.

If you want to display large and small numbers like this, you could use my version of StrF() below. If anyone finds a bug in this routine, please post a reply.

Code: Select all

Procedure.s zstrf(a.f,n.l)
; Convert float to string with n digits precision
  a$=""
  If ab:ProcedureReturn("O/F"):EndIf
  b=1/b
  If a8:n=8:EndIf;max float accuracy
  b=1:For i.l=1 To n:b=b*10:Next i
  e.l=0
  While a>=b:a/10:e+1:Wend
  b=b/10
  While a=0 And e-n And e1:a$=a$+".":EndIf
    a$+Right(Str(i),n-1)
    i=e+n-1
    If i<0
      a$+"E-"+RSet(Str(-i),2,"0")
    Else
      a$+"E+"+RSet(Str(i),2,"0")
    EndIf
  EndIf
ProcedureReturn a$
EndProcedure
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Franco.
Originally posted by geoff
...If anyone finds a bug in this routine, please post a reply.
Done:

Code: Select all

a.f=-1.234567
a$= zstrf(a,6)
Debug a$
a$= StrF(a,6)
Debug a$
Sorry Geoff, but your zstrf gives the wrong result...

Have a nice day...

Franco
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by geoff.

Thanks for the reply Franco.
But I get the correct result

-1.23457
-1.234567

zstrf() is accurate to 6 significant figures.
StrF() is accurate to 6 decimal places.

What result did you get?
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by tinman.

I also get the correct results.

--
I used to be a nihilist but I don't believe in that any more.
(Win98first ed. + all updates, PB3.62, external editor)
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by PB.

> I also get the correct results.

Same.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Franco.
Originally posted by geoff


zstrf() is accurate to 6 significant figures.
StrF() is accurate to 6 decimal places.

What result did you get?
I got the same. Thought that:
; Convert float to string with n digits precision
means that if I declare 6 digits it's ment as decimals.
If not than sorry - I've misunderstood this comment.

On the other hand, this means that this:

Code: Select all

a.f=-12345.678912
a$= zstrf(a,6)
would result in -12345.7 (only one decimal).
For what is this for?

Have a nice day...

Franco
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by geoff.

Franco,
Significant figures makes more sense than number of decimals when you know the accuracy of your calculation but don't know the size of the result.

For example:

Code: Select all

0.00123     needs 5 dec1230000     needs no dec
1.23E+21    needs no dec
1.23E-09    needs 11 dec
But if, for example, you know your answer is between 1.00 and 5.00 then you would be better to use StrF() and specify 2 decimals.
Be careful though, a number like 2.1099 would be printed as 2.10 by StrF().
Post Reply