Skip to content

converting real to float uses double rounding for 64-bit code causing unexpected results #22322

Description

@rainers

This code

import std.stdio;

void main()
{
    real r = 0.5000000894069671353303618843710864894092082977294921875;
    writefln("r = %a", r);
    double d = r;
    writefln("d = %a", d);
    float f = r;
    writefln("f = %a", f);
}

compiled with dmd and -m64 prints

r = 0x1.000002fffffffcp-1
d = 0x1.000003p-1
f = 0x1.000004p-1

but the float value should be 0x1.000002p-1.

This is caused by dmd doing a conversion to double first for f = r, e.g.:

		fld	tbyte ptr 010h[RBP]
		fstp	qword ptr -010h[RBP]
		movsd	XMM0,-010h[RBP]
		cvtsd2ss	XMM0,XMM0

This works correctly for -m32 or LDC or GDC.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Compiler:Backendglue code, optimizer, code generation

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions