From 770132ff37f7b14da4e1a8f7d53e86b8fdaaf7e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Uwe=20Kr=C3=BCger?= <45282134+UweKrueger@users.noreply.github.com> Date: Thu, 18 Jun 2020 22:33:41 +0200 Subject: [PATCH] float: make default string representations for floats become alike --- vlib/builtin/float.v | 36 ++++++++++++++----------- vlib/v/tests/num_lit_call_method_test.v | 11 +++++++- 2 files changed, 30 insertions(+), 17 deletions(-) diff --git a/vlib/builtin/float.v b/vlib/builtin/float.v index e52e0ffa19..2726b14e50 100644 --- a/vlib/builtin/float.v +++ b/vlib/builtin/float.v @@ -7,23 +7,22 @@ import strconv.ftoa #include // ----- f64 to string functions ----- -// str return a f64 as string in scientific notation, auto display digits limit +// str return a f64 as string in suitable notation [inline] -pub fn (d f64) str() string { - return ftoa.ftoa_64(d) -} - -[inline] -pub fn (d any_float) str() string { - x := f64(d) +pub fn (x f64) str() string { abs_x := f64_abs(x) - if abs_x >= 0.01 && abs_x < 1.0e16 { + if abs_x >= 0.0001 && abs_x < 1.0e6 { return ftoa.f64_to_str_l(x) } else { return ftoa.ftoa_64(x) } } +[inline] +pub fn (d any_float) str() string { + return f64(d).str() +} + // return a string of the input f64 in scientific notation with digit_num deciamals displayed, max 17 digits [inline] pub fn (x f64) strsci(digit_num int) string { @@ -43,10 +42,15 @@ pub fn (x f64) strlong() string { } // ----- f32 to string functions ----- -// str return a f32 as string in scientific notation, auto display digits limit +// str return a f32 as string in suitable notation [inline] -pub fn (d f32) str() string { - return ftoa.ftoa_32(d) +pub fn (x f32) str() string { + abs_x := f32_abs(x) + if abs_x >= 0.0001 && abs_x < 1.0e6 { + return ftoa.f32_to_str_l(x) + } else { + return ftoa.ftoa_32(x) + } } // return a string of the input f32 in scientific notation with digit_num deciamals displayed, max 8 digits @@ -69,7 +73,7 @@ pub fn (x f32) strlong() string { // ----- C functions ----- [inline] -fn f32_abs(a f32) f32 { +pub fn f32_abs(a f32) f32 { return if a < 0 { -a } else { @@ -88,7 +92,7 @@ fn f64_abs(a f64) f64 { [inline] -fn f32_max(a, b f32) f32 { +pub fn f32_max(a, b f32) f32 { return if a > b { a } else { @@ -97,7 +101,7 @@ fn f32_max(a, b f32) f32 { } [inline] -fn f32_min(a, b f32) f32 { +pub fn f32_min(a, b f32) f32 { return if a < b { a } else { @@ -106,7 +110,7 @@ fn f32_min(a, b f32) f32 { } [inline] -fn f64_max(a, b f64) f64 { +pub fn f64_max(a, b f64) f64 { return if a > b { a } else { diff --git a/vlib/v/tests/num_lit_call_method_test.v b/vlib/v/tests/num_lit_call_method_test.v index 3346d3acb8..448911295b 100644 --- a/vlib/v/tests/num_lit_call_method_test.v +++ b/vlib/v/tests/num_lit_call_method_test.v @@ -21,8 +21,17 @@ fn test_float_lit_call_method() { x4 := .003e2.str() assert x4 == '0.3' x5 := 2.e-3.str() - assert x5 == '2.e-03' + assert x5 == '0.002' x6 := 5.0.str() assert x6 == '5.' // x7 := 5..str() Syntax `5.` is allowed, but do not call method on it (`5..str()` is parsed as a range). Use `5.0.str()` instead. + x8 := 7.345e-7.str() + assert x8 == '7.345e-07' + x9 := 5.725e6.str() + assert x9 == '5.725e+06' + + a := f32(12.3).str() + assert a[0..4] == '12.3' // there is still trailing garbage + assert f32(7.345e-7).str() == '7.345e-07' + assert f32(5.725e6).str() == '5.725e+06' }