diff --git a/vlib/builtin/string_test.v b/vlib/builtin/string_test.v index f4a3ccca1a..cc8b65b7f7 100644 --- a/vlib/builtin/string_test.v +++ b/vlib/builtin/string_test.v @@ -936,3 +936,19 @@ fn test_index_any() { assert x.index_any('ef') == 4 assert x.index_any('fe') == 4 } + +fn test_string_f64() { + assert ''.f64() == 0 + assert '123'.f64() == 123 + assert '-123'.f64() == -123 + assert '-123.456'.f64() == -123.456 +} + +const f32_epsilon = 0.0000000001 + +fn test_string_f32() { + assert ''.f32() - 0 <= f32_epsilon + assert '123'.f32() - 123 < f32_epsilon + assert '-123'.f32() - (-123) < f32_epsilon + assert '-123.456'.f32() - (-123.456) <= f32_epsilon +} diff --git a/vlib/strconv/atof.c.v b/vlib/strconv/atof.c.v index f537afb327..304c0d3c53 100644 --- a/vlib/strconv/atof.c.v +++ b/vlib/strconv/atof.c.v @@ -78,9 +78,7 @@ fn sub96(s2 u32, s1 u32, s0 u32, d2 u32, d1 u32, d0 u32) (u32, u32, u32) { return r2, r1, r0 } -/* -Constants -*/ +// Constants pub const ( // @@ -118,9 +116,7 @@ pub const ( c_ten = u32(10) ) -/* -Utility -*/ +// Utility functions // NOTE: Modify these if working with non-ASCII encoding fn is_digit(x byte) bool { @@ -135,10 +131,6 @@ fn is_exp(x byte) bool { return (x == `E` || x == `e`) == true } -/* -Support struct -*/ - /* String parser NOTE: #TOFIX need one char after the last char of the number @@ -408,12 +400,13 @@ fn converter(mut pn PrepNumber) u64 { return result } -/* -Public functions -*/ +// Public functions // atof64 return a f64 from a string doing a parsing operation pub fn atof64(s string) f64 { + if s.len == 0 { + return 0 + } mut pn := PrepNumber{} mut res_parsing := 0 mut res := Float64u{}