www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Full precision double to string conversion

reply Ecstatic Coder <ecstatic.coder gmail.com> writes:
import std.conv;
import std.stdio;
void main()
{
     double value = -12.000123456;
     writeln( value.sizeof );
     writeln( value );
     writeln( value.to!string() );
     writeln( value.to!dstring() );
}

/*
8
-12.0001
-12.0001
-12.0001
*/

In Dart, value.toString() returns "-12.000123456".



In D, value.to!string() returns "-12.0001" :(

How can I convert a double value -12.000123456 to its string 
value "-12.000123456", i.e. without loosing double-precision 
digits ?
Nov 03 2018
parent reply Danny Arends <Danny.Arends gmail.com> writes:
On Saturday, 3 November 2018 at 12:27:19 UTC, Ecstatic Coder 
wrote:
 import std.conv;
 import std.stdio;
 void main()
 {
     double value = -12.000123456;
     writeln( value.sizeof );
     writeln( value );
     writeln( value.to!string() );
     writeln( value.to!dstring() );
 }

 /*
 8
 -12.0001
 -12.0001
 -12.0001
 */

 In Dart, value.toString() returns "-12.000123456".



 In D, value.to!string() returns "-12.0001" :(

 How can I convert a double value -12.000123456 to its string 
 value "-12.000123456", i.e. without loosing double-precision 
 digits ?
Specify how many digits you want with writefln: writefln("%.8f", value);
Nov 03 2018
parent reply Ecstatic Coder <ecstatic.coder gmail.com> writes:
On Saturday, 3 November 2018 at 12:45:03 UTC, Danny Arends wrote:
 On Saturday, 3 November 2018 at 12:27:19 UTC, Ecstatic Coder 
 wrote:
 import std.conv;
 import std.stdio;
 void main()
 {
     double value = -12.000123456;
     writeln( value.sizeof );
     writeln( value );
     writeln( value.to!string() );
     writeln( value.to!dstring() );
 }

 /*
 8
 -12.0001
 -12.0001
 -12.0001
 */

 In Dart, value.toString() returns "-12.000123456".



 In D, value.to!string() returns "-12.0001" :(

 How can I convert a double value -12.000123456 to its string 
 value "-12.000123456", i.e. without loosing double-precision 
 digits ?
Specify how many digits you want with writefln: writefln("%.8f", value);
Actually, what I need is the D equivalent of the default I mean a dumb double-to-string standard library conversion function which returns a string including all the double precision digits stored in the 52 significant bits of the value, preferably with the trailing zeroes removed. For an unknown reason, D's default double-to-string conversion function only expose the single-precision significant digits :(
Nov 03 2018
parent reply Stanislav Blinov <stanislav.blinov gmail.com> writes:
On Saturday, 3 November 2018 at 13:20:22 UTC, Ecstatic Coder 
wrote:
 On Saturday, 3 November 2018 at 12:45:03 UTC, Danny Arends 
 wrote:
 How can I convert a double value -12.000123456 to its string 
 value "-12.000123456", i.e. without loosing double-precision 
 digits ?
Specify how many digits you want with writefln: writefln("%.8f", value);
Actually, what I need is the D equivalent of the default
I don't think it means what you think it means: void main() { double value = -12.000123456; int precision = 50; import std.stdio; writefln("%.*g", precision, value); import std.format; string str = format("%.*g", precision, value); writeln(str); } Prints: -12.000123456000000743415512260980904102325439453125 -12.000123456000000743415512260980904102325439453125 ToString().
 I mean a dumb double-to-string standard library conversion 
 function which returns a string including all the double 
 precision digits stored in the 52 significant bits of the 
 value, preferably with the trailing zeroes removed.
All of them? Most implementations of conversion algorithms actually stop when it's "good enough". AFAIR, D doesn't even have it's own implementation and forwards to C, unless that changed in recent years.
Nov 03 2018
parent reply Ecstatic Coder <ecstatic.coder gmail.com> writes:
 Actually, what I need is the D equivalent of the default 

I don't think it means what you think it means: void main() { double value = -12.000123456; int precision = 50; import std.stdio; writefln("%.*g", precision, value); import std.format; string str = format("%.*g", precision, value); writeln(str); } Prints: -12.000123456000000743415512260980904102325439453125 -12.000123456000000743415512260980904102325439453125 ToString().
Unfortunately, but that's still better though, thanks :)
 All of them? Most implementations of conversion algorithms 
 actually stop when it's "good enough". AFAIR, D doesn't even 
 have it's own implementation and forwards to C, unless that 
 changed in recent years.
What I meant was that getting too many significant digits would still be a better solution than not having them. But indeed what I really need is a D function which gives a better decimal approximation to the provided double constant, Is there really no such function in D ?
Nov 03 2018
parent reply Stanislav Blinov <stanislav.blinov gmail.com> writes:
On Saturday, 3 November 2018 at 17:26:19 UTC, Ecstatic Coder 
wrote:

 void main() {
     double value = -12.000123456;
     int precision = 50;

     import std.stdio;
     writefln("%.*g", precision, value);

     import std.format;
     string str = format("%.*g", precision, value);
     writeln(str);
 }

 Prints:

 -12.000123456000000743415512260980904102325439453125
 -12.000123456000000743415512260980904102325439453125


 ToString().
Unfortunately, but that's still better though, thanks :)
attempt to exhaust the precision when converting, given default arguments. It's merely a matter of those defaults. The snippet above obviously provides *more* digits that the default
 But indeed what I really need is a D function which gives a 
 better decimal approximation to the provided double constant, 


 Is there really no such function in D ?
"G" format specifier. https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings?view=netframework-4.7.2#the-general-g-format-specifier So for a double, it will use 15-digit precision. D's to!string simply uses lower default. If you want the exact same behavior as string toStringLikeInCSharp(double value) { import std.format : format; return format("%.15G", value); } void main() { double value = -12.000123456; import std.stdio; writeln(value.toStringLikeInCSharp); // prints: -12.000123456 }
Nov 03 2018
parent Ecstatic Coder <ecstatic.coder gmail.com> writes:
On Saturday, 3 November 2018 at 18:04:07 UTC, Stanislav Blinov 
wrote:
 On Saturday, 3 November 2018 at 17:26:19 UTC, Ecstatic Coder 
 wrote:

 void main() {
     double value = -12.000123456;
     int precision = 50;

     import std.stdio;
     writefln("%.*g", precision, value);

     import std.format;
     string str = format("%.*g", precision, value);
     writeln(str);
 }

 Prints:

 -12.000123456000000743415512260980904102325439453125
 -12.000123456000000743415512260980904102325439453125


 ToString().
Unfortunately, but that's still better though, thanks :)
attempt to exhaust the precision when converting, given default arguments. It's merely a matter of those defaults. The snippet above obviously provides *more* digits that the default
 But indeed what I really need is a D function which gives a 
 better decimal approximation to the provided double constant, 


 Is there really no such function in D ?
the "G" format specifier. https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings?view=netframework-4.7.2#the-general-g-format-specifier So for a double, it will use 15-digit precision. D's to!string simply uses lower default. If you want the exact same behavior string toStringLikeInCSharp(double value) { import std.format : format; return format("%.15G", value); } void main() { double value = -12.000123456; import std.stdio; writeln(value.toStringLikeInCSharp); // prints: -12.000123456 }
This version perfectly gets the job done! Thanks a lot for your help :)
Nov 03 2018