Floats -- an interim solution

Just starting out? Need help? Post your questions and find answers here.
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.

Several members of this forum have voiced the opinion that floating point variables are not much use on the current version of Purebasic. To some extent this is true, indeed Purebasic will continue to be of limited serious use until doubles are added in version 4.0

However, the function StrF() has led some members to believe that current floating point variables cannot handle small and large numbers. They can. Numbers in the range 1e-38 to 1e38 can be used with a precision of about 6 decimal digits.

The routine StrG() below can be used instead of the Purebasic routine StrF() to make this range and accuracy available.

Hopefully, when Fred adds double precision floats in version 4.0 he will also add a float to string routine similar to the one below but capable of providing 15 decimal digit accuracy over a much wider number range.

Code: Select all

Procedure.s StrG(a.f,n.l)
; Convert float to string with n digits precision
  a$=""
  If ab:ProcedureReturn("O/F"):EndIf
  b=1/b
  If a7:n=7: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
;
;
;
;Test StrG() over float number range
x.f=1.23456789
For i=1 To 39
  x=x/10
Next i
For i=1 To 79
  Debug(StrG(x,5)+"    "+StrF(x))
  x=x*10
  Next i
End