Skip to content

Commit 9df7b0e

Browse files
committed
flisp: use jl_strtof_c to convert strings to float
indirect conversion (string->double->float) will lose precision
1 parent 346f38b commit 9df7b0e

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

src/flisp/read.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ int isnumtok_base(fl_context_t *fl_ctx, char *tok, value_t *pval, int base)
3535
int64_t i64;
3636
uint64_t ui64;
3737
double d;
38+
float f;
39+
3840
if (*tok == '\0')
3941
return 0;
4042
if (!((tok[0]=='0' && tok[1]=='x') || (base >= 15)) &&
@@ -44,11 +46,15 @@ int isnumtok_base(fl_context_t *fl_ctx, char *tok, value_t *pval, int base)
4446
if (pval) *pval = mk_double(fl_ctx, d);
4547
return 1;
4648
}
49+
50+
// NOTE(#49689): DO NOT convert double to float directly,
51+
// indirect conversion (string->double->float) will lose precision.
52+
f = jl_strtof_c(tok, &end);
4753
// floats can end in f or f0
4854
if (end > tok && end[0] == 'f' &&
4955
(end[1] == '\0' ||
5056
(end[1] == '0' && end[2] == '\0'))) {
51-
if (pval) *pval = mk_float(fl_ctx, (float)d);
57+
if (pval) *pval = mk_float(fl_ctx, f);
5258
return 1;
5359
}
5460
}
@@ -60,11 +66,15 @@ int isnumtok_base(fl_context_t *fl_ctx, char *tok, value_t *pval, int base)
6066
if (pval) *pval = mk_double(fl_ctx, d);
6167
return 1;
6268
}
69+
70+
// NOTE(#49689): DO NOT convert double to float directly,
71+
// indirect conversion (string->double->float) will lose precision.
72+
f = jl_strtof_c(tok, &end);
6373
// floats can end in f or f0
6474
if (end > tok && end[0] == 'f' &&
6575
(end[1] == '\0' ||
6676
(end[1] == '0' && end[2] == '\0'))) {
67-
if (pval) *pval = mk_float(fl_ctx, (float)d);
77+
if (pval) *pval = mk_float(fl_ctx, f);
6878
return 1;
6979
}
7080
}

0 commit comments

Comments
 (0)