UNPKG

fast-check

Version:

Property based testing framework for JavaScript (like QuickCheck)

33 lines (32 loc) 1.09 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.double = exports.float = void 0; const IntegerArbitrary_1 = require("./IntegerArbitrary"); const TupleArbitrary_1 = require("./TupleArbitrary"); function next(n) { return IntegerArbitrary_1.integer(0, (1 << n) - 1); } const floatInternal = () => { return next(24).map((v) => v / (1 << 24)); }; function float(a, b) { if (a === undefined) return floatInternal(); if (b === undefined) return floatInternal().map((v) => v * a); return floatInternal().map((v) => a + v * (b - a)); } exports.float = float; const doubleFactor = Math.pow(2, 27); const doubleDivisor = Math.pow(2, -53); const doubleInternal = () => { return TupleArbitrary_1.tuple(next(26), next(27)).map((v) => (v[0] * doubleFactor + v[1]) * doubleDivisor); }; function double(a, b) { if (a === undefined) return doubleInternal(); if (b === undefined) return doubleInternal().map((v) => v * a); return doubleInternal().map((v) => a + v * (b - a)); } exports.double = double;