digitalmars.D.learn - floating point value rounded to 6digits
- greatsam4sure (5/5) Sep 19 2017 double value = 20.89766554373733;
- Ivan Kazmenko (12/17) Sep 19 2017 The default when printing floating-point numbers is to show six
- greatsam4sure (4/22) Sep 19 2017 I don't want to use write,writefln or format. I just want to
- Steven Schveighoffer (5/8) Sep 19 2017 It's not a bad idea for an enhancement request -- provide default format...
- Ivan Kazmenko (30/57) Sep 19 2017 Unlikely to be possible. The built-in data types, such as float
- Steven Schveighoffer (9/42) Sep 19 2017 What he's looking for is a way to globally set "I want all floating
- Jonathan M Davis (13/56) Sep 19 2017 The big problem with that is that it does not play nicely at all with pu...
- Steven Schveighoffer (4/58) Sep 19 2017 That's a perfectly acceptable tradeoff. So:
double value = 20.89766554373733; writeln(value); //Output =20.8977 How do I output the whole value without using writfln,write or format. How do I change this default
Sep 19 2017
On Tuesday, 19 September 2017 at 20:47:02 UTC, greatsam4sure wrote:double value = 20.89766554373733; writeln(value); //Output =20.8977 How do I output the whole value without using writfln,write or format. How do I change this defaultThe default when printing floating-point numbers is to show six most significant digits. You can specify the formatting manually with writefln, for example, writefln ("%.10f", value); will print the value with 10 digits after the decimal point. The writef/writefln function behaves much like printf in C. See here for a reference on format strings: https://dlang.org/library/std/format/formatted_write.html#format-string Ivan Kazmenko.
Sep 19 2017
On Tuesday, 19 September 2017 at 21:52:57 UTC, Ivan Kazmenko wrote:On Tuesday, 19 September 2017 at 20:47:02 UTC, greatsam4sure wrote:I don't want to use write,writefln or format. I just want to change the defaultdouble value = 20.89766554373733; writeln(value); //Output =20.8977 How do I output the whole value without using writfln,write or format. How do I change this defaultThe default when printing floating-point numbers is to show six most significant digits. You can specify the formatting manually with writefln, for example, writefln ("%.10f", value); will print the value with 10 digits after the decimal point. The writef/writefln function behaves much like printf in C. See here for a reference on format strings: https://dlang.org/library/std/format/formatted_write.html#format-string Ivan Kazmenko.
Sep 19 2017
On 9/19/17 6:44 PM, greatsam4sure wrote:I don't want to use write,writefln or format. I just want to change the defaultIt's not a bad idea for an enhancement request -- provide default format specifiers for a given type. Currently, there isn't a mechanism for that. -Steve
Sep 19 2017
On Tuesday, 19 September 2017 at 22:44:06 UTC, greatsam4sure wrote:On Tuesday, 19 September 2017 at 21:52:57 UTC, Ivan Kazmenko wrote:Unlikely to be possible. The built-in data types, such as float or double, by definition should not be customizable to such degree. Anyway, under the hood, write uses format with the default format specifier "%s" for the values it takes. So perhaps I'm not quite getting what exactly are you seeking to avoid. For example, consider a helper function to convert the values, like the following: import std.format, std.stdio; string fmt (double v) {return v.format !("%.10f");} void main () { double x = 1.01; writeln (x.fmt); // 1.0100000000 } Alternatively, you can wrap your floating-point numbers in a thin struct with a custom toString(): import std.format, std.stdio; struct myDouble { double v; alias v this; this (double v_) {v = v_;} string toString () {return v.format !("%.10f");} } void main () { myDouble x = 1.01, y = 2.02, z = x + y; writeln (z); // 3.0300000000 } Ivan Kazmenko.On Tuesday, 19 September 2017 at 20:47:02 UTC, greatsam4sure wrote:I don't want to use write,writefln or format. I just want to change the defaultdouble value = 20.89766554373733; writeln(value); //Output =20.8977 How do I output the whole value without using writfln,write or format. How do I change this defaultThe default when printing floating-point numbers is to show six most significant digits. You can specify the formatting manually with writefln, for example, writefln ("%.10f", value); will print the value with 10 digits after the decimal point. The writef/writefln function behaves much like printf in C. See here for a reference on format strings: https://dlang.org/library/std/format/formatted_write.html#format-string Ivan Kazmenko.
Sep 19 2017
On 9/19/17 7:28 PM, Ivan Kazmenko wrote:On Tuesday, 19 September 2017 at 22:44:06 UTC, greatsam4sure wrote:What he's looking for is a way to globally set "I want all floating point values to print this way, unless a more specific specifier is given." It's not a terrible idea, as any code that's using %s most of the time doesn't really care what the result looks like. I imagine an API like this: import std.format: setDefaultFormat; setDefaultFormat!float("%.10f"); -SteveOn Tuesday, 19 September 2017 at 21:52:57 UTC, Ivan Kazmenko wrote:Unlikely to be possible. The built-in data types, such as float or double, by definition should not be customizable to such degree. Anyway, under the hood, write uses format with the default format specifier "%s" for the values it takes. So perhaps I'm not quite getting what exactly are you seeking to avoid.On Tuesday, 19 September 2017 at 20:47:02 UTC, greatsam4sure wrote:I don't want to use write,writefln or format. I just want to change the defaultdouble value = 20.89766554373733; writeln(value); //Output =20.8977 How do I output the whole value without using writfln,write or format. How do I change this defaultThe default when printing floating-point numbers is to show six most significant digits. You can specify the formatting manually with writefln, for example, writefln ("%.10f", value); will print the value with 10 digits after the decimal point. The writef/writefln function behaves much like printf in C. See here for a reference on format strings: https://dlang.org/library/std/format/formatted_write.html#format-string Ivan Kazmenko.
Sep 19 2017
On Tuesday, September 19, 2017 19:35:15 Steven Schveighoffer via Digitalmars-d-learn wrote:On 9/19/17 7:28 PM, Ivan Kazmenko wrote:The big problem with that is that it does not play nicely at all with pure. For writeln, that doesn't matter much, since it can't be pure anyway, because it's doing I/O, but it would matter for stuff like format or formattedWrite, which IIRC, writeln uses internally. If what the OP wants is to change what writeln does for floating point values, the easiest way would be for them to create their own writeln and use that instead. Then, it can forward to std.stdio.writeln for everything but floating point values, and for floating point values, it can call writefln with whatever format specifier gives the desired number of decimal places. - Jonathan M DavisOn Tuesday, 19 September 2017 at 22:44:06 UTC, greatsam4sure wrote:What he's looking for is a way to globally set "I want all floating point values to print this way, unless a more specific specifier is given." It's not a terrible idea, as any code that's using %s most of the time doesn't really care what the result looks like. I imagine an API like this: import std.format: setDefaultFormat; setDefaultFormat!float("%.10f");On Tuesday, 19 September 2017 at 21:52:57 UTC, Ivan Kazmenko wrote:Unlikely to be possible. The built-in data types, such as float or double, by definition should not be customizable to such degree. Anyway, under the hood, write uses format with the default format specifier "%s" for the values it takes. So perhaps I'm not quite getting what exactly are you seeking to avoid.On Tuesday, 19 September 2017 at 20:47:02 UTC, greatsam4sure wrote:I don't want to use write,writefln or format. I just want to change the defaultdouble value = 20.89766554373733; writeln(value); //Output =20.8977 How do I output the whole value without using writfln,write or format. How do I change this defaultThe default when printing floating-point numbers is to show six most significant digits. You can specify the formatting manually with writefln, for example, writefln ("%.10f", value); will print the value with 10 digits after the decimal point. The writef/writefln function behaves much like printf in C. See here for a reference on format strings: https://dlang.org/library/std/format/formatted_write.html#format-strin g Ivan Kazmenko.
Sep 19 2017
On 9/19/17 8:04 PM, Jonathan M Davis wrote:On Tuesday, September 19, 2017 19:35:15 Steven Schveighoffer via Digitalmars-d-learn wrote:That's a perfectly acceptable tradeoff. So: stdout.setDefaultFormat!float("%.10f"); -SteveOn 9/19/17 7:28 PM, Ivan Kazmenko wrote:The big problem with that is that it does not play nicely at all with pure. For writeln, that doesn't matter much, since it can't be pure anyway, because it's doing I/O, but it would matter for stuff like format or formattedWrite, which IIRC, writeln uses internally.On Tuesday, 19 September 2017 at 22:44:06 UTC, greatsam4sure wrote:What he's looking for is a way to globally set "I want all floating point values to print this way, unless a more specific specifier is given." It's not a terrible idea, as any code that's using %s most of the time doesn't really care what the result looks like. I imagine an API like this: import std.format: setDefaultFormat; setDefaultFormat!float("%.10f");On Tuesday, 19 September 2017 at 21:52:57 UTC, Ivan Kazmenko wrote:Unlikely to be possible. The built-in data types, such as float or double, by definition should not be customizable to such degree. Anyway, under the hood, write uses format with the default format specifier "%s" for the values it takes. So perhaps I'm not quite getting what exactly are you seeking to avoid.On Tuesday, 19 September 2017 at 20:47:02 UTC, greatsam4sure wrote:I don't want to use write,writefln or format. I just want to change the defaultdouble value = 20.89766554373733; writeln(value); //Output =20.8977 How do I output the whole value without using writfln,write or format. How do I change this defaultThe default when printing floating-point numbers is to show six most significant digits. You can specify the formatting manually with writefln, for example, writefln ("%.10f", value); will print the value with 10 digits after the decimal point. The writef/writefln function behaves much like printf in C. See here for a reference on format strings: https://dlang.org/library/std/format/formatted_write.html#format-strin g Ivan Kazmenko.
Sep 19 2017