ITADN

Use Math.trunc in `toString()` instead of bitwise operator.

#74Pull RequestTonyRippy 创建于 2024-01-03
T
TonyRippycommented
The current implementation of `toString()` uses a bitwise operation to find the whole number part of a fraction: ```js str+= N / D | 0; ``` This has a side-effect of converting the result to a signed 32-bit integer value, which results in numeric overflow when formatting larger values. [More information about this here.](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/trunc#using_bitwise_no-ops_to_truncate_numbers) Using Math's built-in truncation will allow the code to work for larger values supported by Javascript's number type. A test was added that demonstrates this. Before this change, `(new Fraction(1e12)).toString()` returned `"-727379968"`. After this change, the same code correctly returns `"1000000000000"`.
合并状态:未合并 关闭于 2024-01-22 5 条评论