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

atof: lots of fixes

* removed sprintf for f64 and f32 use

* removed all pointers from the code, used unions instead

* solved module name problem

* fixed tests on vlib/math

* fix for alpine-linux math test

* small fix on byte allocation for ftoa
This commit is contained in:
penguindark
2020-02-26 12:14:06 +01:00
committed by GitHub
parent c4e83faa57
commit 39429f7ac9
9 changed files with 180 additions and 137 deletions

View File

@@ -1,4 +1,5 @@
import math.stats as stats
import math
fn test_freq() {
// Tests were also verified on Wolfram Alpha
@@ -11,20 +12,27 @@ fn test_freq() {
assert o == 0
}
fn tst_res(str1 string, str2 string) bool {
if (math.abs(str1.f64() - str2.f64())) < 1e-5 {
return true
}
return false
}
fn test_mean() {
// Tests were also verified on Wolfram Alpha
mut data := [f64(10.0),f64(4.45),f64(5.9),f64(2.7)]
mut o := stats.mean(data)
// Some issue with precision comparison in f64 using == operator hence serializing to string
assert o.str().eq('5.762500')
assert tst_res(o.str(), '5.762500')
data = [f64(-3.0),f64(67.31),f64(4.4),f64(1.89)]
o = stats.mean(data)
// Some issue with precision comparison in f64 using == operator hence serializing to string
assert o.str().eq('17.650000')
assert tst_res(o.str(), '17.650000')
data = [f64(12.0),f64(7.88),f64(76.122),f64(54.83)]
o = stats.mean(data)
// Some issue with precision comparison in f64 using == operator hence serializing to string
assert o.str().eq('37.708000')
assert tst_res(o.str(), '37.708000')
}
fn test_geometric_mean() {
@@ -32,7 +40,7 @@ fn test_geometric_mean() {
mut data := [f64(10.0),f64(4.45),f64(5.9),f64(2.7)]
mut o := stats.geometric_mean(data)
// Some issue with precision comparison in f64 using == operator hence serializing to string
assert o.str().eq('5.159932')
assert tst_res(o.str(),'5.15993')
data = [f64(-3.0),f64(67.31),f64(4.4),f64(1.89)]
o = stats.geometric_mean(data)
// Some issue with precision comparison in f64 using == operator hence serializing to string
@@ -40,7 +48,7 @@ fn test_geometric_mean() {
data = [f64(12.0),f64(7.88),f64(76.122),f64(54.83)]
o = stats.geometric_mean(data)
// Some issue with precision comparison in f64 using == operator hence serializing to string
assert o.str().eq('25.064496')
assert tst_res(o.str(),'25.064496')
}
fn test_harmonic_mean() {
@@ -48,15 +56,15 @@ fn test_harmonic_mean() {
mut data := [f64(10.0),f64(4.45),f64(5.9),f64(2.7)]
mut o := stats.harmonic_mean(data)
// Some issue with precision comparison in f64 using == operator hence serializing to string
assert o.str().eq('4.626519')
assert tst_res(o.str(), '4.626519')
data = [f64(-3.0),f64(67.31),f64(4.4),f64(1.89)]
o = stats.harmonic_mean(data)
// Some issue with precision comparison in f64 using == operator hence serializing to string
assert o.str().eq('9.134577')
assert tst_res(o.str(), '9.134577')
data = [f64(12.0),f64(7.88),f64(76.122),f64(54.83)]
o = stats.harmonic_mean(data)
// Some issue with precision comparison in f64 using == operator hence serializing to string
assert o.str().eq('16.555477')
assert tst_res(o.str(), '16.555477')
}
fn test_median() {
@@ -67,15 +75,15 @@ fn test_median() {
mut data := [f64(2.7),f64(4.45),f64(5.9),f64(10.0)]
mut o := stats.median(data)
// Some issue with precision comparison in f64 using == operator hence serializing to string
assert o.str().eq('5.175000')
assert tst_res(o.str(), '5.175000')
data = [f64(-3.0),f64(1.89),f64(4.4),f64(67.31)]
o = stats.median(data)
// Some issue with precision comparison in f64 using == operator hence serializing to string
assert o.str().eq('3.145000')
assert tst_res(o.str(), '3.145000')
data = [f64(7.88),f64(12.0),f64(54.83),f64(76.122)]
o = stats.median(data)
// Some issue with precision comparison in f64 using == operator hence serializing to string
assert o.str().eq('33.415000')
assert tst_res(o.str(), '33.415000')
// Odd
data = [f64(2.7),f64(4.45),f64(5.9),f64(10.0),f64(22)]
@@ -108,15 +116,15 @@ fn test_rms() {
mut data := [f64(10.0),f64(4.45),f64(5.9),f64(2.7)]
mut o := stats.rms(data)
// Some issue with precision comparison in f64 using == operator hence serializing to string
assert o.str().eq('6.362046')
assert tst_res(o.str(), '6.362046')
data = [f64(-3.0),f64(67.31),f64(4.4),f64(1.89)]
o = stats.rms(data)
// Some issue with precision comparison in f64 using == operator hence serializing to string
assert o.str().eq('33.773393')
assert tst_res(o.str(), '33.773393')
data = [f64(12.0),f64(7.88),f64(76.122),f64(54.83)]
o = stats.rms(data)
// Some issue with precision comparison in f64 using == operator hence serializing to string
assert o.str().eq('47.452561')
assert tst_res(o.str(), '47.452561')
}
fn test_population_variance() {
@@ -124,15 +132,15 @@ fn test_population_variance() {
mut data := [f64(10.0),f64(4.45),f64(5.9),f64(2.7)]
mut o := stats.population_variance(data)
// Some issue with precision comparison in f64 using == operator hence serializing to string
assert o.str().eq('7.269219')
assert tst_res(o.str(), '7.269219')
data = [f64(-3.0),f64(67.31),f64(4.4),f64(1.89)]
o = stats.population_variance(data)
// Some issue with precision comparison in f64 using == operator hence serializing to string
assert o.str().eq('829.119550')
assert tst_res(o.str(), '829.119550')
data = [f64(12.0),f64(7.88),f64(76.122),f64(54.83)]
o = stats.population_variance(data)
// Some issue with precision comparison in f64 using == operator hence serializing to string
assert o.str().eq('829.852282')
assert tst_res(o.str(), '829.852282')
}
fn test_sample_variance() {
@@ -140,15 +148,15 @@ fn test_sample_variance() {
mut data := [f64(10.0),f64(4.45),f64(5.9),f64(2.7)]
mut o := stats.sample_variance(data)
// Some issue with precision comparison in f64 using == operator hence serializing to string
assert o.str().eq('9.692292')
assert tst_res(o.str(), '9.692292')
data = [f64(-3.0),f64(67.31),f64(4.4),f64(1.89)]
o = stats.sample_variance(data)
// Some issue with precision comparison in f64 using == operator hence serializing to string
assert o.str().eq('1105.492733')
assert tst_res(o.str(), '1105.492733')
data = [f64(12.0),f64(7.88),f64(76.122),f64(54.83)]
o = stats.sample_variance(data)
// Some issue with precision comparison in f64 using == operator hence serializing to string
assert o.str().eq('1106.469709')
assert tst_res(o.str(), '1106.469709')
}
fn test_population_stddev() {
@@ -156,15 +164,15 @@ fn test_population_stddev() {
mut data := [f64(10.0),f64(4.45),f64(5.9),f64(2.7)]
mut o := stats.population_stddev(data)
// Some issue with precision comparison in f64 using == operator hence serializing to string
assert o.str().eq('2.696149')
assert tst_res(o.str(), '2.696149')
data = [f64(-3.0),f64(67.31),f64(4.4),f64(1.89)]
o = stats.population_stddev(data)
// Some issue with precision comparison in f64 using == operator hence serializing to string
assert o.str().eq('28.794436')
assert tst_res(o.str(), '28.794436')
data = [f64(12.0),f64(7.88),f64(76.122),f64(54.83)]
o = stats.population_stddev(data)
// Some issue with precision comparison in f64 using == operator hence serializing to string
assert o.str().eq('28.807157')
assert tst_res(o.str(), '28.807157')
}
fn test_sample_stddev() {
@@ -172,15 +180,15 @@ fn test_sample_stddev() {
mut data := [f64(10.0),f64(4.45),f64(5.9),f64(2.7)]
mut o := stats.sample_stddev(data)
// Some issue with precision comparison in f64 using == operator hence serializing to string
assert o.str().eq('3.113245')
assert tst_res(o.str(), '3.113245')
data = [f64(-3.0),f64(67.31),f64(4.4),f64(1.89)]
o = stats.sample_stddev(data)
// Some issue with precision comparison in f64 using == operator hence serializing to string
assert o.str().eq('33.248951')
assert tst_res(o.str(), '33.248951')
data = [f64(12.0),f64(7.88),f64(76.122),f64(54.83)]
o = stats.sample_stddev(data)
// Some issue with precision comparison in f64 using == operator hence serializing to string
assert o.str().eq('33.263639')
assert tst_res(o.str(), '33.263639')
}
fn test_mean_absdev() {
@@ -188,15 +196,15 @@ fn test_mean_absdev() {
mut data := [f64(10.0),f64(4.45),f64(5.9),f64(2.7)]
mut o := stats.mean_absdev(data)
// Some issue with precision comparison in f64 using == operator hence serializing to string
assert o.str().eq('2.187500')
assert tst_res(o.str(), '2.187500')
data = [f64(-3.0),f64(67.31),f64(4.4),f64(1.89)]
o = stats.mean_absdev(data)
// Some issue with precision comparison in f64 using == operator hence serializing to string
assert o.str().eq('24.830000')
assert tst_res(o.str(), '24.830000')
data = [f64(12.0),f64(7.88),f64(76.122),f64(54.83)]
o = stats.mean_absdev(data)
// Some issue with precision comparison in f64 using == operator hence serializing to string
assert o.str().eq('27.768000')
assert tst_res(o.str(), '27.768000')
}
fn test_min() {