Floating point rounding

Guillaume Chatelet via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Mar 2 13:49:53 PST 2017


On Thursday, 2 March 2017 at 21:34:56 UTC, ag0aep6g wrote:
> On 03/02/2017 10:10 PM, Guillaume Chatelet wrote:
>> On Thursday, 2 March 2017 at 20:30:47 UTC, Guillaume Chatelet 
>> wrote:
>>> Here is the same code in D:
>>> void main(string[] args)
>>> {
>>>     import std.math;
>>>     FloatingPointControl fpctrl;
>>>     fpctrl.rounding = FloatingPointControl.roundUp;
>>>     writefln("%.32g", float.min_normal + 1.0f);
>>> }
>>>
>>> Execution on my machine yields:
>>> dmd -run test_denormal.d
>>> 1
>>>
>>> Did I miss something?
>>
>> This example is closer to the C++ one:
>>
>> void main(string[] args)
>> {
>>     import core.stdc.fenv;
>>     fesetround(FE_UPWARD);
>>     writefln("%.32g", float.min_normal + 1.0f);
>> }
>>
>> It still yields "1"
>
> This prints the same as the C++ version:
>
> ----
> void main(string[] args)
> {
>     import std.stdio;
>     import core.stdc.fenv;
>     fesetround(FE_UPWARD);
>     float x = 1.0f;
>     x += float.min_normal;
>     writefln("%.32g", x);
> }
> ----
>
> Soo, a bug/limitation of constant folding?
>
> With FloatingPointControl it still prints "1". Does 
> FloatingPointControl.rounding do something different than 
> fesetround? The example in the docs [1] only shows how it 
> changes rint's behavior.
>
>
> [1] http://dlang.org/phobos/std_math.html#.FloatingPointControl

Thx for the investigation!
Here is the code for FloatingPointControl
https://github.com/dlang/phobos/blob/master/std/math.d#L4809

Other code (enableExceptions / disableExceptions) seems to have 
two code path depending on "version(X86_Any)", rounding doesn't.

Maybe that's the bug?



More information about the Digitalmars-d-learn mailing list