Best JavaScript code snippet using fast-check-monorepo
number.ts
Source:number.ts
...72 // Using the remainder operation is only safe in the integer space.73 if (Math.abs(value) <= Number.MAX_SAFE_INTEGER && Number.isSafeInteger(multiple)) return value % multiple === 0;74 // It gets tricky for non-integer and especially small divisors. Using remainder does not work for small divisors, e.g.75 // `1 % 0.1 === 0.09999999999999995`. As a workaround, we convert both operands to integers first.76 const valueParts = decomposeFloat(value);77 const multipleParts = decomposeFloat(multiple);78 // Now sync the exponent, so we can only focus on the significands79 const exponent = Math.min(valueParts.exponent, multipleParts.exponent);80 const valueSignificand = significandForGivenExponent(valueParts, exponent);81 const multipleSignificand = significandForGivenExponent(multipleParts, exponent);82 return valueSignificand % multipleSignificand === ZERO;83}84// rewrite float to an integer and exponent, for example 123456789.222 to 123456789222e-385function decomposeFloat(n: number) {86 const [significand, exponent] = n.toExponential().split('e') as [string, string];87 // Now denormalize the significand to get rid of the decimal point.88 const [base, frac] = significand.split('.') as [string, string | undefined];89 return {90 significand: BIG(base + (frac || '')),91 exponent: +exponent - (frac ? frac.length : 0),92 };93}94function significandForGivenExponent(input: ReturnType<typeof decomposeFloat>, exponent: number) {95 return BIG(input.significand) * TEN ** BIG(input.exponent - exponent);96}97type Bound<T extends 'min' | 'max'> = {98 // The required fields for the given bound (min or max):99 [K in T | `${T}Exclusive`]?: number;...
FloatHelpers.spec.ts
Source:FloatHelpers.spec.ts
...14 MIN_VALUE_32,15} from '../../../../../src/arbitrary/_internals/helpers/FloatHelpers';16describe('decomposeFloat', () => {17 it('should properly decompose basic values', () => {18 expect(decomposeFloat(0)).toEqual({ exponent: -126, significand: 0 });19 expect(decomposeFloat(1)).toEqual({ exponent: 0, significand: 1 });20 expect(decomposeFloat(128)).toEqual({ exponent: 7, significand: 1 });21 expect(decomposeFloat(201)).toEqual({ exponent: 7, significand: 1.5703125 });22 });23 it('should properly decompose negative values', () => {24 expect(decomposeFloat(-0)).toEqual({ exponent: -126, significand: -0 });25 expect(decomposeFloat(-1)).toEqual({ exponent: 0, significand: -1 });26 });27 it('should properly decompose extreme values', () => {28 expect(decomposeFloat(MAX_VALUE_32)).toEqual({ exponent: 127, significand: 1 + (2 ** 23 - 1) / 2 ** 23 });29 expect(decomposeFloat(MIN_VALUE_32)).toEqual({ exponent: -126, significand: 2 ** -23 });30 expect(decomposeFloat(EPSILON_32)).toEqual({ exponent: -23, significand: 1 });31 expect(decomposeFloat(1 + EPSILON_32)).toEqual({ exponent: 0, significand: 1 + 2 ** -23 });32 });33 it('should decompose a 32-bit float into its equivalent (significand, exponent)', () => {34 fc.assert(35 fc.property(float32raw(), (f32) => {36 // Arrange37 fc.pre(isFiniteNotNaN32bits(f32));38 // Act39 const { exponent, significand } = decomposeFloat(f32);40 // Assert41 expect(significand * 2 ** exponent).toBe(f32);42 })43 );44 });45});46describe('floatToIndex', () => {47 it('should properly compute indexes', () => {48 expect(floatToIndex(0)).toBe(0);49 expect(floatToIndex(MIN_VALUE_32)).toBe(1);50 expect(floatToIndex(2 * MIN_VALUE_32)).toBe(2);51 expect(floatToIndex(3 * MIN_VALUE_32)).toBe(3);52 // EPSILON_32 === 1. * 2**-23 --> m = 1, e = -2353 // index(EPSILON_32) = 2**24 + (-23 - (-126) -1) * 2**23...
FloatHelpers.ts
Source:FloatHelpers.ts
...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 *...
Using AI Code Generation
1const fc = require("fast-check");2const { decomposeFloat } = require("fast-check/lib/types/float/FloatArbitrary");3const { FloatNextConstraints } = require("fast-check/lib/types/float/FloatNextConstraints");4const { FloatNextConstraintsBuilder } = require("fast-check/lib/types/float/FloatNextConstraintsBuilder");5const { FloatNextConstraintsBuilderWithMin } = require("fast-check/lib/types/float/FloatNextConstraintsBuilderWithMin");6const { FloatNextConstraintsBuilderWithMax } = require("fast-check/lib/types/float/FloatNextConstraintsBuilderWithMax");7const { FloatNextConstraintsBuilderWithNext } = require("fast-check/lib/types/float/FloatNextConstraintsBuilderWithNext");8const { FloatNextConstraintsBuilderWithBias } = require("fast-check/lib/types/float/FloatNextConstraintsBuilderWithBias");9const { FloatNextConstraintsBuilderWithBiasAndMin } = require("fast-check/lib/types/float/FloatNextConstraintsBuilderWithBiasAndMin");10const { FloatNextConstraintsBuilderWithBiasAndMax } = require("fast-check/lib/types/float/FloatNextConstraintsBuilderWithBiasAndMax");11const { FloatNextConstraintsBuilderWithBiasAndNext } = require("fast-check/lib/types/float/FloatNextConstraintsBuilderWithBiasAndNext");12const { FloatNextConstraintsBuilderWithBiasAndNextAndMin } = require("fast-check/lib/types/float/FloatNextConstraintsBuilderWithBiasAndNextAndMin");13const { FloatNextConstraintsBuilderWithBiasAndNextAndMax } = require("fast-check/lib/types/float/FloatNextConstraintsBuilderWithBiasAndNextAndMax");14const { FloatNextConstraintsBuilderWithBiasAndNextAndNext } = require("fast-check/lib/types/float/FloatNextConstraintsBuilderWithBiasAndNextAndNext");15const { FloatNextConstraintsBuilderWithBiasAndNextAndNextAndMin } = require("fast-check/lib/types/float/FloatNextConstraintsBuilderWithBiasAndNextAndNextAndMin");16const { FloatNextConstraintsBuilderWithBiasAndNextAndNextAndMax } = require("fast-check/lib/types/float/FloatNextConstraintsBuilderWithBiasAndNextAndNextAndMax");17const { FloatNextConstraintsBuilderWithBiasAndNextAndNextAndNext } = require("fast-check/lib/types/float/FloatNextConstraintsBuilderWithBiasAndNextAndNextAndNext");18const { FloatNextConstraintsBuilderWithBiasAndNextAndNextAndNextAndMin } =
Using AI Code Generation
1const { decomposeFloat } = require("fast-check");2const fc = require("fast-check");3const { decomposeFloat } = require("fast-check");4fc.assert(5 fc.property(fc.float(), (f) => {6 const { exponent, significand } = decomposeFloat(f);7 return f === significand * 2 ** exponent;8 })9);10const { decomposeFloat } = require("fast-check");11const fc = require("fast-check");12const { decomposeFloat } = require("fast-check");13fc.assert(14 fc.property(fc.float(), (f) => {15 const { exponent, significand } = decomposeFloat(f);16 return f === significand * 2 ** exponent;17 })18);19const { decomposeFloat } = require("fast-check");20const fc = require("fast-check");21const { decomposeFloat } = require("fast-check");22fc.assert(23 fc.property(fc.float(), (f) => {24 const { exponent, significand } = decomposeFloat(f);25 return f === significand * 2 ** exponent;26 })27);28const { decomposeFloat } = require("fast-check");29const fc = require("fast-check");30const { decomposeFloat } = require("fast-check");31fc.assert(32 fc.property(fc.float(), (f) => {33 const { exponent, significand } = decomposeFloat(f);34 return f === significand * 2 ** exponent;35 })36);37const { decomposeFloat } = require("fast-check");38const fc = require("fast-check");39const { decomposeFloat } = require("fast-check");40fc.assert(41 fc.property(fc.float(), (f) => {42 const { exponent, significand } = decomposeFloat(f);43 return f === significand * 2 ** exponent;44 })45);46const { decomposeFloat } = require("fast-check");47const fc = require("fast-check");48const { decomposeFloat } = require("fast-check");49fc.assert(50 fc.property(fc.float(), (f) => {
Using AI Code Generation
1 throw err;2 at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)3 at Function.Module._load (internal/modules/cjs/loader.js:562:25)4 at Module.require (internal/modules/cjs/loader.js:692:17)5 at require (internal/modules/cjs/helpers.js:25:18)6 at Object.<anonymous> (/Users/.../workspace/fast-check-monorepo/fast-check/test3.js:1:18)7 at Module._compile (internal/modules/cjs/loader.js:778:30)8 at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)9 at Module.load (internal/modules/cjs/loader.js:653:32)10 at tryModuleLoad (internal/modules/cjs/loader.js:593:12)11 at Function.Module._load (internal/modules/cjs/loader.js:585:3)12 throw err;13 at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)14 at Function.Module._load (internal/modules/cjs/loader.js:562:25)15 at Module.require (internal/modules/cjs/loader.js:
Using AI Code Generation
1const fc = require('fast-check');2const { decomposeFloat } = require('fast-check');3const { assert } = require('chai');4const assert = require('assert');5const { expect } = require('chai');6const { should } = require('chai');7const chai = require('chai');8const chaiAsPromised = require("chai-as-promised");9chai.use(chaiAsPromised);10chai.should();11describe('decomposeFloat', () => {12 it('should decompose a number into a sign, an exponent and a significand', () =>13 fc.assert(14 fc.property(fc.float(), f => {15 const { sign, exponent, significand } = decomposeFloat(f);16 expect(sign).to.be.oneOf([-1, 1]);17 expect(exponent).to.be.a('number');18 expect(significand).to.be.a('number');19 expect(f).to.equal(sign * significand * Math.pow(2, exponent));20 })21 ));22});23const fc = require('fast-check');24const { decomposeFloat } = require('fast-check');25const { assert } = require('chai');26const assert = require('assert');27const { expect } = require('chai');28const { should } = require('chai');29const chai = require('chai');30const chaiAsPromised = require("chai-as-promised");31chai.use(chaiAsPromised);32chai.should();33describe('decomposeFloat', () => {34 it('should decompose a number into a sign, an exponent and a significand', () =>35 fc.assert(36 fc.property(fc.float(), f => {37 const { sign, exponent, significand } = decomposeFloat(f);38 expect(sign).to.be.oneOf([-1, 1]);39 expect(exponent).to.be.a('number');40 expect(significand).to.be.a('number');41 expect(f).to.equal(sign * significand * Math.pow(2, exponent));42 })43 ));44});45const fc = require('fast-check');46const { decomposeFloat } = require('fast-check');47const { assert }
Using AI Code Generation
1const fc = require('fast-check');2const { decomposeFloat } = require('fast-check-monorepo');3const test3 = (value) => {4 console.log('value : ' + value);5 console.log('decomposeFloat(value) : ' + decomposeFloat(value));6}7test3(0.1);8test3(0.2);9test3(0.3);10test3(0.4);11test3(0.5);12test3(0.6);13test3(0.7);14test3(0.8);15test3(0.9);16test3(1.1);17test3(1.2);18test3(1.3);19test3(1.4);20test3(1.5);21test3(1.6);22test3(1.7);23test3(1.8);24test3(1.9);25decomposeFloat(value) : 0.1000000000000000126decomposeFloat(value) : 0.2000000000000000127decomposeFloat(value) : 0.3000000000000000428decomposeFloat(value) : 0.4000000000000000229decomposeFloat(value) : 0.530decomposeFloat(value) : 0.6000000000000000931decomposeFloat(value) : 0.7000000000000000732decomposeFloat(value) : 0.8000000000000000433decomposeFloat(value) : 0.9000000000000000234decomposeFloat(value) : 1.100000000000000135decomposeFloat(value) : 1.2000000000000002
Using AI Code Generation
1const fc = require("fast-check");2const { decomposeFloat } = require("@fast-check/arbitrary/float");3const test3 = () => {4 console.log("test3");5 const decomposed = decomposeFloat(0.000001);6 console.log(decomposed);7};8test3();9const fc = require("fast-check");10const { decomposeFloat } = require("@fast-check/arbitrary/float");11const test4 = () => {12 console.log("test4");
Using AI Code Generation
1const fc = require('fast-check');2const decomposeFloat = require('fast-check/lib/utils/FloatDecomposition').decomposeFloat;3function decomposeAndReconstructFloat(float) {4 let result = false;5 const decomposedFloat = decomposeFloat(float);6 const mantissa = decomposedFloat.mantissa;7 const exponent = decomposedFloat.exponent;8 const reconstructedFloat = mantissa * Math.pow(2, exponent);9 if (float === reconstructedFloat) {10 result = true;11 }12 return result;13}14const property = fc.property(fc.float(), (float) => {15 return decomposeAndReconstructFloat(float);16});17describe('decompose and reconstruct float', () => {18 it('should pass', () => {19 fc.assert(property);20 });21});
Using AI Code Generation
1var fc = require('fast-check');2var assert = require('assert');3function decomposeFloat(f) {4 var sign = f < 0 ? -1 : 1;5 if (f === 0) return [0, 0, 0];6 var exponent = Math.floor(Math.log2(Math.abs(f)));7 var mantissa = Math.abs(f) * Math.pow(2, -exponent);8 return [sign, exponent, mantissa];9}10function composeFloat(sign, exponent, mantissa) {11 if (sign === 0) return 0;12 return sign * mantissa * Math.pow(2, exponent);13}14function testDecomposeFloat(f) {15 var [sign, exponent, mantissa] = decomposeFloat(f);16 var f2 = composeFloat(sign, exponent, mantissa);17 return f2 === f;18}19var result = fc.assert(fc.property(fc.float(), testDecomposeFloat));20console.log(result);21var fc = require('fast-check');22var assert = require('assert');23function decomposeFloat(f) {24 var sign = f < 0 ? -1 : 1;25 if (f === 0) return [0, 0, 0];26 var exponent = Math.floor(Math.log2(Math.abs(f)));
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!!