strconv: float functions cleaning and speed optimization (#10076)
parent
3b062388ba
commit
d60a55d30b
|
@ -1,6 +1,5 @@
|
||||||
module strconv
|
module strconv
|
||||||
|
/*=============================================================================
|
||||||
/*
|
|
||||||
|
|
||||||
f32 to string
|
f32 to string
|
||||||
|
|
||||||
|
@ -18,8 +17,7 @@ Pages 270–282 https://doi.org/10.1145/3192366.3192369
|
||||||
inspired by the Go version here:
|
inspired by the Go version here:
|
||||||
https://github.com/cespare/ryu/tree/ba56a33f39e3bbbfa409095d0f9ae168a595feea
|
https://github.com/cespare/ryu/tree/ba56a33f39e3bbbfa409095d0f9ae168a595feea
|
||||||
|
|
||||||
*/
|
=============================================================================*/
|
||||||
|
|
||||||
|
|
||||||
// pow of ten table used by n_digit reduction
|
// pow of ten table used by n_digit reduction
|
||||||
const(
|
const(
|
||||||
|
@ -39,11 +37,9 @@ const(
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
|
|
||||||
/*
|
//=============================================================================
|
||||||
|
// Conversion Functions
|
||||||
Conversion Functions
|
//=============================================================================
|
||||||
|
|
||||||
*/
|
|
||||||
const(
|
const(
|
||||||
mantbits32 = u32(23)
|
mantbits32 = u32(23)
|
||||||
expbits32 = u32(8)
|
expbits32 = u32(8)
|
||||||
|
@ -53,11 +49,13 @@ const(
|
||||||
|
|
||||||
// max 46 char
|
// max 46 char
|
||||||
// -3.40282346638528859811704183484516925440e+38
|
// -3.40282346638528859811704183484516925440e+38
|
||||||
|
[direct_array_access]
|
||||||
pub fn (d Dec32) get_string_32(neg bool, i_n_digit int, i_pad_digit int) string {
|
pub fn (d Dec32) get_string_32(neg bool, i_n_digit int, i_pad_digit int) string {
|
||||||
n_digit := i_n_digit + 1
|
n_digit := i_n_digit + 1
|
||||||
pad_digit := i_pad_digit + 1
|
pad_digit := i_pad_digit + 1
|
||||||
mut out := d.m
|
mut out := d.m
|
||||||
mut out_len := decimal_len_32(out)
|
//mut out_len := decimal_len_32(out)
|
||||||
|
mut out_len := dec_digits(out)
|
||||||
out_len_original := out_len
|
out_len_original := out_len
|
||||||
|
|
||||||
mut fw_zeros := 0
|
mut fw_zeros := 0
|
||||||
|
@ -119,15 +117,6 @@ pub fn (d Dec32) get_string_32(neg bool, i_n_digit int, i_pad_digit int) string
|
||||||
fw_zeros--
|
fw_zeros--
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
x=0
|
|
||||||
for x<buf.len {
|
|
||||||
C.printf("d:%c\n",buf[x])
|
|
||||||
x++
|
|
||||||
}
|
|
||||||
C.printf("\n")
|
|
||||||
*/
|
|
||||||
|
|
||||||
buf[i]=`e`
|
buf[i]=`e`
|
||||||
i++
|
i++
|
||||||
|
|
||||||
|
@ -150,13 +139,6 @@ pub fn (d Dec32) get_string_32(neg bool, i_n_digit int, i_pad_digit int) string
|
||||||
i++
|
i++
|
||||||
buf[i]=0
|
buf[i]=0
|
||||||
|
|
||||||
/*
|
|
||||||
x=0
|
|
||||||
for x<buf.len {
|
|
||||||
C.printf("d:%c\n",buf[x])
|
|
||||||
x++
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
return unsafe {
|
return unsafe {
|
||||||
tos(byteptr(&buf[0]), i)
|
tos(byteptr(&buf[0]), i)
|
||||||
}
|
}
|
||||||
|
@ -327,6 +309,10 @@ pub fn f32_to_decimal(mant u32, exp u32) Dec32 {
|
||||||
return Dec32{m: out e: e10 + removed}
|
return Dec32{m: out e: e10 + removed}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//=============================================================================
|
||||||
|
// String Functions
|
||||||
|
//=============================================================================
|
||||||
|
|
||||||
// f32_to_str return a string in scientific notation with max n_digit after the dot
|
// f32_to_str return a string in scientific notation with max n_digit after the dot
|
||||||
pub fn f32_to_str(f f32, n_digit int) string {
|
pub fn f32_to_str(f f32, n_digit int) string {
|
||||||
mut u1 := Uf32{}
|
mut u1 := Uf32{}
|
||||||
|
|
|
@ -1,8 +1,7 @@
|
||||||
module strconv
|
module strconv
|
||||||
|
/*=============================================================================
|
||||||
|
|
||||||
/*
|
f64 to string
|
||||||
|
|
||||||
f32 to string
|
|
||||||
|
|
||||||
Copyright (c) 2019-2021 Dario Deledda. All rights reserved.
|
Copyright (c) 2019-2021 Dario Deledda. All rights reserved.
|
||||||
Use of this source code is governed by an MIT license
|
Use of this source code is governed by an MIT license
|
||||||
|
@ -18,7 +17,7 @@ Pages 270–282 https://doi.org/10.1145/3192366.3192369
|
||||||
inspired by the Go version here:
|
inspired by the Go version here:
|
||||||
https://github.com/cespare/ryu/tree/ba56a33f39e3bbbfa409095d0f9ae168a595feea
|
https://github.com/cespare/ryu/tree/ba56a33f39e3bbbfa409095d0f9ae168a595feea
|
||||||
|
|
||||||
*/
|
=============================================================================*/
|
||||||
|
|
||||||
// pow of ten table used by n_digit reduction
|
// pow of ten table used by n_digit reduction
|
||||||
const(
|
const(
|
||||||
|
@ -46,11 +45,9 @@ const(
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
|
|
||||||
/*
|
//=============================================================================
|
||||||
|
// Conversion Functions
|
||||||
Conversion Functions
|
//=============================================================================
|
||||||
|
|
||||||
*/
|
|
||||||
const(
|
const(
|
||||||
mantbits64 = u32(52)
|
mantbits64 = u32(52)
|
||||||
expbits64 = u32(11)
|
expbits64 = u32(11)
|
||||||
|
@ -58,12 +55,14 @@ const(
|
||||||
maxexp64 = 2047
|
maxexp64 = 2047
|
||||||
)
|
)
|
||||||
|
|
||||||
|
[direct_array_access]
|
||||||
fn (d Dec64) get_string_64(neg bool, i_n_digit int, i_pad_digit int) string {
|
fn (d Dec64) get_string_64(neg bool, i_n_digit int, i_pad_digit int) string {
|
||||||
mut n_digit := i_n_digit + 1
|
mut n_digit := i_n_digit + 1
|
||||||
pad_digit := i_pad_digit + 1
|
pad_digit := i_pad_digit + 1
|
||||||
mut out := d.m
|
mut out := d.m
|
||||||
mut d_exp := d.e
|
mut d_exp := d.e
|
||||||
mut out_len := decimal_len_64(out)
|
// mut out_len := decimal_len_64(out)
|
||||||
|
mut out_len := dec_digits(out)
|
||||||
out_len_original := out_len
|
out_len_original := out_len
|
||||||
|
|
||||||
mut fw_zeros := 0
|
mut fw_zeros := 0
|
||||||
|
@ -135,15 +134,6 @@ fn (d Dec64) get_string_64(neg bool, i_n_digit int, i_pad_digit int) string {
|
||||||
fw_zeros--
|
fw_zeros--
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
x=0
|
|
||||||
for x<buf.len {
|
|
||||||
C.printf("d:%c\n",buf[x])
|
|
||||||
x++
|
|
||||||
}
|
|
||||||
C.printf("\n")
|
|
||||||
*/
|
|
||||||
|
|
||||||
buf[i]=`e`
|
buf[i]=`e`
|
||||||
i++
|
i++
|
||||||
|
|
||||||
|
@ -172,14 +162,6 @@ fn (d Dec64) get_string_64(neg bool, i_n_digit int, i_pad_digit int) string {
|
||||||
i++
|
i++
|
||||||
buf[i]=0
|
buf[i]=0
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
x=0
|
|
||||||
for x<buf.len {
|
|
||||||
C.printf("d:%c\n",buf[x])
|
|
||||||
x++
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
return unsafe {
|
return unsafe {
|
||||||
tos(byteptr(&buf[0]), i)
|
tos(byteptr(&buf[0]), i)
|
||||||
}
|
}
|
||||||
|
@ -376,6 +358,10 @@ fn f64_to_decimal(mant u64, exp u64) Dec64 {
|
||||||
return Dec64{m: out, e: e10 + removed}
|
return Dec64{m: out, e: e10 + removed}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//=============================================================================
|
||||||
|
// String Functions
|
||||||
|
//=============================================================================
|
||||||
|
|
||||||
// f64_to_str return a string in scientific notation with max n_digit after the dot
|
// f64_to_str return a string in scientific notation with max n_digit after the dot
|
||||||
pub fn f64_to_str(f f64, n_digit int) string {
|
pub fn f64_to_str(f f64, n_digit int) string {
|
||||||
mut u1 := Uf64{}
|
mut u1 := Uf64{}
|
||||||
|
|
Loading…
Reference in New Issue