Printing floating point numbers with exponents.

Share your advanced PureBasic knowledge/code with the community.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Printing floating point numbers with exponents.

Post by wilbert »

skywalk wrote:I couldn't get your last asm version SciStrD() to run - Operand size error?
I made a few improvements (see above for updated code).
Maybe it solves the problem. I'm not able to replicate it.
I tried both x86 and x64 and it works fine here.
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Printing floating point numbers with exponents.

Post by skywalk »

Sorry, it ran once with debugger on, but not without.
My only compiler option is #PB_Compiler_unicode.
PB461b1 x86.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Printing floating point numbers with exponents.

Post by wilbert »

skywalk wrote:Sorry, it ran once with debugger on, but not without.
Strange that there's a difference.
I made a small change and think it should be working now.
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Printing floating point numbers with exponents.

Post by skywalk »

Cool, runs now and is 2% faster overall.
But, weird bug only if 1st call to sciStrD(0,6,1) contains a 0?

Code: Select all

; Data.s                   StrDe(S,6)     StrDsci(S,6)    sciStrD(S,6)    
Data.s "0"                ;[          0]  [ 0.000000e+0]  [ 0.000000e310] ;<-- This 'e310' goes away if called later?
Data.s "0.0"              ;[          0]  [ 0.000000e+0]  [ 0.000000e0]   
Data.s "+0e0"             ;[          0]  [ 0.000000e+0]  [ 0.000000e0]   
Data.s "+0.0e0"           ;[          0]  [ 0.000000e+0]  [ 0.000000e0]   
Data.s "+0.0e+0"          ;[          0]  [ 0.000000e+0]  [ 0.000000e0]   
Data.s "1"                ;[ 1.00000e+0]  [ 10.000000e-1] [ 1.000000e+0]  
Data.s "1.1"              ;[ 1.10000e+0]  [ 1.100000e+0]  [ 1.100000e+0]  
Data.s "0.1"              ;[ 100.000e-3]  [ 10.000000e-2] [ 1.000000e-1]  
Data.s "11"               ;[ 11.0000e+0]  [ 1.100000e+1]  [ 1.100000e+1]  
Data.s "111"              ;[ 111.000e+0]  [ 1.110000e+2]  [ 1.110000e+2]  
Data.s "10"               ;[ 10.0000e+0]  [ 1.000000e+1]  [ 1.000000e+1]  
Data.s "100"              ;[ 100.000e+0]  [ 1.000000e+2]  [ 1.000000e+2]  
Data.s "1000"             ;[ 1.00000e+3]  [ 1.000000e+3]  [ 1.000000e+3]  
Data.s "1000000"          ;[ 1.00000e+6]  [ 1.000000e+6]  [ 1.000000e+6]  
Data.s "999"              ;[ 999.000e+0]  [ 9.990000e+2]  [ 9.990000e+2]  
Data.s "-999"             ;[-999.000e+0]  [-9.990000e+2]  [-9.990000e+2]  
Data.s "-3.5e-20"         ;[-35.0000e-21] [-3.500000e-20] [-3.500000e-20] 
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Printing floating point numbers with exponents.

Post by wilbert »

skywalk wrote:Cool, runs now and is 2% faster overall.
But, weird bug only if 1st call to sciStrD(0,6,1) contains a 0?
The bug should be fixed now.

For your engineering notation, maybe a real fixed length would be nice.
If support would be limited from 1e-99 to 1e+99, you could make the exponent always two digits.
Like 1.23456e-06
Little John
Addict
Addict
Posts: 4777
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Printing floating point numbers with exponents.

Post by Little John »

skywalk wrote:Cool, runs now and is 2% faster overall.
And a maximum of 2% of the forum members are able to understand that code. :-)

Regards, LJ
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Printing floating point numbers with exponents.

Post by wilbert »

Little John wrote:And a maximum of 2% of the forum members are able to understand that code. :-)
To me it's like a puzzle. I learned some new things creating the code and that's the fun for me.
Most of the times ASM isn't required on modern computers since they are fast enough.
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Printing floating point numbers with exponents.

Post by skywalk »

wilbert wrote:For your engineering notation, maybe a real fixed length would be nice.
If support would be limited from 1e-99 to 1e+99, you could make the exponent always two digits.
Like 1.23456e-06
Yeah, I thought about forcing 2 exponent digits, but my data did not exceed "nano" or "giga" so e±9 is fine for me. Using "±xxx.yyyye±z" format, any number I print as a string can be retrieved with consistent numeric accuracy.
Little John wrote:And a maximum of 2% of the forum members are able to understand that code.
:lol:
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
akj
Enthusiast
Enthusiast
Posts: 668
Joined: Mon Jun 09, 2003 10:08 pm
Location: Nottingham

Re: Printing floating point numbers with exponents.

Post by akj »

@wilbert:

Thank you for the fix which seems to have solved the problem.
(I don't have any better solution of my own.)
Anthony Jordan
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Re: Printing floating point numbers with exponents.

Post by Psychophanta »

This is the function i use since long time for this issue:

Code: Select all

Procedure$ StrE(fp.d,dec.b=14)
  ;Devuelve cadena alfanumérica en formato de notación científica
  Protected E.b,neg.b=0
  If dec<0:dec=0:ElseIf dec>20:dec=20:EndIf;<- Sanity check
  E=Round(Log10(Abs(fp)),#PB_Round_Down)
  If E
    fp*Pow(10,-E)
    ProcedureReturn RTrim(RTrim(StrD(fp,dec),"0"),".")+"E"+Str(E)
  EndIf
  ProcedureReturn RTrim(RTrim(StrD(fp,dec),"0"),".")
EndProcedure
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
Post Reply