math: new consts + helpers funcs for string to int / float

pull/2387/head
joe-conigliaro 2019-10-17 17:04:57 +11:00 committed by Alexander Medvednikov
parent 5cd38ec91b
commit b32a462b2e
3 changed files with 108 additions and 33 deletions

58
vlib/math/bits.v 100644
View File

@ -0,0 +1,58 @@
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
module math
const (
uvnan = 0x7FF8000000000001
uvinf = 0x7FF0000000000000
uvneginf = 0xFFF0000000000000
uvone = 0x3FF0000000000000
mask = 0x7FF
shift = 64 - 11 - 1
bias = 1023
sign_mask = u64(u64(1) << 63)
frac_mask = u64(u64(u64(1)<<u64(shift)) - u64(1))
)
// inf returns positive infinity if sign >= 0, negative infinity if sign < 0.
pub fn inf(sign int) f64 {
v := if sign >= 0 { uvinf } else { uvneginf }
return f64_from_bits(v)
}
// nan returns an IEEE 754 ``not-a-number'' value.
pub fn nan() f64 { return f64_from_bits(uvnan) }
// is_nan reports whether f is an IEEE 754 ``not-a-number'' value.
pub fn is_nan(f f64) bool {
// IEEE 754 says that only NaNs satisfy f != f.
// To avoid the floating-point hardware, could use:
// x := f64_bits(f);
// return u32(x>>shift)&mask == mask && x != uvinf && x != uvneginf
return f != f
}
// is_inf reports whether f is an infinity, according to sign.
// If sign > 0, is_inf reports whether f is positive infinity.
// If sign < 0, is_inf reports whether f is negative infinity.
// If sign == 0, is_inf reports whether f is either infinity.
pub fn is_inf(f f64, sign int) bool {
// Test for infinity by comparing against maximum float.
// To avoid the floating-point hardware, could use:
// x := f64_bits(f);
// return sign >= 0 && x == uvinf || sign <= 0 && x == uvneginf;
return (sign >= 0 && f > max_f64) || (sign <= 0 && f < -max_f64)
}
// NOTE: (joe-c) exponent notation is borked
// normalize returns a normal number y and exponent exp
// satisfying x == y × 2**exp. It assumes x is finite and non-zero.
// pub fn normalize(x f64) (f64, int) {
// smallest_normal := 2.2250738585072014e-308 // 2**-1022
// if abs(x) < smallest_normal {
// return x * (1 << 52), -52
// }
// return x, 0
// }

50
vlib/math/const.v 100644
View File

@ -0,0 +1,50 @@
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
module math
const (
e = 2.71828182845904523536028747135266249775724709369995957496696763
pi = 3.14159265358979323846264338327950288419716939937510582097494459
phi = 1.61803398874989484820458683436563811772030917980576286213544862
tau = 6.28318530717958647692528676655900576839433879875021164194988918
sqrt2 = 1.41421356237309504880168872420969807856967187537694807317667974
sqrt_e = 1.64872127070012814684865078781416357165377610071014801157507931
sqrt_pi = 1.77245385090551602729816748334114518279754945612238712821380779
sqrt_tau = 2.50662827463100050241576528481104525300698674060993831662992357
sqrt_phi = 1.27201964951406896425242246173749149171560804184009624861664038
ln2 = 0.693147180559945309417232121458176568075500134360255254120680009
log2_e = 1.0 / ln2
ln10 = 2.30258509299404568401799145468436420760110148862877297603332790
log10_e = 1.0 / ln10
)
// Floating-point limit values
// max is the largest finite value representable by the type.
// smallest_non_zero is the smallest positive, non-zero value representable by the type.
const (
max_f32 = 3.40282346638528859811704183484516925440e+38 // 2**127 * (2**24 - 1) / 2**23
smallest_non_zero_f32 = 1.401298464324817070923729583289916131280e-45 // 1 / 2**(127 - 1 + 23)
max_f64 = 1.797693134862315708145274237317043567981e+308 // 2**1023 * (2**53 - 1) / 2**52
smallest_non_zero_f64 = 4.940656458412465441765687928682213723651e-324 // 1 / 2**(1023 - 1 + 52)
)
// Integer limit values
const (
max_i8 = 127
min_i8 = -128
max_i16 = 32767
min_i16 = -32768
max_i32 = 2147483647
min_i32 = -2147483648
min_i64 = -9223372036854775808
max_i64 = 9223372036854775807
max_u8 = 255
max_u16 = 65535
max_u32 = 4294967295
max_u64 = 18446744073709551615
)

View File

@ -10,39 +10,6 @@ module math
// When adding a new function, please make sure it's in the right place.
// All functions are sorted alphabetically.
const (
e = 2.71828182845904523536028747135266249775724709369995957496696763
pi = 3.14159265358979323846264338327950288419716939937510582097494459
phi = 1.61803398874989484820458683436563811772030917980576286213544862
tau = 6.28318530717958647692528676655900576839433879875021164194988918
sqrt2 = 1.41421356237309504880168872420969807856967187537694807317667974
sqrt_e = 1.64872127070012814684865078781416357165377610071014801157507931
sqrt_pi = 1.77245385090551602729816748334114518279754945612238712821380779
sqrt_tau = 2.50662827463100050241576528481104525300698674060993831662992357
sqrt_phi = 1.27201964951406896425242246173749149171560804184009624861664038
ln2 = 0.693147180559945309417232121458176568075500134360255254120680009
log2_e = 1.0 / ln2
ln10 = 2.30258509299404568401799145468436420760110148862877297603332790
log10_e = 1.0 / ln10
)
const (
max_i8 = 127
min_i8 = -128
max_i16 = 32767
min_i16 = -32768
max_i32 = 2147483647
min_i32 = -2147483648
// MaxI64 = ((1<<63) - 1)
// MinI64 = (-(1 << 63) )
max_u8 = 255
max_u16 = 65535
max_u32 = 4294967295
max_u64 = 18446744073709551615
)
// Returns the absolute value.
pub fn abs(a f64) f64 {
if a < 0 {