String to float and float to string

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

Geoff:
I don't believe we need floats and doubles
Tinman:
I think PB should keep floats even if it gets doubles.
Geoff:
Yes, we need both.

Even if expressions are evaluated using doubles, we need float arrays for
applications that use huge arrays but can manage with modest accuracy.
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 Manolo.

Hi geoff,
I make one change in you function for work with 2 decimals all time:

Code: Select all

Procedure.s StrG(a.f)
; Convert float to string with n digits precision
  n=Len(Mid(StrF(a),1,FindString(StrF(a),".",1)+1))
  If Int(a)=0
  n-1
  EndIf
 
  If Int(a)-a=0
      n=Len(StrF(Int(a)))+2     
  EndIf

  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

And work fine. Thanks for this extraordinary function.

Manolo
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.

Manolo,

The latest version of my procedure gives the option of n digits precision or n decimal places. This has some advantages over your modification to the old routine.

1) It allows variable decimal places.
2) It works over a wider number range
3) It does not use StrF(), which will likely change in the future.

Code: Select all

Procedure.s zstrf(a.f,n.l); For n>0 convert float to string with n digits precision
; For nb:ProcedureReturn("O/F"):EndIf
  b=1/b
  If aValF("2e9")
      a$="Too long"
    Else
      i=Round(a+0.5,0)
      b$=Str(i)
      If n=0
        a$+b$
      Else
        If Len(b$)8:n=8:EndIf;max float accuracy
    b=1:For i.l=1 To n: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
  EndIf
ProcedureReturn a$
EndProcedure
Martin Schönegg
New User
New User
Posts: 1
Joined: Wed May 07, 2003 10:53 am

Post by Martin Schönegg »

I do not aggree with the opinion that we need Str instaed of StrF... I know very well what kind of variable i use and there is no problem to use the correct command. But I need the scientific way to put values in and out of my prog.
My first Z80 based computer knows the command using and I need at least a similar way to display values.
USING "###.##" creates the sign, two digits, the decimal point and two digits behind
USING "##.##^" creates sign, one digit bevore and two after decimal point with scientific Exponent (always 2 digits plus sign= 4 characters)

64 bit floats would be great and may be we can live without the old 4-byte-floats. The only thing I can imagine to be useful with this old format is an Array with several dimensions of floats creating huge memoryneeds. But : The inflation with memory should give us legitimation to forget 4-byte-floats at least the focus should be changed.
Please give us double-precision floats and a practicable way to use them. Thanks

Martin Schönegg

I'm not a game programmer, but engineer that needs a tool to create fast and stable solutions. Pb semms to be on the right way to be this tool
Post Reply