Skip to content

change default precision of faker.number.float #1655

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

Closed
matthewmayer opened this issue Dec 11, 2022 · 12 comments · Fixed by #1675
Closed

change default precision of faker.number.float #1655

matthewmayer opened this issue Dec 11, 2022 · 12 comments · Fixed by #1675
Labels
c: feature Request for new feature p: 1-normal Nothing urgent s: needs decision Needs team/maintainer decision
Milestone

Comments

@matthewmayer
Copy link
Contributor

I note that faker.helpers.float defaults to a precision of 0.01, which means this is biased for small percentages. What's the cleanest way to request the max precision for the random float?

Maybe open a new issue for that (max precision float)?

Originally posted by @ST-DDT in #1654 (comment)

@matthewmayer
Copy link
Contributor Author

I was thinking we could add a new method like faker.number.fraction (naming?) which simply returns a random number in the interval [0,1).

The current implementation of faker.number.float always uses faker.number.int so it isn't ideal for getting full precision floats.

this could use a new method on Mersenne, e.g.

nextReal(): number {
      return twister.genrandReal2();
},

@matthewmayer
Copy link
Contributor Author

Other alternative ideas

  • Make this the default behavior of faker.number.float when no precision is passed
  • Add the function to Mersenne, but only use it internally, don't make a new public method for this yet

@ST-DDT
Copy link
Member

ST-DDT commented Dec 11, 2022

@matthewmayer Do explicitly need a max precision float or do you just need more than 2 fractional digits?
AFAICT 8 is the max we can safely generate, after that the float precision starts taking its toll.

@ST-DDT ST-DDT added c: feature Request for new feature p: 1-normal Nothing urgent s: awaiting more info Additional information are requested labels Dec 11, 2022
@matthewmayer
Copy link
Contributor Author

Doesn't have to be fully max precision. In reality for any real-world use case e.g. in #1654 5 or 6 digits is usually more than enough.

@ST-DDT ST-DDT added s: needs decision Needs team/maintainer decision and removed s: awaiting more info Additional information are requested labels Dec 11, 2022
@ST-DDT ST-DDT moved this to Todo in Faker Roadmap Dec 11, 2022
@ST-DDT
Copy link
Member

ST-DDT commented Dec 11, 2022

Having a few more decimal digits from float (e.g. 6) aligns with my expectation regarding that method so I'm fine with it. What do the others think?

@Shinigami92
Copy link
Member

I made some extrem tests

image

But unfortunately every precision I tried has some caveats 😕
Somehow I feel like 1e-14 is the best to pick, but nothing I can proof

@ST-DDT
Copy link
Member

ST-DDT commented Dec 12, 2022

Please note that we loose precision due to the math in faker.number.float

@matthewmayer
Copy link
Contributor Author

Also given the way Mersenne twister works you're only going to get 2^32 possible values even though there are an way more possible float values than that.

I don't think the precise precision matters that much as long as it's considerably smaller than any reasonable weight value.

If you care that much about precision and error values, you are almost certainly not going to use the JavaScript Number class

@ST-DDT
Copy link
Member

ST-DDT commented Dec 12, 2022

Is 0.000_001 (10 ** -6) for precision ok for everyone?
Please use thumps for voting.

@matthewmayer
Copy link
Contributor Author

Just to clarify, are you talking about modifying the default precision of faker.number.float or adding a new method for generating float values?

@ST-DDT
Copy link
Member

ST-DDT commented Dec 13, 2022

Just to clarify, are you talking about modifying the default precision of faker.number.float or adding a new method for generating float values?

Modifying the existing. I assume that the current percentage result might be useful for some as well, so we could theoretically introduce a new method/alias with the old precision as default, but thats optional for me.

@matthewmayer
Copy link
Contributor Author

Cool I agree. The current behavior was somewhat surprising to me. If we change the default then in most cases if you want a fixed number of decimal places you would be much better served by doing

faker.number.float().toFixed(2)

@matthewmayer matthewmayer changed the title Max precision float in [0,1)? change default precision of faker Dec 13, 2022
@matthewmayer matthewmayer changed the title change default precision of faker change default precision of faker.number.float Dec 13, 2022
@github-project-automation github-project-automation bot moved this from Todo to Done in Faker Roadmap Jan 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c: feature Request for new feature p: 1-normal Nothing urgent s: needs decision Needs team/maintainer decision
Projects
No open projects
Status: Done
Development

Successfully merging a pull request may close this issue.

3 participants