Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] use jl_strtof_c to convert strings to float #49699

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/flisp/read.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ int isnumtok_base(fl_context_t *fl_ctx, char *tok, value_t *pval, int base)
int64_t i64;
uint64_t ui64;
double d;
float f;

if (*tok == '\0')
return 0;
if (!((tok[0]=='0' && tok[1]=='x') || (base >= 15)) &&
Expand All @@ -44,11 +46,17 @@ int isnumtok_base(fl_context_t *fl_ctx, char *tok, value_t *pval, int base)
if (pval) *pval = mk_double(fl_ctx, d);
return 1;
}

// floats can end in f or f0
if (end > tok && end[0] == 'f' &&
(end[1] == '\0' ||
(end[1] == '0' && end[2] == '\0'))) {
if (pval) *pval = mk_float(fl_ctx, (float)d);
if (pval) {
// NOTE(#49689): DO NOT convert double to float directly,
// indirect conversion (string->double->float) will lose precision.
f = jl_strtof_c(tok, &end);
*pval = mk_float(fl_ctx, f);
}
return 1;
}
}
Expand All @@ -60,11 +68,17 @@ int isnumtok_base(fl_context_t *fl_ctx, char *tok, value_t *pval, int base)
if (pval) *pval = mk_double(fl_ctx, d);
return 1;
}

// floats can end in f or f0
if (end > tok && end[0] == 'f' &&
(end[1] == '\0' ||
(end[1] == '0' && end[2] == '\0'))) {
if (pval) *pval = mk_float(fl_ctx, (float)d);
if (pval) {
// NOTE(#49689): DO NOT convert double to float directly,
// indirect conversion (string->double->float) will lose precision.
f = jl_strtof_c(tok, &end);
*pval = mk_float(fl_ctx, f);
}
return 1;
}
}
Expand Down
4 changes: 0 additions & 4 deletions src/rtutils.c
Original file line number Diff line number Diff line change
Expand Up @@ -503,11 +503,7 @@ JL_DLLEXPORT jl_nullable_float32_t jl_try_substrtof(char *str, size_t offset, si
bstr = newstr;
pend = bstr+len;
}
#if defined(_OS_WINDOWS_) && !defined(_COMPILER_GCC_)
float out = (float)jl_strtod_c(bstr, &p);
#else
float out = jl_strtof_c(bstr, &p);
#endif

if (errno==ERANGE && (out==0 || out==HUGE_VALF || out==-HUGE_VALF)) {
hasvalue = 0;
Expand Down
27 changes: 27 additions & 0 deletions test/parse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,33 @@ end
@test tryparse(Float32, "1.23") === 1.23f0
@test tryparse(Float16, "1.23") === Float16(1.23)

@testset "issue #49689" begin
"""Test floating-point rounding

::Float32 ::Float64
17.3286_78f0 17.3286_78_131103516
?? 17.3286_79_084777832
?? 17.3286_79_084777833
17.3286_80f0 17.3286_80_03845215
"""
lower_bond = 17.3286_78f0
f64_str832 = "17.328679084777832"
f64_str833 = "17.328679084777833"
upper_bond = 17.3286_80f0

abs_diff(a, b) = abs(BigFloat(a) - BigFloat(b))
# f64_str832
@test abs_diff(lower_bond, f64_str832) < abs_diff(f64_str832, upper_bond)
@test tryparse(Float32, f64_str832) == lower_bond
@test 17.328679084777_832f0 == lower_bond

# f64_str833
@test abs_diff(lower_bond, f64_str833) > abs_diff(f64_str833, upper_bond)
@test tryparse(Float32, f64_str833) == upper_bond
@test_broken 17.328679084777_833f0 == upper_bond
# TODO: fix @test_broken in *nix platforms, add more test
end

# parsing complex numbers (#22250)
@testset "complex parsing" begin
for sign in ('-','+'), Im in ("i","j","im"), s1 in (""," "), s2 in (""," "), s3 in (""," "), s4 in (""," ")
Expand Down