faker icon indicating copy to clipboard operation
faker copied to clipboard

datatype.float does not return a float with given precision

Open Shinigami92 opened this issue 3 years ago • 5 comments

Describe the bug

The code indicates that float can take a number that will be used as precision value. But it returns always a integer number when using typoef options === 'number'

Reproduction

faker.seed(42)
faker.datatype.float(6) === 37452

Additional Info

I found this bug while rewriting the datatype tests in #344

Shinigami92 avatar Jan 29 '22 14:01 Shinigami92

This behavior might be intentional:

faker.datatype.number(1000) // 64000 faker.datatype.float(1000) // 64000

ST-DDT avatar Feb 22 '22 22:02 ST-DDT

After further investigation, I think you're right @ST-DDT - it looks like it's a feature, not a bug. The precision option is set, but the name precision might be a little unfortunate here. It does not refer only to significant decimal digits, as this unit test exposes.

faker.datatype.number(1000) // 64000 faker.datatype.float(1000) // 64000

Are you 100% sure about the faker.datatype.number(1000) output? I think this should be random number form [0;1000]?

faker.seed(42)
faker.datatype.number(1000) // 374

faker.seed(42)
faker.datatype.float(1000) // 37000

piotrekn avatar Feb 23 '22 01:02 piotrekn

Are you 100% sure about the faker.datatype.number(1000) output?

I meant faker.datatype.number({ precision: 1000 })); // 37000. IMO precision is a good word for it (or at least I cannot think of a better one right now).

ST-DDT avatar Feb 23 '22 09:02 ST-DDT

IMO precision is a good word for it (or at least I cannot think of a better one right now).

It resembles precision if represented with 10^n, but in reality it can take any Number. What is this number in those cases, then? How would you explain what's going on? I find it hard to put it in simple words and the term "precision" is misused imho :) I also find it very weird how the precision influence the outcome:

faker.seed(42);
faker.datatype.float({ precision: 1 }   // 37454

faker.seed(42);
faker.datatype.float({ precision: 9 }   // 37449

faker.seed(42);
faker.datatype.float({ precision: 10 }  // 37450

faker.seed(42);
faker.datatype.float({ precision: 11 }  // 37455

Correct me if I'm wrong, but this has little in common to precision as typically the mathematical precision is represented as integer, and refers to the number of significant decimal digits. I think it should work like Number.toPrecision, but in decimal notation: image I doubt it is expected of that Issue to create that massive breaking change right now, is it? It's not my call, I'm happy to go back and provide the PR anytime the expected behavior is established.

piotrekn avatar Feb 23 '22 20:02 piotrekn

I doubt it is expected of that Issue to create that massive breaking change right now, is it?

Yes, you are right. We have to put more thoughts into this.

ST-DDT avatar Feb 23 '22 23:02 ST-DDT

Superseded by https://github.com/faker-js/faker/issues/1596

  • #1596

ST-DDT avatar Jan 26 '23 17:01 ST-DDT