Removal of the 10 digits default from StrD()

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
luis
Addict
Addict
Posts: 3895
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Removal of the 10 digits default from StrD()

Post by luis »

help wrote: A maximum number of decimal places may be specified, if not it will be set to 10 decimal places, with removing the trailing zeros.
I don't think it's a good idea to have StrD() to output a max of 10 digits by default after the decimal point.

I would expect a double -> string conversion to keep all the available digits, and to discard them only when I ask for it explicitly specifying the second parameter.

Code: Select all

dd.d = 1.0/3.0

Debug dd ; 0.33333333333333331 

Debug StrD(dd) ; 0.3333333333     
; why ? no param specified, I just want the same number above converted to string

Debug StrD(dd, 3) ; 0.333         
; now that's ok, I'm asking for this to happen
Also I find counter-intuitive the fact 10 is the default and nevertheless ...

Code: Select all

dd.d = 1.234

Debug StrD(dd) ; 10 digits is the default
; result is 1.234

Debug StrD(dd, 10) ; same as the default and I get a different output... doesn't make a lot of sense
; result is 1.2340000000

So, in my opinion: StrD() without the optional param should convert all the available digits to the string representation.

Code: Select all

Procedure.s MyStrD (value.d, digits = #PB_Ignore)
 Protected Ret$
 Protected *in.Character, i
 
 If digits = #PB_Ignore
    Ret$ = ReverseString(StrD(value, 32))
    *in = @Ret$    
    While *in\c = '0'
        i + 1
        *in + SizeOf(Character)
    Wend             
    If *in\c = '.' 
        i + 1
    EndIf        
    Ret$ = ReverseString(Mid(Ret$, i+1))
 Else
    Ret$ = StrD(value, digits)
 EndIf
 
 ProcedureReturn Ret$
EndProcedure

; FULL DIGITS FOR THE DEFAULT SEEMS PREFERABLE TO ME

Debug MyStrD(123.5e-20) + " vs " + StrD(123.5e-20)
; 0.000000000000000001235 vs 0

Debug MyStrD(1.234025025075) + " vs " + StrD(1.234025025075) 
; 1.234025025075 vs 1.2340250251

Debug MyStrD(10) + " vs " + StrD(10) 
; 10 vs 10

Debug MyStrD(10.5) + " vs " + StrD(10.5)
; 10.5 vs 10.5

Debug MyStrD(1.0/3.0) + " vs " + StrD(1.0/3.0)
; 0.33333333333333331 vs 0.3333333333


; EXAMPLE

dd.d = 0.00045012052407 ;  how can I print the unknown number in dd ?

Debug StrD(dd) ; the default ? maybe I'm loosing some digits... -> 0.0004501205

Debug StrD(dd, 30) ; this way ? mabye I'm printing too many unneeded zeros ... -> 0.000450120524070000000000000000

Debug MyStrD(dd)  ; finally, the natural way -> 0.00045012052407
Last edited by luis on Tue Mar 05, 2013 12:24 am, edited 1 time in total.
"Have you tried turning it off and on again ?"
A little PureBasic review
Little John
Addict
Addict
Posts: 4779
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Removal of the 10 digits default from StrD()

Post by Little John »

+1
Post Reply