Best JavaScript code snippet using fast-check-monorepo
math.ts
Source:math.ts
1/* eslint-disable functional/immutable-data */2import type { Has } from '@principia/base/Has'3import * as I from '@principia/base/IO'4import { Random } from '@principia/base/Random'5/** @internal */6export const MIN_VALUE_32 = 2 ** -126 * 2 ** -237/** @internal */8export const MAX_VALUE_32 = 2 ** 127 * (1 + (2 ** 23 - 1) / 2 ** 23)9/** @internal */10export const EPSILON_32 = 2 ** -2311/** @internal */12const INDEX_POSITIVE_INFINITY_32 = 2139095040 // floatToIndex(MAX_VALUE_32) + 1;13/** @internal */14const INDEX_NEGATIVE_INFINITY_32 = -2139095041 // floatToIndex(-MAX_VALUE_32) - 115export function safeFloatToIndex(f: number, label: string): I.IO<unknown, never, number> {16 const conversionTrick = 'you can convert any double to a 32-bit float by using `new Float32Array([myDouble])[0]`'17 const errorMessage = 'fc.floatNext constraints.' + label + ' must be a 32-bit float - ' + conversionTrick18 if (Number.isNaN(f) || (Number.isFinite(f) && (f < -MAX_VALUE_32 || f > MAX_VALUE_32))) {19 return I.die(new Error(errorMessage))20 }21 const index = floatToIndex(f)22 if (!Number.isInteger(index)) {23 return I.die(new Error(errorMessage))24 }25 return I.succeed(index)26}27export function decomposeFloat(f: number): { exponent: number, significand: number } {28 // 1 => significand 0b1 - exponent 1 (will be preferred)29 // => significand 0b0.1 - exponent 230 const maxSignificand = 1 + (2 ** 23 - 1) / 2 ** 2331 for (let exponent = -126; exponent !== 128; ++exponent) {32 const powExponent = 2 ** exponent33 const maxForExponent = maxSignificand * powExponent34 if (Math.abs(f) <= maxForExponent) {35 return { exponent, significand: f / powExponent }36 }37 }38 return { exponent: Number.NaN, significand: Number.NaN }39}40export function floatToIndex(f: number): number {41 if (f === Number.POSITIVE_INFINITY) {42 return INDEX_POSITIVE_INFINITY_3243 }44 if (f === Number.NEGATIVE_INFINITY) {45 return INDEX_NEGATIVE_INFINITY_3246 }47 const { exponent, significand } = decomposeFloat(f)48 if (Number.isNaN(exponent) || Number.isNaN(significand) || !Number.isInteger(significand * 0x800000)) {49 return Number.NaN50 }51 if (f > 0 || (f === 0 && 1 / f === Number.POSITIVE_INFINITY)) {52 return indexInFloatFromDecomp(exponent, significand)53 } else {54 return -indexInFloatFromDecomp(exponent, -significand) - 155 }56}57/** @internal */58export function indexInFloatFromDecomp(exponent: number, significand: number) {59 // WARNING: significand >= 060 // By construct of significand in decomposeFloat,61 // significand is always max-ed.62 // The float close to zero are the only one having a significand <1, they also have an exponent of -126.63 // They are in range: [2**(-126) * 2**(-23), 2**(-126) * (2 - 2 ** 23)]64 // In other words there are 2**24 elements in that range if we include zero.65 // All other ranges (other exponents) have a length of 2**23 elements.66 if (exponent === -126) {67 return significand * 0x800000 // significand * 2**2368 }69 // Offset due to exp = -126 + Offset of previous exp (excl. -126) + Offset in current exp70 // 2**24 + (exponent - (-126) -1) * 2**23 + (significand - 1) * 2**2371 return (exponent + 127) * 0x800000 + (significand - 1) * 0x80000072}73/**74 * Compute the 32-bit floating point number corresponding to the provided indexes75 *76 * @param n - index of the float77 *78 * @internal79 */80export function indexToFloat(index: number): number {81 if (index < 0) {82 return -indexToFloat(-index - 1)83 }84 if (index === INDEX_POSITIVE_INFINITY_32) {85 return Number.POSITIVE_INFINITY86 }87 if (index < 0x1000000) {88 // The first 2**24 elements correspond to values having89 // exponent = -126 and significand = index * 2**(-23)90 return index * 2 ** -14991 }92 const postIndex = index - 0x100000093 // Math.floor(postIndex / 0x800000) = Math.floor(postIndex / 2**23) = (postIndex >> 23)94 const exponent = -125 + (postIndex >> 23)95 // (postIndex % 0x800000) / 0x800000 = (postIndex & 0x7fffff) / 0x80000096 const significand = 1 + (postIndex & 0x7fffff) / 0x80000097 return significand * 2 ** exponent98}99/** @internal */100export type ArrayInt64 = { sign: 1 | -1, data: [number, number] }101/** @internal */102export const Zero64: ArrayInt64 = { sign: 1, data: [0, 0] }103/** @internal */104export const Unit64: ArrayInt64 = { sign: 1, data: [0, 1] }105/** @internal */106export function isZero64(a: ArrayInt64): boolean {107 return a.data[0] === 0 && a.data[1] === 0108}109/** @internal */110export function isStrictlyNegative64(a: ArrayInt64): boolean {111 return a.sign === -1 && !isZero64(a)112}113/** @internal */114export function isStrictlyPositive64(a: ArrayInt64): boolean {115 return a.sign === 1 && !isZero64(a)116}117/** @internal */118export function isEqual64(a: ArrayInt64, b: ArrayInt64): boolean {119 if (a.data[0] === b.data[0] && a.data[1] === b.data[1]) {120 return a.sign === b.sign || (a.data[0] === 0 && a.data[1] === 0) // either the same or both zero121 }122 return false123}124/** @internal */125function isStrictlySmaller64Internal(a: ArrayInt64['data'], b: ArrayInt64['data']): boolean {126 return a[0] < b[0] || (a[0] === b[0] && a[1] < b[1])127}128/** @internal */129export function isStrictlySmaller64(a: ArrayInt64, b: ArrayInt64): boolean {130 if (a.sign === b.sign) {131 return a.sign === 1132 ? isStrictlySmaller64Internal(a.data, b.data) // a.sign = +1, b.sign = +1133 : isStrictlySmaller64Internal(b.data, a.data) // a.sign = -1, b.sign = -1134 }135 // a.sign = +1, b.sign = -1 is always false136 return a.sign === -1 && (!isZero64(a) || !isZero64(b)) // a.sign = -1, b.sign = +1137}138/** @internal */139export function clone64(a: ArrayInt64): ArrayInt64 {140 return { sign: a.sign, data: [a.data[0], a.data[1]] }141}142/** @internal */143function substract64DataInternal(a: ArrayInt64['data'], b: ArrayInt64['data']): ArrayInt64['data'] {144 let reminderLow = 0145 let low = a[1] - b[1]146 if (low < 0) {147 reminderLow = 1148 low = low >>> 0149 }150 return [a[0] - b[0] - reminderLow, low]151}152/**153 * Expects a >= b154 * @internal155 */156function substract64Internal(a: ArrayInt64, b: ArrayInt64): ArrayInt64 {157 if (a.sign === 1 && b.sign === -1) {158 // Operation is a simple sum of a + abs(b)159 const low = a.data[1] + b.data[1]160 const high = a.data[0] + b.data[0] + (low > 0xffffffff ? 1 : 0)161 return { sign: 1, data: [high >>> 0, low >>> 0] }162 }163 // a.sign === -1 with b.sign === 1 is impossible given: a - b >= 0, except for a = 0 and b = 0164 // Operation is a substraction165 return {166 sign: 1,167 data: a.sign === 1 ? substract64DataInternal(a.data, b.data) : substract64DataInternal(b.data, a.data)168 }169}170/**171 * Substract two ArrayInt64172 * @returns When result is zero always with sign=1173 * @internal174 */175export function substract64(arrayIntA: ArrayInt64, arrayIntB: ArrayInt64): ArrayInt64 {176 if (isStrictlySmaller64(arrayIntA, arrayIntB)) {177 const out = substract64Internal(arrayIntB, arrayIntA)178 out.sign = -1179 return out180 }181 return substract64Internal(arrayIntA, arrayIntB)182}183/**184 * Negative version of an ArrayInt64185 * @internal186 */187export function negative64(arrayIntA: ArrayInt64): ArrayInt64 {188 return {189 sign: -arrayIntA.sign as -1 | 1,190 data: [arrayIntA.data[0], arrayIntA.data[1]]191 }192}193/**194 * Add two ArrayInt64195 * @returns When result is zero always with sign=1196 * @internal197 */198export function add64(arrayIntA: ArrayInt64, arrayIntB: ArrayInt64): ArrayInt64 {199 if (isZero64(arrayIntB)) {200 if (isZero64(arrayIntA)) {201 return clone64(Zero64)202 }203 return clone64(arrayIntA)204 }205 return substract64(arrayIntA, negative64(arrayIntB))206}207/**208 * Halve an ArrayInt64209 * @internal210 */211export function halve64(a: ArrayInt64): ArrayInt64 {212 return {213 sign: a.sign,214 data: [Math.floor(a.data[0] / 2), (a.data[0] % 2 === 1 ? 0x80000000 : 0) + Math.floor(a.data[1] / 2)]215 }216}217/**218 * Apply log2 to an ArrayInt64 (preserve sign)219 * @internal220 */221export function logLike64(a: ArrayInt64): ArrayInt64 {222 // Math.floor(Math.log(hi * 2**32 + low) / Math.log(2)) <= Math.floor(Math.log(2**64) / Math.log(2)) = 64223 return {224 sign: a.sign,225 data: [0, Math.floor(Math.log(a.data[0] * 0x100000000 + a.data[1]) / Math.log(2))]226 }227}228/** @internal */229const INDEX_POSITIVE_INFINITY: ArrayInt64 = { sign: 1, data: [2146435072, 0] } // doubleToIndex(Number.MAX_VALUE) + 1;230/** @internal */231const INDEX_NEGATIVE_INFINITY: ArrayInt64 = { sign: -1, data: [2146435072, 1] } // doubleToIndex(-Number.MAX_VALUE) - 1232/**233 * Decompose a 64-bit floating point number into a significand and exponent234 * such that:235 * - significand over 53 bits including sign (also referred as fraction)236 * - exponent over 11 bits including sign237 * - whenever there are multiple possibilities we take the one having the highest significand (in abs)238 * - Number.MAX_VALUE = 2**1023 * (1 + (2**52-1)/2**52)239 * = 2**1023 * (2 - Number.EPSILON)240 * - Number.MIN_VALUE = 2**(-1022) * 2**(-52)241 * - Number.EPSILON = 2**(-52)242 *243 * @param d - 64-bit floating point number to be decomposed into (significand, exponent)244 *245 * @internal246 */247export function decomposeDouble(d: number): { exponent: number, significand: number } {248 // 1 => significand 0b1 - exponent 1 (will be preferred)249 // => significand 0b0.1 - exponent 2250 const maxSignificand = 2 - Number.EPSILON251 for (let exponent = -1022; exponent !== 1024; ++exponent) {252 const powExponent = 2 ** exponent253 const maxForExponent = maxSignificand * powExponent254 if (Math.abs(d) <= maxForExponent) {255 return { exponent, significand: d / powExponent }256 }257 }258 return { exponent: Number.NaN, significand: Number.NaN }259}260/** @internal */261function positiveNumberToInt64(n: number): ArrayInt64['data'] {262 return [~~(n / 0x100000000), n >>> 0]263}264/** @internal */265function indexInDoubleFromDecomp(exponent: number, significand: number): ArrayInt64['data'] {266 // WARNING: significand >= 0267 // By construct of significand in decomposeDouble,268 // significand is always max-ed.269 // The double close to zero are the only one having a significand <1, they also have an exponent of -1022.270 // They are in range: [2**(-1022) * 2**(-52), 2**(-1022) * (2 - 2 ** 52)]271 // In other words there are 2**53 elements in that range if we include zero.272 // All other ranges (other exponents) have a length of 2**52 elements.273 if (exponent === -1022) {274 // We want the significand to be an integer value (like an index)275 const rescaledSignificand = significand * 2 ** 52 // significand * 2**52276 return positiveNumberToInt64(rescaledSignificand)277 }278 // Offset due to exp = -1022 + Offset of previous exp (excl. -1022) + Offset in current exp279 // 2**53 + (exponent - (-1022) -1) * 2**52 + (significand - 1) * 2**52280 // (exponent + 1023) * 2**52 + (significand - 1) * 2**52281 const rescaledSignificand = (significand - 1) * 2 ** 52 // (significand-1) * 2**52282 const exponentOnlyHigh = (exponent + 1023) * 2 ** 20 // (exponent + 1023) * 2**52 => [high: (exponent + 1023) * 2**20, low: 0]283 const index = positiveNumberToInt64(rescaledSignificand)284 index[0] += exponentOnlyHigh285 return index286}287/**288 * Compute the index of d relative to other available 64-bit floating point numbers289 * Rq: Produces negative indexes for negative doubles290 *291 * @param d - 64-bit floating point number, anything except NaN292 *293 * @internal294 */295export function doubleToIndex(d: number): ArrayInt64 {296 if (d === Number.POSITIVE_INFINITY) {297 return clone64(INDEX_POSITIVE_INFINITY)298 }299 if (d === Number.NEGATIVE_INFINITY) {300 return clone64(INDEX_NEGATIVE_INFINITY)301 }302 const decomp = decomposeDouble(d)303 const exponent = decomp.exponent304 const significand = decomp.significand305 if (d > 0 || (d === 0 && 1 / d === Number.POSITIVE_INFINITY)) {306 return { sign: 1, data: indexInDoubleFromDecomp(exponent, significand) }307 } else {308 const indexOpposite = indexInDoubleFromDecomp(exponent, -significand)309 if (indexOpposite[1] === 0xffffffff) {310 indexOpposite[0] += 1311 indexOpposite[1] = 0312 } else {313 indexOpposite[1] += 1314 }315 return { sign: -1, data: indexOpposite } // -indexInDoubleFromDecomp(exponent, -significand) - 1316 }317}318/**319 * Compute the 64-bit floating point number corresponding to the provided indexes320 *321 * @param n - index of the double322 *323 * @internal324 */325export function indexToDouble(index: ArrayInt64): number {326 if (index.sign === -1) {327 const indexOpposite: ArrayInt64 = { sign: 1, data: [index.data[0], index.data[1]] }328 if (indexOpposite.data[1] === 0) {329 indexOpposite.data[0] -= 1330 indexOpposite.data[1] = 0xffffffff331 } else {332 indexOpposite.data[1] -= 1333 }334 return -indexToDouble(indexOpposite) // -indexToDouble(-index - 1);335 }336 if (isEqual64(index, INDEX_POSITIVE_INFINITY)) {337 return Number.POSITIVE_INFINITY338 }339 if (index.data[0] < 0x200000) {340 // if: index < 2 ** 53 <--> index[0] < 2 ** (53-32) = 0x20_0000341 // The first 2**53 elements correspond to values having342 // exponent = -1022 and significand = index * Number.EPSILON343 // double value = index * 2 ** -1022 * Number.EPSILON344 // = index * 2 ** -1022 * 2 ** -52345 // = index * 2 ** -1074346 return (index.data[0] * 0x100000000 + index.data[1]) * 2 ** -1074347 }348 const postIndexHigh = index.data[0] - 0x200000 // postIndex = index - 2 ** 53349 // exponent = -1021 + Math.floor(postIndex / 2**52)350 // = -1021 + Math.floor(postIndexHigh / 2**(52-32))351 // = -1021 + Math.floor(postIndexHigh / 2**20)352 // = -1021 + (postIndexHigh >> 20)353 const exponent = -1021 + (postIndexHigh >> 20)354 // significand = 1 + (postIndex % 2**52) / 2**52355 // = 1 + ((postIndexHigh * 2**32 + postIndexLow) % 2**52) / 2**52356 // = 1 + ((postIndexHigh % 2**20) * 2**32 + postIndexLow) / 2**52357 // = 1 + ((postIndexHigh & 0xfffff) * 2**32 + postIndexLow) / 2**52358 // = 1 + ((postIndexHigh & 0xfffff) * 2**32 + postIndexLow) * Number.EPSILON359 const significand = 1 + ((postIndexHigh & 0xfffff) * 2 ** 32 + index.data[1]) * Number.EPSILON360 return significand * 2 ** exponent361}362/**363 * Same as {@link doubleToIndex} except it throws in case of invalid double364 *365 * @internal366 */367export function safeDoubleToIndex(d: number, label: string): I.IO<unknown, never, ArrayInt64> {368 if (Number.isNaN(d)) {369 // Number.NaN does not have any associated index in the current implementation370 return I.die(new Error('fc.doubleNext constraints.' + label + ' must be a 32-bit float'))371 }372 return I.succeed(doubleToIndex(d))373}374let EPSILON = Math.pow(2, -52)375let MAX_VALUE = (2 - EPSILON) * Math.pow(2, 1023)376let MIN_VALUE = Math.pow(2, -1022)377export function nextUp(x: number) {378 if (x !== x) {379 return x380 }381 if (x === -1 / 0) {382 return -MAX_VALUE383 }384 if (x === +1 / 0) {385 return +1 / 0386 }387 if (x === +MAX_VALUE) {388 return +1 / 0389 }390 let y = x * (x < 0 ? 1 - EPSILON / 2 : 1 + EPSILON)391 if (y === x) {392 y = MIN_VALUE * EPSILON > 0 ? x + MIN_VALUE * EPSILON : x + MIN_VALUE393 }394 if (y === +1 / 0) {395 y = +MAX_VALUE396 }397 let b = x + (y - x) / 2398 if (x < b && b < y) {399 y = b400 }401 let c = (y + x) / 2402 if (x < c && c < y) {403 y = c404 }405 return y === 0 ? -0 : y406}407export function nextAfter(x: number, y: number) {408 return y < x ? -nextUp(-x) : y > x ? nextUp(x) : x !== x ? x : y409}410export function computeBiasedRanges(411 min: ArrayInt64,412 max: ArrayInt64,413 biasedRanges?: { min: ArrayInt64, max: ArrayInt64 }[]414): { min: ArrayInt64, max: ArrayInt64 }[] {415 if (biasedRanges != null) {416 return biasedRanges417 }418 if (isEqual64(min, max)) {419 return [{ min, max }]420 }421 const minStrictlySmallerThanZero = isStrictlyNegative64(min)422 const maxStrictlyGreaterThanZero = isStrictlyPositive64(max)423 if (minStrictlySmallerThanZero && maxStrictlyGreaterThanZero) {424 const logMin = logLike64(min)425 const logMax = logLike64(max)426 return [427 { min: logMin, max: logMax },428 { min: substract64(max, logMax), max },429 { min, max: substract64(min, logMin) }430 ]431 } else {432 const logGap = logLike64(substract64(max, min))433 const closeToMin = { min, max: add64(min, logGap) }434 const closeToMax = { min: substract64(max, logGap), max }435 return minStrictlySmallerThanZero ? [closeToMax, closeToMin] : [closeToMin, closeToMax]436 }437}438export function computeArrayInt64GenerateRange(439 min: ArrayInt64,440 max: ArrayInt64,441 biasFactor: number | undefined,442 biasedRanges: { min: ArrayInt64, max: ArrayInt64 }[] | undefined443): I.URIO<Has<Random>, { min: ArrayInt64, max: ArrayInt64 }> {444 return I.gen(function* (_) {445 if (biasFactor === undefined || (yield* _(Random.nextIntBetween(1, biasFactor))) !== 1) {446 return { min, max }447 }448 const ranges = computeBiasedRanges(min, max, biasedRanges)449 if (ranges.length === 1) {450 return ranges[0]451 }452 const id = yield* _(Random.nextIntBetween(-2 * (ranges.length - 1), ranges.length - 2))453 return id < 0 ? ranges[0] : ranges[id + 1]454 })455}456export function clamp(n: number, min: number, max: number): number {457 return n < min ? min : n > max ? max : n...
DoubleHelpers.ts
Source:DoubleHelpers.ts
1import { ArrayInt64, clone64, isEqual64 } from './ArrayInt64';2const safeNegativeInfinity = Number.NEGATIVE_INFINITY;3const safePositiveInfinity = Number.POSITIVE_INFINITY;4const safeNaN = Number.NaN;5const safeEpsilon = Number.EPSILON;6/** @internal */7const INDEX_POSITIVE_INFINITY: ArrayInt64 = { sign: 1, data: [2146435072, 0] }; // doubleToIndex(Number.MAX_VALUE) + 1;8/** @internal */9const INDEX_NEGATIVE_INFINITY: ArrayInt64 = { sign: -1, data: [2146435072, 1] }; // doubleToIndex(-Number.MAX_VALUE) - 110/**11 * Decompose a 64-bit floating point number into a significand and exponent12 * such that:13 * - significand over 53 bits including sign (also referred as fraction)14 * - exponent over 11 bits including sign15 * - whenever there are multiple possibilities we take the one having the highest significand (in abs)16 * - Number.MAX_VALUE = 2**1023 * (1 + (2**52-1)/2**52)17 * = 2**1023 * (2 - Number.EPSILON)18 * - Number.MIN_VALUE = 2**(-1022) * 2**(-52)19 * - Number.EPSILON = 2**(-52)20 *21 * @param d - 64-bit floating point number to be decomposed into (significand, exponent)22 *23 * @internal24 */25export function decomposeDouble(d: number): { exponent: number; significand: number } {26 // 1 => significand 0b1 - exponent 1 (will be preferred)27 // => significand 0b0.1 - exponent 228 const maxSignificand = 2 - safeEpsilon;29 for (let exponent = -1022; exponent !== 1024; ++exponent) {30 const powExponent = 2 ** exponent;31 const maxForExponent = maxSignificand * powExponent;32 if (Math.abs(d) <= maxForExponent) {33 return { exponent, significand: d / powExponent };34 }35 }36 return { exponent: safeNaN, significand: safeNaN };37}38/** @internal */39function positiveNumberToInt64(n: number): ArrayInt64['data'] {40 return [~~(n / 0x100000000), n >>> 0];41}42/** @internal */43function indexInDoubleFromDecomp(exponent: number, significand: number): ArrayInt64['data'] {44 // WARNING: significand >= 045 // By construct of significand in decomposeDouble,46 // significand is always max-ed.47 // The double close to zero are the only one having a significand <1, they also have an exponent of -1022.48 // They are in range: [2**(-1022) * 2**(-52), 2**(-1022) * (2 - 2 ** 52)]49 // In other words there are 2**53 elements in that range if we include zero.50 // All other ranges (other exponents) have a length of 2**52 elements.51 if (exponent === -1022) {52 // We want the significand to be an integer value (like an index)53 const rescaledSignificand = significand * 2 ** 52; // significand * 2**5254 return positiveNumberToInt64(rescaledSignificand);55 }56 // Offset due to exp = -1022 + Offset of previous exp (excl. -1022) + Offset in current exp57 // 2**53 + (exponent - (-1022) -1) * 2**52 + (significand - 1) * 2**5258 // (exponent + 1023) * 2**52 + (significand - 1) * 2**5259 const rescaledSignificand = (significand - 1) * 2 ** 52; // (significand-1) * 2**5260 const exponentOnlyHigh = (exponent + 1023) * 2 ** 20; // (exponent + 1023) * 2**52 => [high: (exponent + 1023) * 2**20, low: 0]61 const index = positiveNumberToInt64(rescaledSignificand);62 index[0] += exponentOnlyHigh;63 return index;64}65/**66 * Compute the index of d relative to other available 64-bit floating point numbers67 * Rq: Produces negative indexes for negative doubles68 *69 * @param d - 64-bit floating point number, anything except NaN70 *71 * @internal72 */73export function doubleToIndex(d: number): ArrayInt64 {74 if (d === safePositiveInfinity) {75 return clone64(INDEX_POSITIVE_INFINITY);76 }77 if (d === safeNegativeInfinity) {78 return clone64(INDEX_NEGATIVE_INFINITY);79 }80 const decomp = decomposeDouble(d);81 const exponent = decomp.exponent;82 const significand = decomp.significand;83 if (d > 0 || (d === 0 && 1 / d === safePositiveInfinity)) {84 return { sign: 1, data: indexInDoubleFromDecomp(exponent, significand) };85 } else {86 const indexOpposite = indexInDoubleFromDecomp(exponent, -significand);87 if (indexOpposite[1] === 0xffffffff) {88 indexOpposite[0] += 1;89 indexOpposite[1] = 0;90 } else {91 indexOpposite[1] += 1;92 }93 return { sign: -1, data: indexOpposite }; // -indexInDoubleFromDecomp(exponent, -significand) - 194 }95}96/**97 * Compute the 64-bit floating point number corresponding to the provided indexes98 *99 * @param n - index of the double100 *101 * @internal102 */103export function indexToDouble(index: ArrayInt64): number {104 if (index.sign === -1) {105 const indexOpposite: ArrayInt64 = { sign: 1, data: [index.data[0], index.data[1]] };106 if (indexOpposite.data[1] === 0) {107 indexOpposite.data[0] -= 1;108 indexOpposite.data[1] = 0xffffffff;109 } else {110 indexOpposite.data[1] -= 1;111 }112 return -indexToDouble(indexOpposite); // -indexToDouble(-index - 1);113 }114 if (isEqual64(index, INDEX_POSITIVE_INFINITY)) {115 return safePositiveInfinity;116 }117 if (index.data[0] < 0x200000) {118 // if: index < 2 ** 53 <--> index[0] < 2 ** (53-32) = 0x20_0000119 // The first 2**53 elements correspond to values having120 // exponent = -1022 and significand = index * Number.EPSILON121 // double value = index * 2 ** -1022 * Number.EPSILON122 // = index * 2 ** -1022 * 2 ** -52123 // = index * 2 ** -1074124 return (index.data[0] * 0x100000000 + index.data[1]) * 2 ** -1074;125 }126 const postIndexHigh = index.data[0] - 0x200000; // postIndex = index - 2 ** 53127 // exponent = -1021 + Math.floor(postIndex / 2**52)128 // = -1021 + Math.floor(postIndexHigh / 2**(52-32))129 // = -1021 + Math.floor(postIndexHigh / 2**20)130 // = -1021 + (postIndexHigh >> 20)131 const exponent = -1021 + (postIndexHigh >> 20);132 // significand = 1 + (postIndex % 2**52) / 2**52133 // = 1 + ((postIndexHigh * 2**32 + postIndexLow) % 2**52) / 2**52134 // = 1 + ((postIndexHigh % 2**20) * 2**32 + postIndexLow) / 2**52135 // = 1 + ((postIndexHigh & 0xfffff) * 2**32 + postIndexLow) / 2**52136 // = 1 + ((postIndexHigh & 0xfffff) * 2**32 + postIndexLow) * Number.EPSILON137 const significand = 1 + ((postIndexHigh & 0xfffff) * 2 ** 32 + index.data[1]) * safeEpsilon;138 return significand * 2 ** exponent;...
FloatHelpers.ts
Source:FloatHelpers.ts
1const safeNumberIsInteger = Number.isInteger;2const safeNumberIsNaN = Number.isNaN;3const safeNegativeInfinity = Number.NEGATIVE_INFINITY;4const safePositiveInfinity = Number.POSITIVE_INFINITY;5const safeNaN = Number.NaN;6/** @internal */7export const MIN_VALUE_32 = 2 ** -126 * 2 ** -23;8/** @internal */9export const MAX_VALUE_32 = 2 ** 127 * (1 + (2 ** 23 - 1) / 2 ** 23);10/** @internal */11export const EPSILON_32 = 2 ** -23;12/** @internal */13const INDEX_POSITIVE_INFINITY = 2139095040; // floatToIndex(MAX_VALUE_32) + 1;14/** @internal */15const INDEX_NEGATIVE_INFINITY = -2139095041; // floatToIndex(-MAX_VALUE_32) - 116/**17 * Decompose a 32-bit floating point number into a significand and exponent18 * such that:19 * - significand over 24 bits including sign (also referred as fraction)20 * - exponent over 8 bits including sign21 * - whenever there are multiple possibilities we take the one having the highest significand (in abs)22 *23 * Remark in 64-bit floating point number:24 * - significand is over 53 bits including sign25 * - exponent is over 11 bits including sign26 * - Number.MAX_VALUE = 2**1023 * (1 + (2**52-1)/2**52)27 * - Number.MIN_VALUE = 2**(-1022) * 2**(-52)28 * - Number.EPSILON = 2**(-52)29 *30 * @param f - 32-bit floating point number to be decomposed into (significand, exponent)31 *32 * @internal33 */34export function decomposeFloat(f: number): { exponent: number; significand: number } {35 // 1 => significand 0b1 - exponent 1 (will be preferred)36 // => significand 0b0.1 - exponent 237 const maxSignificand = 1 + (2 ** 23 - 1) / 2 ** 23;38 for (let exponent = -126; exponent !== 128; ++exponent) {39 const powExponent = 2 ** exponent;40 const maxForExponent = maxSignificand * powExponent;41 if (Math.abs(f) <= maxForExponent) {42 return { exponent, significand: f / powExponent };43 }44 }45 return { exponent: safeNaN, significand: safeNaN };46}47/** @internal */48function indexInFloatFromDecomp(exponent: number, significand: number) {49 // WARNING: significand >= 050 // By construct of significand in decomposeFloat,51 // significand is always max-ed.52 // The float close to zero are the only one having a significand <1, they also have an exponent of -126.53 // They are in range: [2**(-126) * 2**(-23), 2**(-126) * (2 - 2 ** 23)]54 // In other words there are 2**24 elements in that range if we include zero.55 // All other ranges (other exponents) have a length of 2**23 elements.56 if (exponent === -126) {57 return significand * 0x800000; // significand * 2**2358 }59 // Offset due to exp = -126 + Offset of previous exp (excl. -126) + Offset in current exp60 // 2**24 + (exponent - (-126) -1) * 2**23 + (significand - 1) * 2**2361 return (exponent + 127) * 0x800000 + (significand - 1) * 0x800000;62}63/**64 * Compute the index of f relative to other available 32-bit floating point numbers65 * Rq: Produces negative indexes for negative floats66 *67 * @param f - 32-bit floating point number68 *69 * @internal70 */71export function floatToIndex(f: number): number {72 if (f === safePositiveInfinity) {73 return INDEX_POSITIVE_INFINITY;74 }75 if (f === safeNegativeInfinity) {76 return INDEX_NEGATIVE_INFINITY;77 }78 const decomp = decomposeFloat(f);79 const exponent = decomp.exponent;80 const significand = decomp.significand;81 if (safeNumberIsNaN(exponent) || safeNumberIsNaN(significand) || !safeNumberIsInteger(significand * 0x800000)) {82 return safeNaN;83 }84 if (f > 0 || (f === 0 && 1 / f === safePositiveInfinity)) {85 return indexInFloatFromDecomp(exponent, significand);86 } else {87 return -indexInFloatFromDecomp(exponent, -significand) - 1;88 }89}90/**91 * Compute the 32-bit floating point number corresponding to the provided indexes92 *93 * @param n - index of the float94 *95 * @internal96 */97export function indexToFloat(index: number): number {98 if (index < 0) {99 return -indexToFloat(-index - 1);100 }101 if (index === INDEX_POSITIVE_INFINITY) {102 return safePositiveInfinity;103 }104 if (index < 0x1000000) {105 // The first 2**24 elements correspond to values having106 // exponent = -126 and significand = index * 2**(-23)107 return index * 2 ** -149;108 }109 const postIndex = index - 0x1000000;110 // Math.floor(postIndex / 0x800000) = Math.floor(postIndex / 2**23) = (postIndex >> 23)111 const exponent = -125 + (postIndex >> 23);112 // (postIndex % 0x800000) / 0x800000 = (postIndex & 0x7fffff) / 0x800000113 const significand = 1 + (postIndex & 0x7fffff) / 0x800000;114 return significand * 2 ** exponent;...
Using AI Code Generation
1const fc = require("fast-check");2const { INDEX_POSITIVE_INFINITY } = require("fast-check/lib/check/arbitrary/definition/Constants");3const { integer } = require("fast-check/lib/check/arbitrary/IntegerArbitrary");4const { array } = require("fast-check/lib/check/arbitrary/ArrayArbitrary");5const { tuple } = require("fast-check/lib/check/arbitrary/TupleArbitrary");6const { map } = require("fast-check/lib/check/arbitrary/MapArbitrary");7const { set } = require("fast-check/lib/check/arbitrary/SetArbitrary");8const { string } = require("fast-check/lib/check/arbitrary/StringArbitrary");9const { dictionary } = require("fast-check/lib/check/arbitrary/DictionaryArbitrary");10const { record } = require("fast-check/lib/check/arbitrary/RecordArbitrary");11const { date } = require("fast-check/lib/check/arbitrary/DateArbitrary");12const { bigInt } = require("fast-check/lib/check/arbitrary/BigIntArbitrary");13const { bigUintN } = require("fast-check/lib/check/arbitrary/BigUintNArbitrary");14const { float } = require("fast-check/lib/check/arbitrary/FloatArbitrary");15const { double } = require("fast-check/lib/check/arbitrary/DoubleArbitrary");16const { unicode } = require("fast-check/lib/check/arbitrary/UnicodeArbitrary");17const { unicodeJsonString } = require("fast-check/lib/check/arbitrary/UnicodeJsonStringArbitrary");18const { unicodeJsonObject } = require("fast-check/lib/check/arbitrary/UnicodeJsonObjectArbitrary");19const { unicodeJson } = require("fast-check/lib/check/arbitrary/UnicodeJsonArbitrary");20const { json } = require("fast-check/lib/check/arbitrary/JsonArbitrary");21const { jsonObject } = require("fast-check/lib/check/arbitrary/JsonObjectArbitrary");22const { jsonString } = require("fast-check/lib/check/arbitrary/JsonStringArbitrary");23const { frequency } = require("fast-check/lib/check/arbitrary/FrequencyArbitrary");24const { oneof } = require("fast-check/lib/check/arbitrary/OneOfArbitrary");25const { option } = require("fast-check/lib/check/arbitrary/OptionArbitrary");26const { constantFrom } = require("fast-check/lib/check/arbitrary/ConstantFromArbitrary");27const { constant } = require("fast-check/lib/check/arbitrary/ConstantArbitrary");28const { boolean
Using AI Code Generation
1const fc = require('fast-check');2const { INDEX_POSITIVE_INFINITY } = require('fast-check/lib/check/arbitrary/IntegerArbitrary.js');3fc.assert(4 fc.property(5 fc.integer({min: 0, max: INDEX_POSITIVE_INFINITY}),6 (n) => {7 return n < INDEX_POSITIVE_INFINITY;8 }9);10const fc = require('fast-check');11const { INDEX_NEGATIVE_INFINITY } = require('fast-check/lib/check/arbitrary/IntegerArbitrary.js');12fc.assert(13 fc.property(14 fc.integer({min: INDEX_NEGATIVE_INFINITY, max: -1}),15 (n) => {16 return n > INDEX_NEGATIVE_INFINITY;17 }18);19const fc = require('fast-check');20const { INDEX_POSITIVE_INFINITY } = require('fast-check/lib/check/arbitrary/IntegerArbitrary.js');21fc.assert(22 fc.property(23 fc.integer({min: 0, max: INDEX_POSITIVE_INFINITY}),24 (n) => {25 return n < INDEX_POSITIVE_INFINITY;26 }27);28const fc = require('fast-check');29const { INDEX_NEGATIVE_INFINITY } = require('fast-check/lib/check/arbitrary/IntegerArbitrary.js');30fc.assert(31 fc.property(32 fc.integer({min: INDEX_NEGATIVE_INFINITY, max: -1}),33 (n) => {34 return n > INDEX_NEGATIVE_INFINITY;35 }36);37const fc = require('fast-check');38const { INDEX_POSITIVE_INFINITY } = require('fast-check/lib/check/arbitrary/IntegerArbitrary.js');39fc.assert(40 fc.property(41 fc.integer({min: 0, max: INDEX_POSITIVE_INFINITY}),42 (n) => {43 return n < INDEX_POSITIVE_INFINITY;44 }45);46const fc = require('fast-check');47const { INDEX_NEGATIVE_INFINITY } = require('fast-check/lib/check/arbitrary/IntegerArbitrary.js');
Using AI Code Generation
1const fc = require('fast-check');2const { INDEX_POSITIVE_INFINITY } = require('fast-check/lib/arbitrary/_internals/IndexedArbitrary');3const { array } = require('fast-check/lib/arbitrary/array');4const { integer } = require('fast-check/lib/arbitrary/integer');5const myTest = () => {6 const myArray = array(integer(), 0, INDEX_POSITIVE_INFINITY);7 fc.assert(fc.property(myArray, (arr) => {8 console.log(arr);9 return true;10 }));11};12myTest();13const fc = require('fast-check');14const { INDEX_POSITIVE_INFINITY } = require('fast-check/lib/arbitrary/_internals/IndexedArbitrary');15const { array } = require('fast-check/lib/arbitrary/array');16const { integer } = require('fast-check/lib/arbitrary/integer');17const myTest = () => {18 const myArray = array(integer(), 0, INDEX_POSITIVE_INFINITY);19 fc.assert(fc.property(myArray, (arr) => {20 console.log(arr);21 return true;22 }));23};24myTest();25const fc = require('fast-check');26const { INDEX_POSITIVE_INFINITY } = require('fast-check/lib/arbitrary/_internals/IndexedArbitrary');27const { array } = require('fast-check/lib/arbitrary/array');28const { integer } = require('fast-check/lib/arbitrary/integer');29const myTest = () => {30 const myArray = array(integer(), 0, INDEX_POSITIVE_INFINITY);31 fc.assert(fc.property(myArray, (arr) => {32 console.log(arr);33 return true;34 }));35};36myTest();37const fc = require('fast-check');38const { INDEX_POSITIVE_INFINITY } = require('fast-check/lib/arbitrary/_internals/IndexedArbitrary');39const { array } = require('fast-check/lib/arbitrary/array');40const { integer } = require('fast-check/lib/arbitrary/integer');41const myTest = () => {42 const myArray = array(integer(), 0, INDEX_POSITIVE_INFINITY);43 fc.assert(fc.property(myArray, (arr) => {
Using AI Code Generation
1const { indexPositiveInfinity } = require('fast-check-monorepo');2test('indexPositiveInfinity', () => {3 expect(indexPositiveInfinity()).toBe(Infinity);4});5const { indexNegativeInfinity } = require('fast-check-monorepo');6test('indexNegativeInfinity', () => {7 expect(indexNegativeInfinity()).toBe(-Infinity);8});9const { indexMaxSafeInteger } = require('fast-check-monorepo');10test('indexMaxSafeInteger', () => {11 expect(indexMaxSafeInteger()).toBe(Number.MAX_SAFE_INTEGER);12});13const { indexMinSafeInteger } = require('fast-check-monorepo');14test('indexMinSafeInteger', () => {15 expect(indexMinSafeInteger()).toBe(Number.MIN_SAFE_INTEGER);16});17const { indexMaxValue } = require('fast-check-monorepo');18test('indexMaxValue', () => {19 expect(indexMaxValue()).toBe(Number.MAX_VALUE);20});21const { indexMinValue } = require('fast-check-monorepo');22test('indexMinValue', () => {23 expect(indexMinValue()).toBe(Number.MIN_VALUE);24});25const { indexMaxInteger } = require('fast-check-monorepo');26test('indexMaxInteger', () => {27 expect(indexMaxInteger()).toBe(Number.MAX_SAFE_INTEGER);28});29const { indexMinInteger } = require('fast-check-monorepo');30test('indexMinInteger', () => {31 expect(indexMinInteger()).toBe(Number.MIN_SAFE_INTEGER);32});33const { indexMaxSafeIntegerNegative } = require('fast-check-monorepo');34test('index
Using AI Code Generation
1const fc = require('fast-check');2const { IndexPositiveInfinity } = require('fast-check');3const indexPositiveInfinity = IndexPositiveInfinity();4fc.assert(5 fc.property(6 fc.array(fc.integer()),7 fc.nat(),8 (arr, index) => {9 const result = indexPositiveInfinity(arr, index);10 return result === arr[index % arr.length];11 }12);13console.log('Test3.js passed');14const fc = require('fast-check');15const { IndexNegativeInfinity } = require('fast-check');16const indexNegativeInfinity = IndexNegativeInfinity();17fc.assert(18 fc.property(19 fc.array(fc.integer()),20 fc.nat(),21 (arr, index) => {22 const result = indexNegativeInfinity(arr, index);23 return result === arr[arr.length - 1 - (index % arr.length)];24 }25);26console.log('Test4.js passed');27const fc = require('fast-check');28const { IndexRandom } = require('fast-check');29const indexRandom = IndexRandom();30fc.assert(31 fc.property(32 fc.array(fc.integer()),33 fc.nat(),34 (arr, index) => {35 const result = indexRandom(arr, index);36 return result === arr[index % arr.length];37 }38);39console.log('Test5.js passed');40const fc = require('fast-check');41const { IndexRandom } = require('fast-check');42const indexRandom = IndexRandom();43fc.assert(44 fc.property(45 fc.array(fc.integer()),46 fc.nat(),47 (arr, index) => {48 const result = indexRandom(arr, index);49 return result === arr[index % arr.length];50 }51);52console.log('Test6.js passed');53const fc = require('fast-check');54const { IndexRandom } = require('fast-check');55const indexRandom = IndexRandom();56fc.assert(57 fc.property(58 fc.array(fc.integer()),59 fc.nat(),60 (arr, index) => {
Using AI Code Generation
1const fc = require('fast-check')2console.log(fc)3const { indexPositiveInfinity } = require('fast-check/numbers')4console.log(indexPositiveInfinity)5const { indexNegativeInfinity } = require('fast-check/numbers')6console.log(indexNegativeInfinity)7const { indexZero } = require('fast-check/numbers')8console.log(indexZero)9const { indexOne } = require('fast-check/numbers')10console.log(indexOne)11const { indexMinusOne } = require('fast-check/numbers')12console.log(indexMinusOne)13const { indexMaxSafeInteger } = require('fast-check/numbers')14console.log(indexMaxSafeInteger)15const { indexMinSafeInteger } = require('fast-check/numbers')16console.log(indexMinSafeInteger)17const { indexMaxSafeIntegerMinusOne } = require('fast-check/numbers')18console.log(indexMaxSafeIntegerMinusOne)19const { indexMinSafeIntegerPlusOne } = require('fast-check/numbers')20console.log(indexMinSafeIntegerPlusOne)21const { indexMaxSafeIntegerPlusOne } = require('fast-check/numbers')22console.log(indexMaxSafeIntegerPlusOne)23const { indexMinSafeIntegerMinusOne } = require('fast-check/numbers')24console.log(indexMinSafeIntegerMinusOne)25const { indexMaxSafeIntegerDividedByTwo } = require('fast-check/numbers')26console.log(indexMaxSafeIntegerDividedByTwo)27const { indexMinSafeIntegerDividedByTwo } = require('fast-check/numbers')28console.log(indexMinSafeIntegerDividedByTwo)29const { indexMaxSafeIntegerMinusOneDividedByTwo } = require('fast-check/numbers')30console.log(indexMaxSafeIntegerMinusOneDividedByTwo)31const { indexMinSafeIntegerPlusOneDividedByTwo } = require('fast-check/numbers')32console.log(indexMinSafeIntegerPlusOneDividedByTwo)33const { indexMaxSafeIntegerPlusOneDividedByTwo } = require('fast-check/numbers')34console.log(indexMaxSafeIntegerPlusOneDividedByTwo)35const { indexMinSafeIntegerMinusOneDividedByTwo } = require('fast-check/numbers')36console.log(indexMinSafeIntegerMinusOneDividedByTwo)37const { indexMaxSafeIntegerDividedByMinusTwo } = require('fast-check/numbers')38console.log(indexMaxSafeIntegerDividedByMinusTwo)39const { indexMin
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!