strconv: bug fix for "${239.5:0.0f}" (#13483)

pull/13493/head
Delyan Angelov 2022-02-16 10:33:29 +02:00 committed by GitHub
parent 6d2a88e31f
commit 9c24d09dc3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 88 additions and 1 deletions

View File

@ -145,3 +145,86 @@ fn test_f32_eq_epsilon() {
assert !e.eq_epsilon(y) assert !e.eq_epsilon(y)
assert !(-y).eq_epsilon(-e) assert !(-y).eq_epsilon(-e)
} }
fn test_float_point_formatting_rounding() {
float_1 := 462.18
float_2 := 45.02227
float_3 := 238.5
float_4 := 239.5
assert '${float_1:0.0f}' == '462'
assert '${float_2:0.0f}' == '45'
assert '${float_3:0.0f}' == '239'
assert '${float_4:0.0f}' == '240'
//
//
assert '${239.5555551:0.0f}' == '240'
assert '${239.5555551:0.1f}' == '239.6'
assert '${239.5555551:0.2f}' == '239.56'
assert '${239.5555551:0.3f}' == '239.556'
assert '${239.5555551:0.4f}' == '239.5556'
assert '${239.5555551:0.5f}' == '239.55556'
assert '${239.5555551:0.6f}' == '239.555555'
assert '${239.5555551:0.7f}' == '239.5555551'
assert '${239.5555551:0.8f}' == '239.55555510'
assert '${239.5555551:0.9f}' == '239.555555100'
assert '${239.5555551:0.10f}' == '239.5555551000'
assert '${239.5555551:0.11f}' == '239.55555510000'
//
assert '${239.5:0.0f}' == '240'
assert '${239.55:0.1f}' == '239.6'
assert '${239.555:0.2f}' == '239.56'
assert '${239.5555:0.3f}' == '239.555' // NB: 5 ?
assert '${239.55555:0.4f}' == '239.5556'
assert '${239.555555:0.5f}' == '239.55555' // NB: 5 ?
assert '${239.5555555:0.6f}' == '239.555556' // after this, it is all ending in 6
assert '${239.55555555:0.7f}' == '239.5555556'
assert '${239.555555555:0.8f}' == '239.55555556'
assert '${239.5555555555:0.9f}' == '239.555555556'
assert '${239.55555555555:0.10f}' == '239.5555555556'
//
assert '${239.5550:0.3f}' == '239.555'
assert '${239.5551:0.3f}' == '239.555'
assert '${239.5552:0.3f}' == '239.555'
assert '${239.5553:0.3f}' == '239.555'
assert '${239.5554:0.3f}' == '239.555'
assert '${239.5555:0.3f}' == '239.555'
assert '${239.5556:0.3f}' == '239.556' // rounding at last 6 ?
assert '${239.5557:0.3f}' == '239.556'
assert '${239.5558:0.3f}' == '239.556'
assert '${239.5559:0.3f}' == '239.556'
//
assert '${239.5555551:0.6f}' == '239.555555'
assert '${239.5555552:0.6f}' == '239.555555'
assert '${239.5555553:0.6f}' == '239.555555'
assert '${239.5555554:0.6f}' == '239.555555'
assert '${239.5555555:0.6f}' == '239.555556'
assert '${239.5555556:0.6f}' == '239.555556'
assert '${239.5555557:0.6f}' == '239.555556'
assert '${239.5555558:0.6f}' == '239.555556'
assert '${239.5555559:0.6f}' == '239.555556'
//
assert '${239.55555555555:0.10f}' == '239.5555555556'
assert '${239.55555555555:0.9f}' == '239.555555556'
assert '${239.55555555555:0.8f}' == '239.55555556'
assert '${239.55555555555:0.7f}' == '239.5555556'
assert '${239.55555555555:0.6f}' == '239.555556'
assert '${239.55555555555:0.5f}' == '239.55556'
assert '${239.55555555555:0.4f}' == '239.5556'
assert '${239.55555555555:0.3f}' == '239.556'
assert '${239.55555555555:0.2f}' == '239.56'
assert '${239.55555555555:0.1f}' == '239.6'
assert '${239.55555555555:0.0f}' == '240'
//
assert '${-239.55555555555:0.10f}' == '-239.5555555556'
assert '${-239.55555555555:0.9f}' == '-239.555555556'
assert '${-239.55555555555:0.8f}' == '-239.55555556'
assert '${-239.55555555555:0.7f}' == '-239.5555556'
assert '${-239.55555555555:0.6f}' == '-239.555556'
assert '${-239.55555555555:0.5f}' == '-239.55556'
assert '${-239.55555555555:0.4f}' == '-239.5556'
assert '${-239.55555555555:0.3f}' == '-239.556'
assert '${-239.55555555555:0.2f}' == '-239.56'
assert '${-239.55555555555:0.1f}' == '-239.6'
assert '${-239.55555555555:0.0f}' == '-240'
}

View File

@ -156,7 +156,7 @@ pub fn f64_to_str_lnd1(f f64, dec_digit int) string {
mut dot_res_sp := -1 mut dot_res_sp := -1
// get sign and deciaml parts // get sign and decimal parts
for c in s { for c in s {
if c == `-` { if c == `-` {
sgn = -1 sgn = -1
@ -263,6 +263,10 @@ pub fn f64_to_str_lnd1(f f64, dec_digit int) string {
// no more digits needed, stop here // no more digits needed, stop here
if dec_digit <= 0 { if dec_digit <= 0 {
// C.printf(c'f: %f, i: %d, res.data: %p | dot_res_sp: %d | *(res.data): %s \n', f, i, res.data, dot_res_sp, res.data)
if dot_res_sp < 0 {
dot_res_sp = i + 1
}
tmp_res := tos(res.data, dot_res_sp).clone() tmp_res := tos(res.data, dot_res_sp).clone()
res.free() res.free()
return tmp_res return tmp_res