mirror of
https://github.com/storytold/harfbuzz.git
synced 2026-11-25 00:41:09 +01:00
[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.
This commit is contained in:
1 parent
6134871eee
commit
cc5fef8df1
3 files changed
+39
-6
No files matched your search
+14
-6
@@ -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.;
|
||||
|
||||
|
||||
@@ -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.;
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
Reference in new issue
Block a user