www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - floating point precision

reply dsmith <dsmith nomail.com> writes:
How do you increase floating point precision beyond the default of 6?
example:

double var = exp(-1.987654321123456789);
writeln(var);

--> 0.137016

Assuming this result is only an output format issue and that operations are
still using double's 64 places, if var above is passed to a function, are all
64 places passed?  Must it be passed by reference to make it so?
Jan 11 2012
next sibling parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 01/11/2012 03:51 PM, dsmith wrote:
 How do you increase floating point precision beyond the default of 6?
 example:

 double var = exp(-1.987654321123456789);
 writeln(var);

 -->  0.137016

 Assuming this result is only an output format issue and that operations are
 still using double's 64 places, if var above is passed to a function, are all
 64 places passed?  Must it be passed by reference to make it so?
writefln takes a format argument. The following uses the actual precision of the variable but it is wrong as it assumes (knows) that the whole part of the result is zero. Since precision is the total of significant digits, the digits after the period must be adjusted if the whole part is not zero. import std.math; import std.stdio; import std.conv; double var = exp(-1.987654321123456789); auto fmt = "%." ~ to!string(var.dig) ~ "f"; writefln(fmt, var); Also check out "%e", "%g", and "%a"; some of which produce the same output depending on the value. Ali
Jan 11 2012
prev sibling parent Mail Mantis <mail.mantis.88 gmail.com> writes:
All is passed, to print, say, 50 signs after period use following:
writefln("%.50f", var);


2012/1/12 dsmith <dsmith nomail.com>:
 How do you increase floating point precision beyond the default of 6?
 example:

 double var =3D exp(-1.987654321123456789);
 writeln(var);

 --> 0.137016

 Assuming this result is only an output format issue and that operations a=
re
 still using double's 64 places, if var above is passed to a function, are=
all
 64 places passed? =A0Must it be passed by reference to make it so?
Jan 11 2012