[Issue 16341] New: 32-bit floating-point issue

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Sun Jul 31 02:18:18 PDT 2016


https://issues.dlang.org/show_bug.cgi?id=16341

          Issue ID: 16341
           Summary: 32-bit floating-point issue
           Product: D
           Version: D2
          Hardware: x86
                OS: Linux
            Status: NEW
          Severity: major
          Priority: P1
         Component: dmd
          Assignee: nobody at puremagic.com
          Reporter: greensunny12 at gmail.com

While the difference is usually not noticeable, it's there and once used with
pow and exp, it can yield to totally different results (that's how I discovered
it).

import std.stdio;

alias S = float;
float shL = 0x1.1b95eep-4; // -0.069235
float shR = 0x1.9c7cfep-7; // -0.012588

F fun(F)(F x)
{
    return 1.0 + x * 2;
}

S working()
{
    S r = fun(shR);
    S l = fun(shL);
    return (r - l);
}

S failing()
{
    return (fun(shR) - fun(shL));
}

unittest
{
    writefln("work: %a", working);
    writefln("fail: %a", failing);
    assert(working == failing);
}


assembler:

.text._D3foo7workingFZf segment
        assume  CS:.text._D3foo7workingFZf
_D3foo7workingFZf:
                push    EBP
                mov     EBP,ESP
                sub     ESP,0Ch
                mov     -0Ch[EBP],EBX
                mov     EAX,GS:[00h]
                mov     ECX,_D3foo3shRf at TLS_IE
                push    [ECX][EAX]
                call      _D3foo10__T3funTfZ3funFNaNbNiNffZf at PC32
                fstp    float ptr -8[EBP]
                mov     EDX,GS:[00h]
                mov     EBX,_D3foo3shLf at TLS_IE
                push    [EBX][EDX]
                call      _D3foo10__T3funTfZ3funFNaNbNiNffZf at PC32
                fstp    float ptr -4[EBP]
                fld     float ptr -8[EBP]
                fsub    float ptr -4[EBP]
                mov     EBX,-0Ch[EBP]
                leave
                ret
                nop
                nop
                nop
                nop
                nop
.text._D3foo7workingFZf ends
.text._D3foo7failingFZf segment
        assume  CS:.text._D3foo7failingFZf
_D3foo7failingFZf:
                push    EBP
                mov     EBP,ESP
                sub     ESP,010h
                mov     -010h[EBP],EBX
                mov     EAX,GS:[00h]
                mov     ECX,_D3foo3shRf at TLS_IE
                push    [ECX][EAX]
                call      _D3foo10__T3funTfZ3funFNaNbNiNffZf at PC32
                mov     EDX,GS:[00h]
                mov     EBX,_D3foo3shLf at TLS_IE
                push    [EBX][EDX]
                fstp    float ptr -0Ch[EBP]
                call      _D3foo10__T3funTfZ3funFNaNbNiNffZf at PC32
                fld     float ptr -0Ch[EBP]
                fsubrp  ST(1),ST
                mov     EBX,-010h[EBP]
                leave
                ret
                nop
.text._D3foo7failingFZf ends
.text._D3foo10__T3funTfZ3funFNaNbNiNffZf        segment
        assume  CS:.text._D3foo10__T3funTfZ3funFNaNbNiNffZf
_D3foo10__T3funTfZ3funFNaNbNiNffZf:
                fld     float ptr 4[ESP]
                fadd    ST(0),ST
                fld1
                faddp   ST(1),ST
                ret     4
                nop
                nop
                nop
.text._D3foo10__T3funTfZ3funFNaNbNiNffZf        ends

--


More information about the Digitalmars-d-bugs mailing list