www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Division - precision

reply Rygel <rygel ryygel.com> writes:
Hi there. I'm a beginners, so my questions could be silly.

	double x1 = 7.0;
	double x2 = 3.0;
	writeln(x1 / x2);

from this code i get:

2.33333

this is ok but how can i get more digits? For example:

2.333333333.....

thx :-)
May 21 2016
next sibling parent Era Scarecrow <rtcvb32 yahoo.com> writes:
On Saturday, 21 May 2016 at 21:45:19 UTC, Rygel wrote:
 Hi there. I'm a beginners, so my questions could be silly.

 	double x1 = 7.0;
 	double x2 = 3.0;
 	writeln(x1 / x2);

 this is ok but how can i get more digits? For example:

 2.333333333.....
I'm not sure how to globally set the number of output characters, but you can force it with formatting. writefln("%.10g", x1 / x2); //floating point with 10 digits of precision
May 21 2016
prev sibling next sibling parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 05/21/2016 02:45 PM, Rygel wrote:
 Hi there. I'm a beginners, so my questions could be silly.

      double x1 = 7.0;
      double x2 = 3.0;
      writeln(x1 / x2);

 from this code i get:

 2.33333

 this is ok but how can i get more digits? For example:

 2.333333333.....

 thx :-)
There are two kinds of precision to talk about in this case: 1) The value: You can't do anything about the precision other than picking a more precise type. ('real' has more precision than 'double'.) 2) The display: You can display the value more precisely with a format specifier like "%.10f" (with 10 digit precision): import std.stdio; import std.string; void main() { double x1 = 7.0; double x2 = 3.0; pragma(msg, format("%s has %s-digit precision", double.stringof, double.dig)); writeln("Printing with too many digits:"); writefln("%.30f", x1 / x2); static const betterFormatSpec = format("%%.%sf", double.dig); writefln("Printing with just the right amount precision:"); writefln("(The format string used is \"%s\".)", betterFormatSpec); writefln(betterFormatSpec, x1 / x2); } In any case, watch Don Clugston's DConf 2016 presentation as an eye-opener and great entertainment: http://www.ustream.tv/recorded/86406491/highlight/699207 Ali
May 21 2016
prev sibling parent Rygel <rygel ryygel.com> writes:
Ah, ok, got it. thx you all.
May 21 2016