Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document float/double algorithms #640

Closed
dubzzz opened this issue Sep 9, 2023 · 7 comments · Fixed by #715
Closed

Document float/double algorithms #640

dubzzz opened this issue Sep 9, 2023 · 7 comments · Fixed by #715

Comments

@dubzzz
Copy link
Owner

dubzzz commented Sep 9, 2023

How to generate floats or doubles out of an integer value?

Note: We have snippets doing so in fast-check

@BorisTheBrave
Copy link

Yes, please add. Baffling to have a prng library with no way of generating floats.

@smohanty92
Copy link

How to generate floats or doubles out of an integer value?

Note: We have snippets doing so in fast-check

Hi! Where can I find the snippets?

@dubzzz
Copy link
Owner Author

dubzzz commented Jun 26, 2024

From https://fast-check.dev/docs/core-blocks/arbitraries/primitives/number/#float:

fc.noBias(fc.integer({ min: 0, max: (1 << 24) - 1 }).map((v) => v / (1 << 24)));
// generate a in range [0 (included), (1 << 24) - 1 (included)] then compute "a / (1 << 24)"

From https://fast-check.dev/docs/core-blocks/arbitraries/primitives/number/#double:

fc.noBias(
  fc
    .tuple(fc.integer({ min: 0, max: (1 << 26) - 1 }), fc.integer({ min: 0, max: (1 << 27) - 1 }))
    .map((v) => (v[0] * Math.pow(2, 27) + v[1]) * Math.pow(2, -53)),
);
// generate a in range [0 (included), (1 << 26) - 1 (included)]
// generate b in range [0 (included), (1 << 27) - 1 (included)]
// then compute "(a * Math.pow(2, 27) + b) * Math.pow(2, -53)"

@smohanty92
Copy link

fc.noBias(fc.integer({ min: 0, max: (1 << 24) - 1 }).map((v) => v / (1 << 24)));

Thanks!

I'm just doing import fc from 'fast-check' fc.noBias(fc.integer({ min: 0, max: (1 << 24) - 1 }).map((v) => v / (1 << 24)));

But get fc.noBias is not a function

I installed with pnpm and can see the Arbitrary class within fc

@dubzzz
Copy link
Owner Author

dubzzz commented Jun 26, 2024

No need to use fast-check if the only thing you need is to generate random floating point value. My example with fast-check was just a quick extract of a code doing the conversion of an integer value to a floating point one.

Regarding noBias, the fc.noBias has not been rolled-out yet. For now it should be fc.integer({ min: 0, max: (1 << 24) - 1 }).noBias().map((v) => v / (1 << 24)).

@smohanty92
Copy link

smohanty92 commented Jun 26, 2024

map((v) => v / (1 << 24)));

Thanks for the clarification.

I had tried that int method and it worked.
I also did,

import fc from 'fast-check'
const min = 1.5;
const max = 5.7;

const min32 = Math.fround(min);
const max32 = Math.fround(max);

// Generate a random float within the specified range
const randomFloatArbitrary = fc.float({ min: min32, max: max32 }).noBias()
const randomFloat = fc.sample(randomFloatArbitrary, 1, { seed: 42})[0];
console.log(randomFloat);

and that seemed to work just fine too.

@dubzzz
Copy link
Owner Author

dubzzz commented Jun 27, 2024

Documentation of pure-rand updated at: #715

dubzzz added a commit that referenced this issue Jun 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants