mirror of
https://github.com/vlang/v.git
synced 2025-09-13 22:42:26 +03:00
This commit is contained in:
parent
aa7de61e44
commit
a8e0c9efff
5 changed files with 76 additions and 13 deletions
|
@ -172,3 +172,35 @@ fn test_float_to_str() {
|
||||||
// assert ftoa.f64_to_str(0.3456789123456, 4)=="3.4568e-01"
|
// assert ftoa.f64_to_str(0.3456789123456, 4)=="3.4568e-01"
|
||||||
// assert ftoa.f32_to_str(0.345678, 3)=="3.457e-01"
|
// assert ftoa.f32_to_str(0.345678, 3)=="3.457e-01"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn test_issue_25141_1() {
|
||||||
|
assert strconv.ftoa_long_32(0.1234567901) == '0.12345679'
|
||||||
|
}
|
||||||
|
|
||||||
|
fn test_issue_25141_2() {
|
||||||
|
assert strconv.f32_to_str_l(math.f32_from_bits(1055100472)) == '0.44444442'
|
||||||
|
}
|
||||||
|
|
||||||
|
fn test_issue_25141_3() {
|
||||||
|
assert strconv.f32_to_str_l(math.f32_from_bits(1055100473)) == '0.44444445'
|
||||||
|
}
|
||||||
|
|
||||||
|
fn test_issue_25141_4() {
|
||||||
|
assert strconv.f32_to_str_l(math.f32_from_bits(1055100474)) == '0.44444448'
|
||||||
|
}
|
||||||
|
|
||||||
|
fn test_issue_25141_5() {
|
||||||
|
assert strconv.f32_to_str_l(math.f32_from_bits(1055100475)) == '0.4444445'
|
||||||
|
}
|
||||||
|
|
||||||
|
fn test_issue_25141_0001() {
|
||||||
|
assert strconv.f32_to_str_l(math.f32_from_bits(953267991)) == '0.0001'
|
||||||
|
assert strconv.f32_to_str_l(math.f32_from_bits(953267992)) == '0.000100000005'
|
||||||
|
assert strconv.f32_to_str_l(math.f32_from_bits(953267993)) == '0.00010000001'
|
||||||
|
assert strconv.f32_to_str_l(math.f32_from_bits(953267994)) == '0.00010000002'
|
||||||
|
assert strconv.f32_to_str_l(math.f32_from_bits(953267995)) == '0.00010000003'
|
||||||
|
assert strconv.f32_to_str_l(math.f32_from_bits(953267996)) == '0.000100000034'
|
||||||
|
assert strconv.f32_to_str_l(math.f32_from_bits(953267997)) == '0.00010000004'
|
||||||
|
assert strconv.f32_to_str_l(math.f32_from_bits(953267998)) == '0.00010000005'
|
||||||
|
assert strconv.f32_to_str_l(math.f32_from_bits(953267999)) == '0.000100000056'
|
||||||
|
}
|
||||||
|
|
35
vlib/strconv/f32_str_should_be_different_test.v
Normal file
35
vlib/strconv/f32_str_should_be_different_test.v
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
import math
|
||||||
|
|
||||||
|
fn test_around() {
|
||||||
|
// do not pass 0
|
||||||
|
for f in [f32(1e-12), 1e-9, 1e-6, 1e-4, 0.1, 0.2, 0.5, 1, 2, 5, 10, 1e6, 1e7, 1e9, 1e12] {
|
||||||
|
check_around(f, 1000)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn check_around(fval f32, total int) {
|
||||||
|
check(fval, total)
|
||||||
|
check(-fval, total)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn check(fval f32, total int) {
|
||||||
|
middle_bits := math.f32_bits(fval)
|
||||||
|
start_bits := middle_bits - u32(total / 2)
|
||||||
|
end_bits := middle_bits + u32(total / 2)
|
||||||
|
min, max := if start_bits < end_bits { start_bits, end_bits } else { end_bits, start_bits }
|
||||||
|
println('> check_around ${total} f32 values around fval: ${fval:26.12f}, middle_bits for fval: ${middle_bits:12} | min: ${min:12} | max: ${max:12}')
|
||||||
|
for ux in min .. max {
|
||||||
|
x := math.f32_from_bits(ux)
|
||||||
|
if math.is_nan(x) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
uy := ux - 1
|
||||||
|
y := math.f32_from_bits(uy)
|
||||||
|
if x == y {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
sx := x.str()
|
||||||
|
sy := y.str()
|
||||||
|
assert sx != sy, 'math.f32_from_bits(${ux})'
|
||||||
|
}
|
||||||
|
}
|
|
@ -19,7 +19,6 @@ https://github.com/cespare/ryu/tree/ba56a33f39e3bbbfa409095d0f9ae168a595feea
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// ftoa_64 returns a string in scientific notation with max 17 digits after the dot.
|
// ftoa_64 returns a string in scientific notation with max 17 digits after the dot.
|
||||||
//
|
|
||||||
// Example: assert strconv.ftoa_64(123.1234567891011121) == '1.2312345678910111e+02'
|
// Example: assert strconv.ftoa_64(123.1234567891011121) == '1.2312345678910111e+02'
|
||||||
@[inline]
|
@[inline]
|
||||||
pub fn ftoa_64(f f64) string {
|
pub fn ftoa_64(f f64) string {
|
||||||
|
@ -27,7 +26,6 @@ pub fn ftoa_64(f f64) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
// ftoa_long_64 returns `f` as a `string` in decimal notation with a maximum of 17 digits after the dot.
|
// ftoa_long_64 returns `f` as a `string` in decimal notation with a maximum of 17 digits after the dot.
|
||||||
//
|
|
||||||
// Example: assert strconv.f64_to_str_l(123.1234567891011121) == '123.12345678910111'
|
// Example: assert strconv.f64_to_str_l(123.1234567891011121) == '123.12345678910111'
|
||||||
@[inline]
|
@[inline]
|
||||||
pub fn ftoa_long_64(f f64) string {
|
pub fn ftoa_long_64(f f64) string {
|
||||||
|
@ -35,16 +33,14 @@ pub fn ftoa_long_64(f f64) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
// ftoa_32 returns a `string` in scientific notation with max 8 digits after the dot.
|
// ftoa_32 returns a `string` in scientific notation with max 8 digits after the dot.
|
||||||
//
|
|
||||||
// Example: assert strconv.ftoa_32(34.1234567) == '3.4123455e+01'
|
// Example: assert strconv.ftoa_32(34.1234567) == '3.4123455e+01'
|
||||||
@[inline]
|
@[inline]
|
||||||
pub fn ftoa_32(f f32) string {
|
pub fn ftoa_32(f f32) string {
|
||||||
return f32_to_str(f, 8)
|
return f32_to_str(f, 8)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ftoa_long_32 returns `f` as a `string` in decimal notation with a maximum of 6 digits after the dot.
|
// ftoa_long_32 returns `f` as a `string` in decimal notation with a maximum of 8 digits after the dot.
|
||||||
//
|
// Example: assert strconv.ftoa_long_32(0.1234567901) == '0.12345679'
|
||||||
// Example: assert strconv.ftoa_long_32(34.1234567) == '34.12346'
|
|
||||||
@[inline]
|
@[inline]
|
||||||
pub fn ftoa_long_32(f f32) string {
|
pub fn ftoa_long_32(f f32) string {
|
||||||
return f32_to_str_l(f)
|
return f32_to_str_l(f)
|
||||||
|
|
|
@ -23,24 +23,24 @@ f64 to string with string format
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// TODO: Investigate precision issues
|
// TODO: Investigate precision issues
|
||||||
// f32_to_str_l returns `f` as a `string` in decimal notation with a maximum of 6 digits after the dot.
|
// f32_to_str_l returns `f` as a `string` in decimal notation with a maximum of 8 digits after the dot.
|
||||||
//
|
// Example: assert strconv.f32_to_str_l(0.1234567891) == '0.12345679'
|
||||||
// Example: assert strconv.f32_to_str_l(34.1234567) == '34.12346'
|
// Example: assert strconv.f32_to_str_l(34.1234567891) == '34.123455'
|
||||||
@[manualfree]
|
@[manualfree]
|
||||||
pub fn f32_to_str_l(f f32) string {
|
pub fn f32_to_str_l(f f32) string {
|
||||||
s := f32_to_str(f, 6)
|
s := f32_to_str(f, 8)
|
||||||
res := fxx_to_str_l_parse(s)
|
res := fxx_to_str_l_parse(s)
|
||||||
unsafe { s.free() }
|
unsafe { s.free() }
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
// f32_to_str_l_with_dot returns `f` as a `string` in decimal notation with a maximum of 6 digits after the dot.
|
// f32_to_str_l_with_dot returns `f` as a `string` in decimal notation with a maximum of 8 digits after the dot.
|
||||||
// If the decimal digits after the dot are zero, a '.0' is appended for clarity.
|
// If the decimal digits after the dot are zero, a '.0' is appended for clarity.
|
||||||
//
|
//
|
||||||
// Example: assert strconv.f32_to_str_l_with_dot(34.) == '34.0'
|
// Example: assert strconv.f32_to_str_l_with_dot(34.) == '34.0'
|
||||||
@[manualfree]
|
@[manualfree]
|
||||||
pub fn f32_to_str_l_with_dot(f f32) string {
|
pub fn f32_to_str_l_with_dot(f f32) string {
|
||||||
s := f32_to_str(f, 6)
|
s := f32_to_str(f, 8)
|
||||||
res := fxx_to_str_l_parse_with_dot(s)
|
res := fxx_to_str_l_parse_with_dot(s)
|
||||||
unsafe { s.free() }
|
unsafe { s.free() }
|
||||||
return res
|
return res
|
||||||
|
|
|
@ -4,7 +4,7 @@ fn test() { println('foo') } fn test2(a int) { println(a) }
|
||||||
test()
|
test()
|
||||||
test2(123)
|
test2(123)
|
||||||
===output===
|
===output===
|
||||||
-0.2623749
|
-0.26237485
|
||||||
0.7376251
|
0.7376251
|
||||||
foo
|
foo
|
||||||
123
|
123
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue