tc39 / proposal-decimal

Built-in decimal datatype in JavaScript
http://tc39.es/proposal-decimal/
422 stars 15 forks source link

Remainder produces spurious negative values #117

Open waldemarhorwat opened 1 month ago

waldemarhorwat commented 1 month ago

The remainder operation should be specified the same way as for Numbers.

The remainder operation in IEEE 754 is designed to have nice properties for numeric experts. It's confusing to most users — it does not match what users typically think of as remainder. Also, IEEE 754 remainder is explicitly specified to not respect the rounding mode you give it.

Here's the problem. All numbers below are meant to be Decimal and I'm using % to indicate remainder; I'm skipping the boilerplate function calls and conversions.

42 % 10 produces a remainder of 2 in most languages (including ECMAScript's Numbers) and in IEEE 754. 46 % 10 produces a remainder of 6 in most languages (including ECMAScript's Numbers). IEEE 754's remainder produces -4 instead. This is not what users would expect.

littledan commented 1 month ago

Seems reasonable for us to follow JS's pattern here rather than IEEE's.

jessealama commented 1 month ago

That sounds fine. I wasn't aware of that aspect of IEEE 754.

FWIW the remainder operation that's in the NPM library that accompanies this proposal follows the General Decimal Arithmetic Specification, which deviates from IEEE 754 in the way you describe.

waldemarhorwat commented 1 month ago

The General Decimal Arithmetic Specification has both kinds of remainder. They call the IEEE 754 version remainder-near.