1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

builtin,strconv: append ".0", to float string representations, to ensure clarity (#16079)

This commit is contained in:
Subhomoy Haldar
2022-10-17 13:41:07 +01:00
committed by GitHub
parent 29b1796791
commit 43b9a716c5
21 changed files with 98 additions and 74 deletions

View File

@@ -19,10 +19,10 @@ pub fn (x f64) str() string {
f: x
}
if f.u == strconv.double_minus_zero {
return '-0'
return '-0.0'
}
if f.u == strconv.double_plus_zero {
return '0'
return '0.0'
}
}
abs_x := f64_abs(x)
@@ -37,11 +37,11 @@ pub fn (x f64) str() string {
[inline]
pub fn (x f64) strg() string {
if x == 0 {
return '0'
return '0.0'
}
abs_x := f64_abs(x)
if abs_x >= 0.0001 && abs_x < 1.0e6 {
return strconv.f64_to_str_l_no_dot(x)
return strconv.f64_to_str_l_with_dot(x)
} else {
return strconv.ftoa_64(x)
}
@@ -85,10 +85,10 @@ pub fn (x f32) str() string {
f: x
}
if f.u == strconv.single_minus_zero {
return '-0'
return '-0.'
}
if f.u == strconv.single_plus_zero {
return '0'
return '0.'
}
}
abs_x := f32_abs(x)
@@ -103,11 +103,11 @@ pub fn (x f32) str() string {
[inline]
pub fn (x f32) strg() string {
if x == 0 {
return '0'
return '0.0'
}
abs_x := f32_abs(x)
if abs_x >= 0.0001 && abs_x < 1.0e6 {
return strconv.f32_to_str_l_no_dot(x)
return strconv.f32_to_str_l_with_dot(x)
} else {
return strconv.ftoa_32(x)
}

View File

@@ -2,7 +2,7 @@ fn test_map_of_f32() {
mut m32 := map[f32]string{}
m32[1.0] = 'one'
println(m32)
assert '$m32' == r"{1.: 'one'}"
assert '$m32' == r"{1.0: 'one'}"
for k, v in m32 {
assert typeof(k).name == 'f32'
assert typeof(v).name == 'string'
@@ -17,7 +17,7 @@ fn test_map_of_f64() {
}
m64[1.0] = 'one'
println(m64)
assert '$m64' == r"{3.14: 'pi', 1.: 'one'}"
assert '$m64' == r"{3.14: 'pi', 1.0: 'one'}"
for k, v in m64 {
assert typeof(k).name == 'f64'
assert typeof(v).name == 'string'

View File

@@ -190,11 +190,11 @@ fn test_signed_cast() {
mut u := strconv.Float64u{
u: strconv.double_plus_zero
}
assert '${u.f:g}' == '0'
assert '${u.f:G}' == '0'
assert '${u.f:g}' == '0.0'
assert '${u.f:G}' == '0.0'
u.u = strconv.double_minus_zero
assert '${u.f:g}' == '0'
assert '${u.f:G}' == '0'
assert '${u.f:g}' == '0.0'
assert '${u.f:G}' == '0.0'
u.u = strconv.double_plus_infinity
assert '${u.f:g}' == '+inf'
assert '${u.f:G}' == '+INF'
@@ -206,11 +206,11 @@ fn test_signed_cast() {
mut u := strconv.Float32u{
u: strconv.single_plus_zero
}
assert '${u.f:g}' == '0'
assert '${u.f:G}' == '0'
assert '${u.f:g}' == '0.0'
assert '${u.f:G}' == '0.0'
u.u = strconv.single_minus_zero
assert '${u.f:g}' == '0'
assert '${u.f:G}' == '0'
assert '${u.f:g}' == '0.0'
assert '${u.f:G}' == '0.0'
u.u = strconv.single_plus_infinity
assert '${u.f:g}' == '+inf'
assert '${u.f:G}' == '+INF'