float: make default string representations for floats become alike
parent
1c68417918
commit
770132ff37
|
@ -7,23 +7,22 @@ import strconv.ftoa
|
||||||
|
|
||||||
#include <float.h>
|
#include <float.h>
|
||||||
// ----- f64 to string functions -----
|
// ----- 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]
|
[inline]
|
||||||
pub fn (d f64) str() string {
|
pub fn (x f64) str() string {
|
||||||
return ftoa.ftoa_64(d)
|
|
||||||
}
|
|
||||||
|
|
||||||
[inline]
|
|
||||||
pub fn (d any_float) str() string {
|
|
||||||
x := f64(d)
|
|
||||||
abs_x := f64_abs(x)
|
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)
|
return ftoa.f64_to_str_l(x)
|
||||||
} else {
|
} else {
|
||||||
return ftoa.ftoa_64(x)
|
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
|
// return a string of the input f64 in scientific notation with digit_num deciamals displayed, max 17 digits
|
||||||
[inline]
|
[inline]
|
||||||
pub fn (x f64) strsci(digit_num int) string {
|
pub fn (x f64) strsci(digit_num int) string {
|
||||||
|
@ -43,10 +42,15 @@ pub fn (x f64) strlong() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
// ----- f32 to string functions -----
|
// ----- 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]
|
[inline]
|
||||||
pub fn (d f32) str() string {
|
pub fn (x f32) str() string {
|
||||||
return ftoa.ftoa_32(d)
|
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
|
// 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 -----
|
// ----- C functions -----
|
||||||
[inline]
|
[inline]
|
||||||
fn f32_abs(a f32) f32 {
|
pub fn f32_abs(a f32) f32 {
|
||||||
return if a < 0 {
|
return if a < 0 {
|
||||||
-a
|
-a
|
||||||
} else {
|
} else {
|
||||||
|
@ -88,7 +92,7 @@ fn f64_abs(a f64) f64 {
|
||||||
|
|
||||||
|
|
||||||
[inline]
|
[inline]
|
||||||
fn f32_max(a, b f32) f32 {
|
pub fn f32_max(a, b f32) f32 {
|
||||||
return if a > b {
|
return if a > b {
|
||||||
a
|
a
|
||||||
} else {
|
} else {
|
||||||
|
@ -97,7 +101,7 @@ fn f32_max(a, b f32) f32 {
|
||||||
}
|
}
|
||||||
|
|
||||||
[inline]
|
[inline]
|
||||||
fn f32_min(a, b f32) f32 {
|
pub fn f32_min(a, b f32) f32 {
|
||||||
return if a < b {
|
return if a < b {
|
||||||
a
|
a
|
||||||
} else {
|
} else {
|
||||||
|
@ -106,7 +110,7 @@ fn f32_min(a, b f32) f32 {
|
||||||
}
|
}
|
||||||
|
|
||||||
[inline]
|
[inline]
|
||||||
fn f64_max(a, b f64) f64 {
|
pub fn f64_max(a, b f64) f64 {
|
||||||
return if a > b {
|
return if a > b {
|
||||||
a
|
a
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -21,8 +21,17 @@ fn test_float_lit_call_method() {
|
||||||
x4 := .003e2.str()
|
x4 := .003e2.str()
|
||||||
assert x4 == '0.3'
|
assert x4 == '0.3'
|
||||||
x5 := 2.e-3.str()
|
x5 := 2.e-3.str()
|
||||||
assert x5 == '2.e-03'
|
assert x5 == '0.002'
|
||||||
x6 := 5.0.str()
|
x6 := 5.0.str()
|
||||||
assert x6 == '5.'
|
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.
|
// 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'
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue