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

@@ -19,6 +19,13 @@
*
**********************************************************************/
module strconv
union Float64u {
mut:
f f64
u u64 = u64(0)
}
/**********************************************************************
*
* 96 bit operation utilities
@@ -149,25 +156,6 @@ fn is_exp(x byte) bool {
return (x == `E` || x == `e`) == true
}
/*
// return a string of the input f64 in scientific notation with digit_num digits displayed
pub fn strsci(x f64, digit_num int) string{
buf := malloc(digit_num*2+2)// TODO
conf_str := '%0.'+digit_num.str()+'e'
C.sprintf(charptr(buf), charptr(conf_str.str), x)
tmpstr := tos(buf, vstrlen(buf))
return tmpstr
}
// return a long string of the input f64, max
pub fn strlong(x f64) string {
buf := malloc(18+32)// TODO
C.sprintf(charptr(buf),"%0.30lf",x)
tmpstr := tos(buf, vstrlen(buf))
return tmpstr
}
*/
/**********************************************************************
*
* Support struct
@@ -545,29 +533,30 @@ pub fn atof64(s string) f64 {
mut pn := PrepNumber{
}
mut res_parsing := 0
mut result := f64(0)
result = f64(0.0)
mut res_ptr := *u64(&result)
mut res := Float64u{}
res_parsing,pn = parser(s + ' ') // TODO: need an extra char for now
// println(pn)
match res_parsing {
PARSER_OK {
*res_ptr = converter(mut pn)
res.u = converter(mut pn)
}
PARSER_PZERO {
*res_ptr = DOUBLE_PLUS_ZERO
res.u = DOUBLE_PLUS_ZERO
}
PARSER_MZERO {
*res_ptr = DOUBLE_MINUS_ZERO
res.u = DOUBLE_MINUS_ZERO
}
PARSER_PINF {
*res_ptr = DOUBLE_PLUS_INFINITY
res.u = DOUBLE_PLUS_INFINITY
}
PARSER_MINF {
*res_ptr = DOUBLE_MINUS_INFINITY
res.u = DOUBLE_MINUS_INFINITY
}
else {
}}
return result
return res.f
}

View File

@@ -3,7 +3,6 @@
* String to float Test
*
**********************************************************************/
import (
strconv
strconv.atofq
@@ -41,15 +40,19 @@ fn test_atof() {
// slow atof
assert strconv.atof64(src_num_str[c]).strlong() == x.strlong()
// quick atof
mut s1 := (atofq.atof_quick(src_num_str[c]).str())
s1 = s1[..src_num_str[c].len]
mut s2 := (x.str())
s2 = s2[..src_num_str[c].len]
assert s1 == s2
delta := s1.f64() - s2.f64()
//println("$s1 $s2 $delta")
assert delta < f64(1e-16)
// test C.atof
assert x.strsci(18) == f64(C.atof(src_num_str[c].str)).strsci(18)
n1 := x.strsci(18)
n2 := f64(C.atof(src_num_str[c].str)).strsci(18)
//println("$n1 $n2")
assert n1 == n2
}
// check conversion case 2 string <==> f64
@@ -71,4 +74,5 @@ fn test_atof() {
// DOUBLE_MINUS_ZERO
f1=-0.0
assert *ptr == u64(0x8000000000000000)
//println("DONE!")
}

View File

@@ -18,17 +18,23 @@
module atofq
import strconv
// same used in atof, here only for references
// const(
// DOUBLE_PLUS_ZERO = u64(0x0000000000000000)
// DOUBLE_MINUS_ZERO = 0x8000000000000000
// DOUBLE_PLUS_INFINITY = 0x7FF0000000000000
// DOUBLE_MINUS_INFINITY = 0xFFF0000000000000
const(
DOUBLE_PLUS_ZERO = u64(0x0000000000000000)
DOUBLE_MINUS_ZERO = 0x8000000000000000
DOUBLE_PLUS_INFINITY = 0x7FF0000000000000
DOUBLE_MINUS_INFINITY = 0xFFF0000000000000
)
union Float64u {
mut:
f f64
u u64 = u64(0)
}
// atof_quick return a f64 number from a string in a quick way
pub fn atof_quick(s string) f64 {
mut f := f64(0.0) // result
mut f := Float64u{} // result
mut sign := f64(1.0) // result sign
mut i := 0 // index
// skip white spaces
@@ -47,34 +53,32 @@ pub fn atof_quick(s string) f64 {
}
// infinite
if s[i] == `i` && i + 2 < s.len && s[i + 1] == `n` && s[i + 2] == `f` {
mut d := *u64(&f)
if sign > 0.0 {
*d = strconv.DOUBLE_PLUS_INFINITY
f.u = DOUBLE_PLUS_INFINITY
}
else {
*d = strconv.DOUBLE_MINUS_INFINITY
f.u = DOUBLE_MINUS_INFINITY
}
return f
return f.f
}
// skip zeros
for i < s.len && s[i] == `0` {
i++
// we have a zero, manage it
if i >= s.len {
mut d := *u64(&f)
if sign > 0.0 {
*d = strconv.DOUBLE_PLUS_ZERO
f.u = DOUBLE_PLUS_ZERO
}
else {
*d = strconv.DOUBLE_MINUS_ZERO
f.u = DOUBLE_MINUS_ZERO
}
return f
return f.f
}
}
// integer part
for i < s.len && (s[i] >= `0` && s[i] <= `9`) {
f *= f64(10.0)
f += f64(s[i] - `0`)
f.f *= f64(10.0)
f.f += f64(s[i] - `0`)
i++
}
// decimal point
@@ -82,7 +86,7 @@ pub fn atof_quick(s string) f64 {
i++
mut frac_mul := f64(0.1)
for i < s.len && (s[i] >= `0` && s[i] <= `9`) {
f += f64(s[i] - `0`) * frac_mul
f.f += f64(s[i] - `0`) * frac_mul
frac_mul *= f64(0.1)
i++
}
@@ -113,41 +117,36 @@ pub fn atof_quick(s string) f64 {
}
if exp_sign == 1 {
if exp > pos_exp.len {
mut d := *u64(&f)
if sign > 0 {
*d = strconv.DOUBLE_PLUS_INFINITY
f.u = DOUBLE_PLUS_INFINITY
}
else {
*d = strconv.DOUBLE_MINUS_INFINITY
f.u = DOUBLE_MINUS_INFINITY
}
return f
return f.f
}
tmp_mul := f64(0.0)
mut ptr_d := *u64(&tmp_mul)
*ptr_d = pos_exp[exp]
tmp_mul := Float64u{u: pos_exp[exp]}
// C.printf("exp: %d [0x%016llx] %f,",exp,pos_exp[exp],tmp_mul)
f = f * tmp_mul
f.f = f.f * tmp_mul.f
}
else {
if exp > neg_exp.len {
mut d := *u64(&f)
if (sign > 0) {
*d = strconv.DOUBLE_PLUS_ZERO
f.u = DOUBLE_PLUS_ZERO
}
else {
*d = strconv.DOUBLE_MINUS_ZERO
f.u = DOUBLE_MINUS_ZERO
}
return f
return f.f
}
tmp_mul := f64(0.0)
mut ptr_d := *u64(&tmp_mul)
*ptr_d = neg_exp[exp]
tmp_mul := Float64u{u: neg_exp[exp]}
// C.printf("exp: %d [0x%016llx] %f,",exp,pos_exp[exp],tmp_mul)
f = f * tmp_mul
f.f = f.f * tmp_mul.f
}
}
f = f * sign
return f
f.f = f.f * sign
return f.f
}
const (

View File

@@ -59,8 +59,8 @@ test_cases_f32 = [
exp_result_f32 = [
"0e+00",
"-0e+00",
"NaN",
"NaN",
"nan",
"nan",
"+inf",
"-inf",
"1.e+00",
@@ -111,8 +111,8 @@ test_cases_f64 = [
exp_result_f64 = [
"0e+00",
"-0e+00",
"NaN",
"NaN",
"nan",
"nan",
"+inf",
"-inf",
"1.e+00",
@@ -141,7 +141,7 @@ exp_result_f64 = [
fn test_float_to_str(){
// test f32
for c,x in test_cases_f32 {
s := ftoa.f32_to_str(x,8)
s := f32_to_str(x,8)
s1 := exp_result_f32[c]
//println("$s1 $s")
assert s == s1
@@ -149,7 +149,7 @@ fn test_float_to_str(){
// test f64
for c,x in test_cases_f64 {
s := ftoa.f64_to_str(x,17)
s := f64_to_str(x,17)
s1 := exp_result_f64[c]
//println("$s1 $s")
assert s == s1
@@ -157,11 +157,11 @@ fn test_float_to_str(){
// test long format
for exp := 1 ; exp < 120 ; exp++ {
a :=ftoa.f64_to_str_l(("1e"+exp.str()).f64())
a := f64_to_str_l(("1e"+exp.str()).f64())
//println(a)
assert a.len == exp + 1
b :=ftoa.f64_to_str_l(("1e-"+exp.str()).f64())
b := f64_to_str_l(("1e-"+exp.str()).f64())
//println(b)
assert b.len == exp + 2
}

View File

@@ -58,7 +58,7 @@ fn bool_to_u64(b bool) u64 {
fn get_string_special(neg bool, expZero bool, mantZero bool) string {
if !mantZero {
return "NaN"
return "nan"
}
if !expZero {
if neg {
@@ -230,7 +230,7 @@ pub fn f32_to_str_l(f f64) string {
// f64_to_str_l return a string with the f64 converted in a strign in decimal notation
pub fn f64_to_str_l(f f64) string {
s := ftoa.f64_to_str(f,18)
s := f64_to_str(f,18)
// check for +inf -inf Nan
if s.len > 2 && (s[0] == `N` || s[1] == `i`) {
@@ -239,7 +239,7 @@ pub fn f64_to_str_l(f f64) string {
m_sgn_flag := false
mut sgn := 1
mut b := [32]byte
mut b := [18+8]byte
mut d_pos := 1
mut i := 0
mut i1 := 0