Skip to content

Print a C++ float joined to a String as the Float of its value prints - #488

Open
dimensionscape wants to merge 1 commit into
mainfrom
fix/string-float-digits
Open

dimensionscape wants to merge 1 commit into
mainfrom
fix/string-float-digits

Conversation

@dimensionscape

Copy link
Copy Markdown
Member

Fixes #486.

Native code printed a 32-bit float joined straight to a String with 15 significant digits, and the same value held in a Float first with the shortest digits that read back:

var bytes = haxe.io.Bytes.alloc(4);
bytes.setFloat(0, 1 / 3);
Sys.println("" + bytes.getFloat(0));  // 0.333333343267441
var held:Float = bytes.getFloat(0);
Sys.println("" + held);               // 0.3333333432674408

cppia and Haxe's interpreter print 0.3333333432674408 for both. Small exponents differed too: 1.0000000116861e-07 against 1.0000000116860974e-7.

Cause and change

String::String(const float &) had its own copy of the formatting, and it stayed %.15g when String::String(const double &) got the shortest digits. It now makes its String from the double of the same value, so a float takes the shortest digits, or the format a program set with __hxcpp_set_float_format, exactly as a Float does.

This changes what native programs print for such a float: more digits where 15 did not read back. Lime's Matrix3.toString is one place it shows.

Tests

test/regression/FloatFromSingle prints each value joined straight from the read and from a Float variable, and the two lines of each pair must be the same: fractions, a small exponent, the largest float, the smallest denormal, zeros, infinities, NaN, and a set format. Before the change 5 of its 28 lines differed.

None of the existing tests prints a 32-bit read through this constructor, so no existing stdout.txt changes.

Run on 64-bit Windows with MSVC and Haxe 4.3.7: FloatFromSingle, FloatFormatPattern, FloatShortestDigits, BytesOddOffsets, CppiaFloat32Array, CppiaJitBuiltins, CppiaFloatFrames, BoxNegativeZero, DynamicDoubleRange, EnumFormattingShapes, MapFormattingCycles, ParseFloatWords, and test/haxe (2698 assertions). Those ran before the branch was rebased onto the commit that opens 6.0.1, which changes only the version and Changes.md; FloatFromSingle and FloatShortestDigits ran again after it.

Not run here: the whole of test/regression, test/std, and the other platforms.

The Changes.md entry sits at the same place as the one in the pull request for #485, so whichever merges second needs a rebase.

String's constructor from a float had its own copy of the formatting, and it
stayed "%.15g" when a Float got the shortest digits that read back. So native
code printed "" + bytes.getFloat(0) as 0.333333343267441, and the same value
held in a Float first as 0.3333333432674408, which is what cppia and the
other targets print for both. Lime's Matrix3.toString showed it in any
native build.

The float constructor now makes its String from the double of the same
value, so it takes the shortest digits, or the format a program set, exactly
as a Float does.
@dimensionscape
dimensionscape force-pushed the fix/string-float-digits branch from c99d847 to cc0f63b Compare November 26, 2026 16:41
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.

A 32-bit float joined to a String prints 15 digits, not the shortest

1 participant