Stop to! rounding?

cal callumenator at gmail.com
Tue Jul 2 22:16:12 PDT 2013


On Wednesday, 3 July 2013 at 05:10:03 UTC, Jonathan M Davis wrote:
> On Wednesday, July 03, 2013 07:04:47 cal wrote:
>> On Wednesday, 3 July 2013 at 04:32:15 UTC, Jonathan M Davis 
>> wrote:
>> > On Wednesday, July 03, 2013 06:23:12 Josh wrote:
>> >> writeln(to!double("151.42499"));    //prints 151.425
>> >> 
>> >> Is there any way to stop this rounding?
>> > 
>> > No. double can't hold the value 151.42499. There are _tons_ 
>> > of
>> > values that it
>> > can't hold exactly. The same goes for float and real. 
>> > Floating
>> > point values are
>> > rounded all the time. Note that
>> > 
>> >     double d = 151.42499;
>> >     writeln(d);
>> > 
>> > prints exactly the same thing as your example.
>> > 
>> > - Jonathan M Davis
>> 
>> void main()
>> {
>>     double d = 151.42499;
>>     assert(d == 151.42499);
>> }
>> 
>> The rounding occurs in writeln surely.
>
> That's true because _both_ of the floating point values there 
> get rounded to
> 151.425, and 151.425 is equal to 151.425. writeln is not doing 
> anything wrong.
> I highly suggest that you read this:
>
> http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html
>
> - Jonathan M Davis

import std.stdio;
void main()
{
	double d = 151.42499;
	writefln("%.10f", d);
}


More information about the Digitalmars-d-learn mailing list