Jump to page: 1 2 3
Thread overview
Stop to! rounding?
Jul 03, 2013
Josh
Jul 03, 2013
Jonathan M Davis
Jul 03, 2013
Josh
Jul 03, 2013
Jonathan M Davis
Jul 03, 2013
Josh
Jul 03, 2013
Jonathan M Davis
Jul 03, 2013
monarch_dodra
Jul 03, 2013
Maxim Fomin
Jul 03, 2013
monarch_dodra
Jul 03, 2013
Jay Norwood
Jul 03, 2013
bearophile
Jul 03, 2013
cal
Jul 03, 2013
Jonathan M Davis
Jul 03, 2013
Ali Çehreli
Jul 03, 2013
H. S. Teoh
Jul 03, 2013
Timon Gehr
Jul 03, 2013
Marco Leise
Jul 03, 2013
H. S. Teoh
Jul 03, 2013
Jonathan M Davis
Jul 03, 2013
cal
Jul 03, 2013
cal
July 03, 2013
writeln(to!double("151.42499"));    //prints 151.425

Is there any way to stop this rounding?

Thanks,
Josh
July 03, 2013
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
July 03, 2013
On Wednesday, 3 July 2013 at 04:23:17 UTC, Josh wrote:
> writeln(to!double("151.42499"));    //prints 151.425
>
> Is there any way to stop this rounding?
>
> Thanks,
> Josh


The rounding is probably just a stdio default floating point format, ie:

writefln("%.10f", to!double("151.42499")); // 151.4249900000
July 03, 2013
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

Is there any way I would be able to hold that number then?

Thanks,
Josh
July 03, 2013
On Wednesday, July 03, 2013 06:44:33 Josh 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
> 
> Is there any way I would be able to hold that number then?

Without rounding? You'd need fixed point math rather than floating point math. There is nothing built into the language to support that (or into any C-based language of which I'm aware). I believe that C libraries exist which provide it though.

http://en.wikipedia.org/wiki/Fixed-point_arithmetic

Most people just use floating point though, since in most cases, you really don't need fixed point.

- Jonathan M Davis
July 03, 2013
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.
July 03, 2013
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
July 03, 2013
On 07/02/2013 10:09 PM, Jonathan M Davis wrote:

> On Wednesday, July 03, 2013 07:04:47 cal wrote:

>> 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,

The value that can be stored is not 151.42499, nor 151.425.

import std.stdio;
import std.conv;

void main()
{
    auto a = to!double("151.42499");
    writefln("%.60f", a);
}

Prints:

151.424990000000008194547262974083423614501953125000000000000000

> writeln is not doing anything wrong.

True. It is using its default floating point precision, 6.

Ali

July 03, 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);
}
July 03, 2013
On Tue, Jul 02, 2013 at 10:14:33PM -0700, Ali Çehreli wrote: [...]
> import std.stdio;
> import std.conv;
> 
> void main()
> {
>     auto a = to!double("151.42499");
>     writefln("%.60f", a);
> }

I wouldn't write it like that; IMO it's better to write:

	writefln("%.*f", double.dig, a);

So that you don't give the wrong impression that there are more digits than are actually there. Using double.dig also lets you see all the digits that *are* there, not a rounded value, that the OP was complaining about.


T

-- 
The easy way is the wrong way, and the hard way is the stupid way. Pick one.
« First   ‹ Prev
1 2 3