2020-10-07 11:06:52 +03:00
|
|
|
module strconv
|
2020-06-08 00:04:23 +03:00
|
|
|
|
2022-04-11 12:01:47 +03:00
|
|
|
// Copyright (c) 2019-2022 Dario Deledda. All rights reserved.
|
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
//
|
|
|
|
// This file contains utilities for converting a string to a f64 variable.
|
|
|
|
// IEEE 754 standard is used.
|
|
|
|
// Know limitation: limited to 18 significant digits
|
|
|
|
//
|
|
|
|
// The code is inspired by:
|
|
|
|
// Grzegorz Kraszewski krashan@teleinfo.pb.edu.pl
|
|
|
|
// URL: http://krashan.ppa.pl/articles/stringtofloat/
|
|
|
|
// Original license: MIT
|
|
|
|
// 96 bit operation utilities
|
|
|
|
//
|
|
|
|
// Note: when u128 will be available, these function can be refactored.
|
|
|
|
|
|
|
|
// f32 constants
|
|
|
|
pub const (
|
|
|
|
single_plus_zero = u32(0x0000_0000)
|
|
|
|
single_minus_zero = u32(0x8000_0000)
|
|
|
|
single_plus_infinity = u32(0x7F80_0000)
|
|
|
|
single_minus_infinity = u32(0xFF80_0000)
|
|
|
|
)
|
2020-06-08 00:04:23 +03:00
|
|
|
|
2022-04-11 12:01:47 +03:00
|
|
|
// f64 constants
|
|
|
|
pub const (
|
|
|
|
digits = 18
|
|
|
|
double_plus_zero = u64(0x0000000000000000)
|
|
|
|
double_minus_zero = u64(0x8000000000000000)
|
|
|
|
double_plus_infinity = u64(0x7FF0000000000000)
|
|
|
|
double_minus_infinity = u64(0xFFF0000000000000)
|
|
|
|
)
|
2020-06-08 00:04:23 +03:00
|
|
|
|
2022-04-11 12:01:47 +03:00
|
|
|
// char constants
|
|
|
|
pub const (
|
|
|
|
c_dpoint = `.`
|
|
|
|
c_plus = `+`
|
|
|
|
c_minus = `-`
|
|
|
|
c_zero = `0`
|
|
|
|
c_nine = `9`
|
|
|
|
c_ten = u32(10)
|
|
|
|
)
|
2019-12-20 00:29:37 +03:00
|
|
|
|
2019-12-17 01:07:13 +03:00
|
|
|
// right logical shift 96 bit
|
2021-04-28 07:42:22 +03:00
|
|
|
fn lsr96(s2 u32, s1 u32, s0 u32) (u32, u32, u32) {
|
2019-12-17 01:07:13 +03:00
|
|
|
mut r0 := u32(0)
|
|
|
|
mut r1 := u32(0)
|
|
|
|
mut r2 := u32(0)
|
2021-04-28 07:42:22 +03:00
|
|
|
r0 = (s0 >> 1) | ((s1 & u32(1)) << 31)
|
|
|
|
r1 = (s1 >> 1) | ((s2 & u32(1)) << 31)
|
|
|
|
r2 = s2 >> 1
|
|
|
|
return r2, r1, r0
|
2019-12-17 01:07:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// left logical shift 96 bit
|
2021-04-28 07:42:22 +03:00
|
|
|
fn lsl96(s2 u32, s1 u32, s0 u32) (u32, u32, u32) {
|
2019-12-17 01:07:13 +03:00
|
|
|
mut r0 := u32(0)
|
|
|
|
mut r1 := u32(0)
|
|
|
|
mut r2 := u32(0)
|
2021-04-28 07:42:22 +03:00
|
|
|
r2 = (s2 << 1) | ((s1 & (u32(1) << 31)) >> 31)
|
|
|
|
r1 = (s1 << 1) | ((s0 & (u32(1) << 31)) >> 31)
|
|
|
|
r0 = s0 << 1
|
|
|
|
return r2, r1, r0
|
2019-12-17 01:07:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// sum on 96 bit
|
2021-04-28 07:42:22 +03:00
|
|
|
fn add96(s2 u32, s1 u32, s0 u32, d2 u32, d1 u32, d0 u32) (u32, u32, u32) {
|
2019-12-20 00:29:37 +03:00
|
|
|
mut w := u64(0)
|
2019-12-17 01:07:13 +03:00
|
|
|
mut r0 := u32(0)
|
|
|
|
mut r1 := u32(0)
|
|
|
|
mut r2 := u32(0)
|
|
|
|
w = u64(s0) + u64(d0)
|
|
|
|
r0 = u32(w)
|
|
|
|
w >>= 32
|
|
|
|
w += u64(s1) + u64(d1)
|
|
|
|
r1 = u32(w)
|
|
|
|
w >>= 32
|
|
|
|
w += u64(s2) + u64(d2)
|
|
|
|
r2 = u32(w)
|
2021-04-28 07:42:22 +03:00
|
|
|
return r2, r1, r0
|
2019-12-17 01:07:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// subtraction on 96 bit
|
2021-04-28 07:42:22 +03:00
|
|
|
fn sub96(s2 u32, s1 u32, s0 u32, d2 u32, d1 u32, d0 u32) (u32, u32, u32) {
|
2019-12-20 00:29:37 +03:00
|
|
|
mut w := u64(0)
|
2019-12-17 01:07:13 +03:00
|
|
|
mut r0 := u32(0)
|
|
|
|
mut r1 := u32(0)
|
|
|
|
mut r2 := u32(0)
|
|
|
|
w = u64(s0) - u64(d0)
|
|
|
|
r0 = u32(w)
|
|
|
|
w >>= 32
|
|
|
|
w += u64(s1) - u64(d1)
|
|
|
|
r1 = u32(w)
|
|
|
|
w >>= 32
|
|
|
|
w += u64(s2) - u64(d2)
|
|
|
|
r2 = u32(w)
|
2021-04-28 07:42:22 +03:00
|
|
|
return r2, r1, r0
|
2019-12-17 01:07:13 +03:00
|
|
|
}
|
|
|
|
|
2022-01-04 18:43:45 +03:00
|
|
|
// Utility functions
|
2019-12-17 01:07:13 +03:00
|
|
|
fn is_digit(x byte) bool {
|
2021-04-28 07:42:22 +03:00
|
|
|
return (x >= strconv.c_zero && x <= strconv.c_nine) == true
|
2019-12-17 01:07:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn is_space(x byte) bool {
|
2021-09-08 14:19:53 +03:00
|
|
|
return x == `\t` || x == `\n` || x == `\v` || x == `\f` || x == `\r` || x == ` `
|
2019-12-17 01:07:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn is_exp(x byte) bool {
|
|
|
|
return (x == `E` || x == `e`) == true
|
|
|
|
}
|
|
|
|
|
2022-04-11 12:01:47 +03:00
|
|
|
// Possible parser return values.
|
|
|
|
enum ParserState {
|
|
|
|
ok // parser finished OK
|
|
|
|
pzero // no digits or number is smaller than +-2^-1022
|
|
|
|
mzero // number is negative, module smaller
|
|
|
|
pinf // number is higher than +HUGE_VAL
|
|
|
|
minf // number is lower than -HUGE_VAL
|
|
|
|
invalid_number // invalid number, used for '#@%^' for example
|
|
|
|
}
|
2019-12-17 01:07:13 +03:00
|
|
|
|
2022-04-11 12:01:47 +03:00
|
|
|
// parser tries to parse the given string into a number
|
|
|
|
// NOTE: #TOFIX need one char after the last char of the number
|
|
|
|
fn parser(s string) (ParserState, PrepNumber) {
|
2019-12-20 00:29:37 +03:00
|
|
|
mut digx := 0
|
2022-04-11 12:01:47 +03:00
|
|
|
mut result := ParserState.ok
|
2019-12-20 00:29:37 +03:00
|
|
|
mut expneg := false
|
|
|
|
mut expexp := 0
|
|
|
|
mut i := 0
|
2021-04-28 07:42:22 +03:00
|
|
|
mut pn := PrepNumber{}
|
|
|
|
|
|
|
|
// skip spaces
|
|
|
|
for i < s.len && s[i].is_space() {
|
|
|
|
i++
|
2019-12-20 00:29:37 +03:00
|
|
|
}
|
2021-04-28 07:42:22 +03:00
|
|
|
|
|
|
|
// check negatives
|
|
|
|
if s[i] == `-` {
|
|
|
|
pn.negative = true
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
|
|
|
// positive sign ignore it
|
|
|
|
if s[i] == `+` {
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
|
|
|
// read mantissa
|
|
|
|
for i < s.len && s[i].is_digit() {
|
|
|
|
// println("$i => ${s[i]}")
|
|
|
|
if digx < strconv.digits {
|
|
|
|
pn.mantissa *= 10
|
|
|
|
pn.mantissa += u64(s[i] - strconv.c_zero)
|
|
|
|
digx++
|
|
|
|
} else if pn.exponent < 2147483647 {
|
|
|
|
pn.exponent++
|
|
|
|
}
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
|
|
|
// read mantissa decimals
|
|
|
|
if (i < s.len) && (s[i] == `.`) {
|
|
|
|
i++
|
|
|
|
for i < s.len && s[i].is_digit() {
|
|
|
|
if digx < strconv.digits {
|
|
|
|
pn.mantissa *= 10
|
|
|
|
pn.mantissa += u64(s[i] - strconv.c_zero)
|
|
|
|
pn.exponent--
|
|
|
|
digx++
|
2019-12-17 01:07:13 +03:00
|
|
|
}
|
2021-04-28 07:42:22 +03:00
|
|
|
i++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// read exponent
|
|
|
|
if (i < s.len) && ((s[i] == `e`) || (s[i] == `E`)) {
|
|
|
|
i++
|
|
|
|
if i < s.len {
|
|
|
|
// esponent sign
|
|
|
|
if s[i] == strconv.c_plus {
|
|
|
|
i++
|
|
|
|
} else if s[i] == strconv.c_minus {
|
|
|
|
expneg = true
|
|
|
|
i++
|
2019-12-17 01:07:13 +03:00
|
|
|
}
|
2021-04-28 07:42:22 +03:00
|
|
|
|
|
|
|
for i < s.len && s[i].is_digit() {
|
|
|
|
if expexp < 214748364 {
|
|
|
|
expexp *= 10
|
|
|
|
expexp += int(s[i] - strconv.c_zero)
|
2019-12-17 01:07:13 +03:00
|
|
|
}
|
2021-04-28 07:42:22 +03:00
|
|
|
i++
|
2019-12-17 01:07:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-04-28 07:42:22 +03:00
|
|
|
|
2019-12-17 01:07:13 +03:00
|
|
|
if expneg {
|
|
|
|
expexp = -expexp
|
|
|
|
}
|
|
|
|
pn.exponent += expexp
|
|
|
|
if pn.mantissa == 0 {
|
|
|
|
if pn.negative {
|
2022-04-11 12:01:47 +03:00
|
|
|
result = .mzero
|
2021-04-28 07:42:22 +03:00
|
|
|
} else {
|
2022-04-11 12:01:47 +03:00
|
|
|
result = .pzero
|
2019-12-20 00:29:37 +03:00
|
|
|
}
|
2021-04-28 07:42:22 +03:00
|
|
|
} else if pn.exponent > 309 {
|
2019-12-17 01:07:13 +03:00
|
|
|
if pn.negative {
|
2022-04-11 12:01:47 +03:00
|
|
|
result = .minf
|
2021-04-28 07:42:22 +03:00
|
|
|
} else {
|
2022-04-11 12:01:47 +03:00
|
|
|
result = .pinf
|
2019-12-17 01:07:13 +03:00
|
|
|
}
|
2021-04-28 07:42:22 +03:00
|
|
|
} else if pn.exponent < -328 {
|
2019-12-17 01:07:13 +03:00
|
|
|
if pn.negative {
|
2022-04-11 12:01:47 +03:00
|
|
|
result = .mzero
|
2021-04-28 07:42:22 +03:00
|
|
|
} else {
|
2022-04-11 12:01:47 +03:00
|
|
|
result = .pzero
|
2019-12-17 01:07:13 +03:00
|
|
|
}
|
|
|
|
}
|
2022-02-10 14:27:32 +03:00
|
|
|
if i == 0 && s.len > 0 {
|
2022-04-11 12:01:47 +03:00
|
|
|
return ParserState.invalid_number, pn
|
2022-02-10 14:27:32 +03:00
|
|
|
}
|
2021-04-28 07:42:22 +03:00
|
|
|
return result, pn
|
2019-12-17 01:07:13 +03:00
|
|
|
}
|
|
|
|
|
2022-04-11 12:01:47 +03:00
|
|
|
// converter returns a u64 with the bit image of the f64 number
|
2020-06-04 11:35:40 +03:00
|
|
|
fn converter(mut pn PrepNumber) u64 {
|
2019-12-17 01:07:13 +03:00
|
|
|
mut binexp := 92
|
2022-04-11 12:01:47 +03:00
|
|
|
// s0,s1,s2 are the parts of a 96-bit precision integer
|
|
|
|
mut s2 := u32(0)
|
2019-12-20 00:29:37 +03:00
|
|
|
mut s1 := u32(0)
|
|
|
|
mut s0 := u32(0)
|
2022-04-11 12:01:47 +03:00
|
|
|
// q0,q1,q2 are the parts of a 96-bit precision integer
|
|
|
|
mut q2 := u32(0)
|
2019-12-20 00:29:37 +03:00
|
|
|
mut q1 := u32(0)
|
|
|
|
mut q0 := u32(0)
|
2022-04-11 12:01:47 +03:00
|
|
|
// r0,r1,r2 are the parts of a 96-bit precision integer
|
|
|
|
mut r2 := u32(0)
|
2019-12-20 00:29:37 +03:00
|
|
|
mut r1 := u32(0)
|
|
|
|
mut r0 := u32(0)
|
2022-04-11 12:01:47 +03:00
|
|
|
//
|
2021-04-28 07:42:22 +03:00
|
|
|
mask28 := u32(u64(0xF) << 28)
|
2019-12-17 01:07:13 +03:00
|
|
|
mut result := u64(0)
|
|
|
|
// working on 3 u32 to have 96 bit precision
|
|
|
|
s0 = u32(pn.mantissa & u64(0x00000000FFFFFFFF))
|
2021-04-28 07:42:22 +03:00
|
|
|
s1 = u32(pn.mantissa >> 32)
|
2019-12-17 01:07:13 +03:00
|
|
|
s2 = u32(0)
|
|
|
|
// so we take the decimal exponent off
|
|
|
|
for pn.exponent > 0 {
|
2021-04-28 07:42:22 +03:00
|
|
|
q2, q1, q0 = lsl96(s2, s1, s0) // q = s * 2
|
|
|
|
r2, r1, r0 = lsl96(q2, q1, q0) // r = s * 4 <=> q * 2
|
|
|
|
s2, s1, s0 = lsl96(r2, r1, r0) // s = s * 8 <=> r * 2
|
|
|
|
s2, s1, s0 = add96(s2, s1, s0, q2, q1, q0) // s = (s * 8) + (s * 2) <=> s*10
|
2019-12-17 01:07:13 +03:00
|
|
|
pn.exponent--
|
|
|
|
for (s2 & mask28) != 0 {
|
2021-04-28 07:42:22 +03:00
|
|
|
q2, q1, q0 = lsr96(s2, s1, s0)
|
2019-12-17 01:07:13 +03:00
|
|
|
binexp++
|
|
|
|
s2 = q2
|
|
|
|
s1 = q1
|
|
|
|
s0 = q0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for pn.exponent < 0 {
|
2021-04-28 07:42:22 +03:00
|
|
|
for !((s2 & (u32(1) << 31)) != 0) {
|
|
|
|
q2, q1, q0 = lsl96(s2, s1, s0)
|
2019-12-17 01:07:13 +03:00
|
|
|
binexp--
|
|
|
|
s2 = q2
|
|
|
|
s1 = q1
|
|
|
|
s0 = q0
|
|
|
|
}
|
2021-04-28 07:42:22 +03:00
|
|
|
q2 = s2 / strconv.c_ten
|
|
|
|
r1 = s2 % strconv.c_ten
|
|
|
|
r2 = (s1 >> 8) | (r1 << 24)
|
|
|
|
q1 = r2 / strconv.c_ten
|
|
|
|
r1 = r2 % strconv.c_ten
|
|
|
|
r2 = ((s1 & u32(0xFF)) << 16) | (s0 >> 16) | (r1 << 24)
|
|
|
|
r0 = r2 / strconv.c_ten
|
|
|
|
r1 = r2 % strconv.c_ten
|
|
|
|
q1 = (q1 << 8) | ((r0 & u32(0x00FF0000)) >> 16)
|
|
|
|
q0 = r0 << 16
|
|
|
|
r2 = (s0 & u32(0xFFFF)) | (r1 << 16)
|
|
|
|
q0 |= r2 / strconv.c_ten
|
2019-12-17 01:07:13 +03:00
|
|
|
s2 = q2
|
|
|
|
s1 = q1
|
|
|
|
s0 = q0
|
|
|
|
pn.exponent++
|
|
|
|
}
|
2019-12-20 00:29:37 +03:00
|
|
|
// C.printf("mantissa before normalization: %08x%08x%08x binexp: %d \n", s2,s1,s0,binexp)
|
2019-12-17 01:07:13 +03:00
|
|
|
// normalization, the 28 bit in s2 must the leftest one in the variable
|
2019-12-20 00:29:37 +03:00
|
|
|
if s2 != 0 || s1 != 0 || s0 != 0 {
|
2019-12-17 01:47:30 +03:00
|
|
|
for (s2 & mask28) == 0 {
|
2021-04-28 07:42:22 +03:00
|
|
|
q2, q1, q0 = lsl96(s2, s1, s0)
|
2019-12-17 01:07:13 +03:00
|
|
|
binexp--
|
|
|
|
s2 = q2
|
|
|
|
s1 = q1
|
|
|
|
s0 = q0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// rounding if needed
|
|
|
|
/*
|
|
|
|
* "round half to even" algorithm
|
|
|
|
* Example for f32, just a reminder
|
|
|
|
*
|
|
|
|
* If bit 54 is 0, round down
|
|
|
|
* If bit 54 is 1
|
|
|
|
* If any bit beyond bit 54 is 1, round up
|
|
|
|
* If all bits beyond bit 54 are 0 (meaning the number is halfway between two floating-point numbers)
|
|
|
|
* If bit 53 is 0, round down
|
|
|
|
* If bit 53 is 1, round up
|
|
|
|
*/
|
2021-04-28 07:42:22 +03:00
|
|
|
/*
|
|
|
|
test case 1 complete
|
2019-12-17 01:07:13 +03:00
|
|
|
s2=0x1FFFFFFF
|
|
|
|
s1=0xFFFFFF80
|
|
|
|
s0=0x0
|
|
|
|
*/
|
|
|
|
|
2021-04-28 07:42:22 +03:00
|
|
|
/*
|
|
|
|
test case 1 check_round_bit
|
2019-12-17 01:07:13 +03:00
|
|
|
s2=0x18888888
|
|
|
|
s1=0x88888880
|
|
|
|
s0=0x0
|
|
|
|
*/
|
|
|
|
|
2021-04-28 07:42:22 +03:00
|
|
|
/*
|
|
|
|
test case check_round_bit + normalization
|
2019-12-17 01:07:13 +03:00
|
|
|
s2=0x18888888
|
|
|
|
s1=0x88888F80
|
|
|
|
s0=0x0
|
|
|
|
*/
|
|
|
|
|
2019-12-20 00:29:37 +03:00
|
|
|
// C.printf("mantissa before rounding: %08x%08x%08x binexp: %d \n", s2,s1,s0,binexp)
|
2019-12-17 01:07:13 +03:00
|
|
|
// s1 => 0xFFFFFFxx only F are rapresented
|
|
|
|
nbit := 7
|
2021-04-28 07:42:22 +03:00
|
|
|
check_round_bit := u32(1) << u32(nbit)
|
|
|
|
check_round_mask := u32(0xFFFFFFFF) << u32(nbit)
|
2019-12-17 01:07:13 +03:00
|
|
|
if (s1 & check_round_bit) != 0 {
|
2019-12-20 00:29:37 +03:00
|
|
|
// C.printf("need round!! cehck mask: %08x\n", s1 & ~check_round_mask )
|
2019-12-17 01:07:13 +03:00
|
|
|
if (s1 & ~check_round_mask) != 0 {
|
2019-12-20 00:29:37 +03:00
|
|
|
// C.printf("Add 1!\n")
|
2021-04-28 07:42:22 +03:00
|
|
|
s2, s1, s0 = add96(s2, s1, s0, 0, check_round_bit, 0)
|
|
|
|
} else {
|
2019-12-20 00:29:37 +03:00
|
|
|
// C.printf("All 0!\n")
|
2021-04-28 07:42:22 +03:00
|
|
|
if (s1 & (check_round_bit << u32(1))) != 0 {
|
2019-12-20 00:29:37 +03:00
|
|
|
// C.printf("Add 1 form -1 bit control!\n")
|
2021-04-28 07:42:22 +03:00
|
|
|
s2, s1, s0 = add96(s2, s1, s0, 0, check_round_bit, 0)
|
2019-12-17 01:07:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
s1 = s1 & check_round_mask
|
|
|
|
s0 = u32(0)
|
2019-12-22 01:38:02 +03:00
|
|
|
// recheck normalization
|
2021-04-28 07:42:22 +03:00
|
|
|
if s2 & (mask28 << u32(1)) != 0 {
|
2019-12-22 01:38:02 +03:00
|
|
|
// C.printf("Renormalize!!")
|
2021-04-28 07:42:22 +03:00
|
|
|
q2, q1, q0 = lsr96(s2, s1, s0)
|
2019-12-22 01:38:02 +03:00
|
|
|
binexp--
|
|
|
|
s2 = q2
|
|
|
|
s1 = q1
|
|
|
|
s0 = q0
|
|
|
|
}
|
2019-12-17 01:07:13 +03:00
|
|
|
}
|
2019-12-20 00:29:37 +03:00
|
|
|
// tmp := ( u64(s2 & ~mask28) << 24) | ((u64(s1) + u64(128)) >> 8)
|
|
|
|
// C.printf("mantissa after rounding : %08x%08x%08x binexp: %d \n", s2,s1,s0,binexp)
|
|
|
|
// C.printf("Tmp result: %016x\n",tmp)
|
2019-12-17 01:07:13 +03:00
|
|
|
// end rounding
|
|
|
|
// offset the binary exponent IEEE 754
|
|
|
|
binexp += 1023
|
2019-12-17 01:47:30 +03:00
|
|
|
if binexp > 2046 {
|
2019-12-17 01:07:13 +03:00
|
|
|
if pn.negative {
|
2021-04-28 07:42:22 +03:00
|
|
|
result = strconv.double_minus_infinity
|
|
|
|
} else {
|
|
|
|
result = strconv.double_plus_infinity
|
2019-12-17 01:07:13 +03:00
|
|
|
}
|
2021-04-28 07:42:22 +03:00
|
|
|
} else if binexp < 1 {
|
2019-12-17 01:07:13 +03:00
|
|
|
if pn.negative {
|
2021-04-28 07:42:22 +03:00
|
|
|
result = strconv.double_minus_zero
|
|
|
|
} else {
|
|
|
|
result = strconv.double_plus_zero
|
2019-12-22 01:41:42 +03:00
|
|
|
}
|
2021-04-28 07:42:22 +03:00
|
|
|
} else if s2 != 0 {
|
2019-12-17 01:07:13 +03:00
|
|
|
mut q := u64(0)
|
2021-04-28 07:42:22 +03:00
|
|
|
binexs2 := u64(binexp) << 52
|
|
|
|
q = (u64(s2 & ~mask28) << 24) | ((u64(s1) + u64(128)) >> 8) | binexs2
|
2019-12-17 01:07:13 +03:00
|
|
|
if pn.negative {
|
2021-04-28 07:42:22 +03:00
|
|
|
q |= (u64(1) << 63)
|
2019-12-17 01:07:13 +03:00
|
|
|
}
|
|
|
|
result = q
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2022-04-11 12:01:47 +03:00
|
|
|
// atof64 parses the string `s`, and if possible, converts it into a f64 number
|
2022-02-10 14:27:32 +03:00
|
|
|
pub fn atof64(s string) ?f64 {
|
2022-01-04 18:43:45 +03:00
|
|
|
if s.len == 0 {
|
2022-02-10 14:27:32 +03:00
|
|
|
return error('expected a number found an empty string')
|
2022-01-04 18:43:45 +03:00
|
|
|
}
|
2021-04-28 07:42:22 +03:00
|
|
|
mut res := Float64u{}
|
2022-04-11 12:01:47 +03:00
|
|
|
mut res_parsing, mut pn := parser(s)
|
2019-12-17 01:07:13 +03:00
|
|
|
match res_parsing {
|
2022-04-11 12:01:47 +03:00
|
|
|
.ok {
|
2020-02-26 14:14:06 +03:00
|
|
|
res.u = converter(mut pn)
|
2019-12-17 01:07:13 +03:00
|
|
|
}
|
2022-04-11 12:01:47 +03:00
|
|
|
.pzero {
|
2021-04-28 07:42:22 +03:00
|
|
|
res.u = strconv.double_plus_zero
|
2019-12-17 01:07:13 +03:00
|
|
|
}
|
2022-04-11 12:01:47 +03:00
|
|
|
.mzero {
|
2021-04-28 07:42:22 +03:00
|
|
|
res.u = strconv.double_minus_zero
|
2019-12-17 01:07:13 +03:00
|
|
|
}
|
2022-04-11 12:01:47 +03:00
|
|
|
.pinf {
|
2021-04-28 07:42:22 +03:00
|
|
|
res.u = strconv.double_plus_infinity
|
2019-12-17 01:07:13 +03:00
|
|
|
}
|
2022-04-11 12:01:47 +03:00
|
|
|
.minf {
|
2021-04-28 07:42:22 +03:00
|
|
|
res.u = strconv.double_minus_infinity
|
2021-01-04 22:44:37 +03:00
|
|
|
}
|
2022-04-11 12:01:47 +03:00
|
|
|
.invalid_number {
|
2022-02-10 14:27:32 +03:00
|
|
|
return error('not a number')
|
|
|
|
}
|
2021-01-04 22:44:37 +03:00
|
|
|
}
|
2021-04-28 07:42:22 +03:00
|
|
|
return unsafe { res.f }
|
2019-12-17 01:07:13 +03:00
|
|
|
}
|