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

feat: precise number calculation #779

Closed
wants to merge 5 commits into from
Closed
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/datatype.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import { deprecated } from './internal/deprecated';
* Module to generate various primitive values and data types.
*/
export class Datatype {
public preciseNumberDivider:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does someone have a better name?

I also need to declare and use it like this way, because if I eagerly provide a default function, it counts as datatype module function and will be bound by the constructor and the all_functional test fails badly.

| ((value: number, precision: number) => number)
| null = null;

constructor(private readonly faker: Faker) {
// Bind `this` so namespaced is working correctly
for (const name of Object.getOwnPropertyNames(Datatype.prototype)) {
Expand Down Expand Up @@ -56,8 +60,11 @@ export class Datatype {
this.faker.mersenne.rand(max / precision, min / precision)
);

// Workaround problem in float point arithmetics for e.g. 6681493 / 0.01
return randomNumber / (1 / precision);
return (
this.preciseNumberDivider?.(randomNumber, precision) ??
// Workaround problem in float point arithmetics for e.g. 6681493 / 0.01
randomNumber / (1 / precision)
);
}

/**
Expand Down