Skip to content

print floats more accurately#1297

Open
regehr wants to merge 1 commit intoAliveToolkit:masterfrom
regehr:print-floats-accurately
Open

print floats more accurately#1297
regehr wants to merge 1 commit intoAliveToolkit:masterfrom
regehr:print-floats-accurately

Conversation

@regehr
Copy link
Copy Markdown
Contributor

@regehr regehr commented Mar 17, 2026

earlier today I was working with this example:

define i1 @src(i16 %x, i16 %y) {
    %x1 = uitofp i16 %x to float
    %y1 = uitofp i16 %y to float
    %sub = fsub float %x1, %y1
    %abs = tail call float @llvm.fabs.f32(float %sub)
    %ext = fpext float %abs to double
    %cmp = fcmp olt double %ext, 1.000000e-15
    ret i1 %cmp
}

define i1 @tgt(i16 %x, i16 %y) {
    %cmp = icmp eq i16 %x, %y
    ret i1 %cmp
}

Alive verifies it but prints the float as 0.000000. with this patch we print the float as 1.0000000000000001E-15

also works for other float types, let me know if you want tests on this

toString(apfloat.bitcastToAPInt(), 10, false),
true);
unique_ptr<FloatConst> c;
if (apfloat.isFinite()) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this need to check for NaN?

c = make_unique<FloatConst>(*ty, string(str), false);
} else {
c = make_unique<FloatConst>(*ty,
toString(apfloat.bitcastToAPInt(), 10, false),
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm very surprised we would need this patch. Either bitcastToAPInt is not working correctly, or something is off with the printing in constant.cpp. Representing the bit pattern of the number should be the most precise way of storing it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants