Skip to content

Floating point comparison miscompilation #41

Closed
@aykevl

Description

@aykevl

I have the following C code:

void sideeffect(void);

int identify(float v) {
    if (v+v == v && v > 0) {
        return 2;
    }
    if (v < 0) {
        sideeffect();
        return 5;
    }
    return 4;
}

When I call this with identify(20) (where 20 is the floating point value with bits 0x41a00000, see here) the function will return 5. This is clearly incorrect, as v is 20 which is bigger than 0. Note that sideeffect doesn't do anything, but without some external call the misbehavior appears to be optimized away.
I have seen the same issue with other positive integers, I don't think the actual number matters.

I found the issue originally in TinyGo (runtime.printfloat32) but managed to reduce it and convert it to C while still maintaining the incorrect behavior.

This issue might be a duplicate of #20.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions