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

fix: format decimal precision problem #172

Closed
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion src/parser/content-evaluator-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ export class ContentEvaluatorModule implements Module {
currentComment.score = {
...(currentComment.score || { multiplier: 0 }),
relevance: new Decimal(currentRelevance).toNumber(),
reward: currentReward.toNumber(),
reward: new Decimal(currentReward).mul(100).round().div(100).toNumber(),
Copy link
Member

Choose a reason for hiding this comment

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

Multiplying and dividing seems wrong. It just cancels itself out.

Copy link
Member

Choose a reason for hiding this comment

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

@0x4007 It doesn't because of the round, so this basically is a 3 decimal rounding operation. But I don't think this is necessary because Decimaljs can handle an "infinite" amount of decimals. currentReward should be a finite number during all operations.

Copy link
Author

@FibrinLab FibrinLab Oct 28, 2024

Choose a reason for hiding this comment

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

Just to clarify, are you suggesting that rounding should be avoided altogether to keep maximum precision until the final display stage? I understood from your comment that decimal.js can handle high precision throughout all operations without the need for intermediate rounding. If so, would it be preferable to apply formatting only at the end for display purposes, rather than during calculations? @gentlementlegen

Copy link
Member

Choose a reason for hiding this comment

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

Yes exactly.

};
}

Expand Down
2 changes: 1 addition & 1 deletion src/parser/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export class Processor {
}
}

return totalReward.toNumber();
return new Decimal(totalReward).mul(100).round().div(100).toNumber();
}
}

Expand Down
7 changes: 6 additions & 1 deletion src/parser/user-extractor-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,12 @@ export class UserExtractorModule implements Module {
data.self?.assignees?.forEach((assignee) => {
const task = data.self
? {
reward: new Decimal(this._extractTaskPrice(data.self)).mul(this._getTaskMultiplier(data.self)).toNumber(),
reward: new Decimal(this._extractTaskPrice(data.self))
.mul(this._getTaskMultiplier(data.self))
.mul(100)
.round()
.div(100)
.toNumber(),
multiplier: this._getTaskMultiplier(data.self).toNumber(),
}
: undefined;
Expand Down
Loading