From ac2cbe7e3937106d485d4cb763ddf6e90085f2e1 Mon Sep 17 00:00:00 2001 From: inoas Date: Tue, 12 Apr 2022 00:43:17 +0200 Subject: fixes and refactoring --- src/gleam_stdlib.mjs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'src/gleam_stdlib.mjs') diff --git a/src/gleam_stdlib.mjs b/src/gleam_stdlib.mjs index 59077e9..6604bff 100644 --- a/src/gleam_stdlib.mjs +++ b/src/gleam_stdlib.mjs @@ -247,7 +247,18 @@ export function power(base, exponent) { } export function random_uniform() { - return Math.random(); + let random_uniform_result = Math.random(); + // With round-to-nearest-even behavior, the ranges claimed for the functions below + // (excluding the one for Math.random() itself) aren't exact. + // If extremely large bounds are chosen (2^53 or higher), + // it's possible in extremely rare cases to calculate the usually-excluded upper bound. + // Note that as numbers in JavaScript are IEEE 754 floating point numbers + // See: + // Because of this, we just loop 'until' we get a valid result where 0.0 <= x < 1.0: + if (random_uniform_result === 1.0) { + return random_uniform(); + } + return random_uniform_result; } export function bit_string_slice(bits, position, length) { -- cgit v1.2.3