1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

rand: add full precision f32 and f64 random functions; fix f32/f64 multipliers (#16875)

This commit is contained in:
John
2023-01-19 09:21:47 -04:00
committed by GitHub
parent 550cae931f
commit 4098612a87
6 changed files with 337 additions and 15 deletions

View File

@@ -2,11 +2,16 @@ module constants
// Commonly used constants across RNGs - some taken from "Numerical Recipes".
pub const (
lower_mask = u64(0x00000000FFFFFFFF)
max_u32 = u32(0xFFFFFFFF)
max_u64 = u64(0xFFFFFFFFFFFFFFFF)
max_u32_as_f32 = f32(max_u32) + 1
max_u64_as_f64 = f64(max_u64) + 1
u31_mask = u32(0x7FFFFFFF)
u63_mask = u64(0x7FFFFFFFFFFFFFFF)
lower_mask = u64(0x00000000FFFFFFFF)
max_u32 = u32(0xFFFFFFFF)
max_u64 = u64(0xFFFFFFFFFFFFFFFF)
u31_mask = u32(0x7FFFFFFF)
u63_mask = u64(0x7FFFFFFFFFFFFFFF)
// 23 bits for f32
ieee754_mantissa_f32_mask = (u32(1) << 23) - 1
// 52 bits for f64
ieee754_mantissa_f64_mask = (u64(1) << 52) - 1
// smallest mantissa with exponent 0 (un normalized)
reciprocal_2_23rd = 1.0 / f64(u32(1) << 23)
reciprocal_2_52nd = 1.0 / f64(u64(1) << 52)
)