Git Product home page Git Product logo

Comments (14)

matthewmayer avatar matthewmayer commented on June 4, 2024 1

You could do some kind of special case for precisions which are powers of 10 (most common case), like:

const logPrecision = Math.log10(precision)
if (Number.isInteger(logPrecision)) {
   return parseFloat((int / factor).toFixed(-logPrecision));
} else {
   return int / factor;
}

from faker.

matthewmayer avatar matthewmayer commented on June 4, 2024 1

Similarly you would still have issues with precisions which are not powers of 10 e.g.:

> [ ...new Set( Array.from({ length: 500 }, () => faker.number.float({ min: 0, max: 1, precision: 0.3, }) ) ), ].sort();
[ 0, 0.3, 0.6, 0.8999999999999999 ]

from faker.

matthewmayer avatar matthewmayer commented on June 4, 2024 1

Another option might be if 1/precision is "almost" an integer (within a small epsilon value) then we round it to an integer

from faker.

matthewmayer avatar matthewmayer commented on June 4, 2024 1

I think i have a fairly neat solution in #2581 - please take a look

from faker.

xDivisionByZerox avatar xDivisionByZerox commented on June 4, 2024

Hi @clocke3 , thank you for your bug.
I'm currently not at home, so I can't verify this (the minimal reproducible example link doesn't work on my phone). I'll give more feedback in a bit.

From your description (and screenshot!) this seems to be legit.
You selected that you are willing to create a PR to fix this. If that's the case, you are welcome to do so.

from faker.

ST-DDT avatar ST-DDT commented on June 4, 2024

The example doesn't work for me either.

Error: Cannot find module '@faker-js/[email protected]'

I assume the bug is related to a float precision issue.

from faker.

clocke3 avatar clocke3 commented on June 4, 2024

@xDivisionByZerox I apologize I am new to this issue making and checked the section on "creating a PR for issue" on mistake.

from faker.

clocke3 avatar clocke3 commented on June 4, 2024

@ST-DDT Yes it is. Looks like it existed in 8.0.2, 8.2.0, and I did test it out using 8.3.1 locally. I will try to see if I can produce reproduction code using 8.3.1.

from faker.

ST-DDT avatar ST-DDT commented on June 4, 2024

@xDivisionByZerox I apologize I am new to this issue making and checked the section on "creating a PR for issue" on mistake.

No problem at all. If you have a suggestion for an algorithm that might fix this issue feel free to post it here anyway.

from faker.

ST-DDT avatar ST-DDT commented on June 4, 2024

@ST-DDT Yes it is. Looks like it existed in 8.0.2, 8.2.0, and I did test it out using 8.3.1 locally. I will try to see if I can produce reproduction code using 8.3.1.

I don't think we changed anything significant in the method in this major version, so the problem is likely still there.
But thank you for your help.

from faker.

matthewmayer avatar matthewmayer commented on June 4, 2024

Definitely a JS floating point issue. Not all floats can be exactly represented in Javascript.

if (precision !== undefined) {
      if (precision <= 0) {
        throw new FakerError(`Precision should be greater than 0.`);
      }

      const factor = 1 / precision;
      const int = this.int({
        min: min * factor,
        max: max * factor,
      });
      return int / factor;
    }

The code const factor = 1 / precision gives a non-integer for certain values of precision:

> 1/0.1
10
> 1/0.01
100
> 1/0.001
1000
> 1/0.0001
10000
> 1/0.00001
99999.99999999999
> 1/0.000001
1000000
> 1/0.0000001
10000000
> 1/0.00000001
100000000
> 1/0.000000001
999999999.9999999
> 1/0.0000000001
10000000000

from faker.

ST-DDT avatar ST-DDT commented on June 4, 2024

You could do some kind of special case for precisions which are powers of 10 (most common case), like:

const logPrecision = Math.log10(precision)
if (Number.isInteger(logPrecision)) {
   return parseFloat((int / factor).toFixed(-logPrecision));
} else {
   return int / factor;
}

This might also work for non power 10 cases.

const logPrecision = precision.toString().replace(/\d+\./, '').length;
return parseFloat((int / factor).toFixed(logPrecision));

Not sure about the performance implications though.

from faker.

clocke3 avatar clocke3 commented on June 4, 2024

Any movement on this in development or still brainstorming?

from faker.

matthewmayer avatar matthewmayer commented on June 4, 2024

Still under discussion. As a workaround,

parseFloat(faker.number.float({precision:0.00001, min:X, max:Y}).toFixed(5)) should do what you want

from faker.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.