mirror of
https://github.com/vlang/v.git
synced 2025-09-16 16:02:29 +03:00
vlib/math: Add a pure V backend for vlib/math (#11267)
This commit is contained in:
parent
dd486bb0fb
commit
1cfc4198f5
67 changed files with 3805 additions and 1798 deletions
|
@ -4,17 +4,29 @@
|
|||
module math
|
||||
|
||||
const (
|
||||
uvnan = u64(0x7FF8000000000001)
|
||||
uvinf = u64(0x7FF0000000000000)
|
||||
uvneginf = u64(0xFFF0000000000000)
|
||||
uvone = u64(0x3FF0000000000000)
|
||||
mask = 0x7FF
|
||||
shift = 64 - 11 - 1
|
||||
bias = 1023
|
||||
sign_mask = (u64(1) << 63)
|
||||
frac_mask = ((u64(1) << u64(shift)) - u64(1))
|
||||
uvnan = u64(0x7FF8000000000001)
|
||||
uvinf = u64(0x7FF0000000000000)
|
||||
uvneginf = u64(0xFFF0000000000000)
|
||||
uvone = u64(0x3FF0000000000000)
|
||||
mask = 0x7FF
|
||||
shift = 64 - 11 - 1
|
||||
bias = 1023
|
||||
normalize_smallest_mask = (u64(1) << 52)
|
||||
sign_mask = (u64(1) << 63)
|
||||
frac_mask = ((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 { math.uvinf } else { math.uvneginf }
|
||||
return f64_from_bits(v)
|
||||
}
|
||||
|
||||
// nan returns an IEEE 754 ``not-a-number'' value.
|
||||
pub fn nan() f64 {
|
||||
return f64_from_bits(math.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.
|
||||
|
@ -36,13 +48,16 @@ pub fn is_inf(f f64, sign int) bool {
|
|||
return (sign >= 0 && f > max_f64) || (sign <= 0 && f < -max_f64)
|
||||
}
|
||||
|
||||
// NOTE: (joe-c) exponent notation is borked
|
||||
pub fn is_finite(f f64) bool {
|
||||
return !is_nan(f) && !is_inf(f, 0)
|
||||
}
|
||||
|
||||
// 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
|
||||
// }
|
||||
pub fn normalize(x f64) (f64, int) {
|
||||
smallest_normal := 2.2250738585072014e-308 // 2**-1022
|
||||
if abs(x) < smallest_normal {
|
||||
return x * math.normalize_smallest_mask, -52
|
||||
}
|
||||
return x, 0
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue