Stop to! rounding?

Josh moonburntm at gmail.com
Tue Jul 2 23:11:50 PDT 2013


On Wednesday, 3 July 2013 at 04:51:06 UTC, Jonathan M Davis wrote:
> 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

Jonathan, do you know of any fixed point D library? If not, would 
it be worth me making one for phobos?

Thanks,
Josh


More information about the Digitalmars-d-learn mailing list