Thread overview | |||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
July 03, 2013 Stop to! rounding? | ||||
---|---|---|---|---|
| ||||
writeln(to!double("151.42499")); //prints 151.425 Is there any way to stop this rounding? Thanks, Josh |
July 03, 2013 Re: Stop to! rounding? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Josh | 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 Re: Stop to! rounding? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Josh | 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 Re: Stop to! rounding? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan M Davis | 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 Re: Stop to! rounding? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Josh | 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 Re: Stop to! rounding? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan M Davis | 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 Re: Stop to! rounding? | ||||
---|---|---|---|---|
| ||||
Posted in reply to cal | 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 Re: Stop to! rounding? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan M Davis | 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 Re: Stop to! rounding? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan M Davis | 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 Re: Stop to! rounding? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | 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. |
Copyright © 1999-2021 by the D Language Foundation