From cc5fef8df1a25b53bcd492445caf73c98ca96799 Mon Sep 17 00:00:00 2001 From: Ebrahim Date: Thu, 8 Oct 2026 23:25:19 +0330 Subject: [PATCH] [number] Round instead of truncating long fractional parts Previously we considered 0.9999999999999999 as 0.9999999999999990 and this rounds when it's not possible to record more frac. --- src/hb-number-parser.hh | 20 ++++++++++++++------ src/hb-number-parser.rl | 8 ++++++++ src/test-number.cc | 17 +++++++++++++++++ 3 files changed, 39 insertions(+), 6 deletions(-) diff --git a/src/hb-number-parser.hh b/src/hb-number-parser.hh index 8b8e9088b..34e95d6fc 100644 --- a/src/hb-number-parser.hh +++ b/src/hb-number-parser.hh @@ -91,7 +91,7 @@ static const int double_parser_error = 0; static const int double_parser_en_main = 1; -#line 68 "hb-number-parser.rl" +#line 70 "hb-number-parser.rl" /* Works only for n < 512 */ @@ -129,6 +129,8 @@ strtod_rl (const char *p, const char **end_ptr /* IN/OUT */) double frac_count = 0; unsigned exp = 0; bool neg = false, exp_neg = false, exp_overflow = false; + int frac_drop = -1; + bool frac_sticky = false; const unsigned long long MAX_FRACT = 0xFFFFFFFFFFFFFull; /* 2^52-1 */ const unsigned MAX_EXP = 0x7FFu; /* 2^11-1 */ @@ -138,12 +140,12 @@ strtod_rl (const char *p, const char **end_ptr /* IN/OUT */) int cs; -#line 142 "hb-number-parser.hh" +#line 144 "hb-number-parser.hh" { cs = double_parser_start; } -#line 147 "hb-number-parser.hh" +#line 149 "hb-number-parser.hh" { int _slen; int _trans; @@ -190,10 +192,12 @@ _resume: frac = frac * 10. + ((*p) - '0'); ++frac_count; } + // Record the tail we couldn't fit for round-to-nearest below + else if (frac_drop < 0) frac_drop = (*p) - '0'; else if ((*p) != '0') frac_sticky = true; } break; case 5: -#line 50 "hb-number-parser.rl" +#line 52 "hb-number-parser.rl" { if (likely (exp * 10 + ((*p) - '0') <= MAX_EXP)) exp = exp * 10 + ((*p) - '0'); @@ -201,7 +205,7 @@ _resume: exp_overflow = true; } break; -#line 205 "hb-number-parser.hh" +#line 209 "hb-number-parser.hh" } _again: @@ -213,11 +217,15 @@ _again: _out: {} } -#line 116 "hb-number-parser.rl" +#line 120 "hb-number-parser.rl" *end_ptr = p; + // Apply round-to-nearest-even using the dropped tail recorded above + if (frac_drop > 5 || + (frac_drop == 5 && (frac_sticky || (((uint64_t) frac) & 1)))) + frac += 1; if (frac_count) value += frac / _pow10 (frac_count); if (neg) value *= -1.; diff --git a/src/hb-number-parser.rl b/src/hb-number-parser.rl index fdc5f07f5..4ec7317e6 100644 --- a/src/hb-number-parser.rl +++ b/src/hb-number-parser.rl @@ -46,6 +46,8 @@ action add_frac { frac = frac * 10. + (fc - '0'); ++frac_count; } + // Record the tail we couldn't fit for round-to-nearest below + else if (frac_drop < 0) frac_drop = fc - '0'; else if (fc != '0') frac_sticky = true; } action add_exp { if (likely (exp * 10 + (fc - '0') <= MAX_EXP)) @@ -102,6 +104,8 @@ strtod_rl (const char *p, const char **end_ptr /* IN/OUT */) double frac_count = 0; unsigned exp = 0; bool neg = false, exp_neg = false, exp_overflow = false; + int frac_drop = -1; + bool frac_sticky = false; const unsigned long long MAX_FRACT = 0xFFFFFFFFFFFFFull; /* 2^52-1 */ const unsigned MAX_EXP = 0x7FFu; /* 2^11-1 */ @@ -117,6 +121,10 @@ strtod_rl (const char *p, const char **end_ptr /* IN/OUT */) *end_ptr = p; + // Apply round-to-nearest-even using the dropped tail recorded above + if (frac_drop > 5 || + (frac_drop == 5 && (frac_sticky || (((uint64_t) frac) & 1)))) + frac += 1; if (frac_count) value += frac / _pow10 (frac_count); if (neg) value *= -1.; diff --git a/src/test-number.cc b/src/test-number.cc index 947791aba..377726589 100644 --- a/src/test-number.cc +++ b/src/test-number.cc @@ -253,5 +253,22 @@ main (int argc, char **argv) hb_always_assert (pv == 0.0); } + { + const char str[] = "0.9999999999999999"; + const char *pp = str; + const char *end = str + ARRAY_LENGTH (str) - 1; + double pv; + hb_always_assert (hb_parse_double (&pp, end, &pv)); + hb_always_assert (pv == 1.0); + } + { + const char str[] = "0.499999999999999999"; + const char *pp = str; + const char *end = str + ARRAY_LENGTH (str) - 1; + double pv; + hb_always_assert (hb_parse_double (&pp, end, &pv)); + hb_always_assert (pv == 0.5); + } + return 0; }