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

Normalization in other implementations #89

Open
sffc opened this issue Nov 27, 2023 · 6 comments
Open

Normalization in other implementations #89

sffc opened this issue Nov 27, 2023 · 6 comments

Comments

@sffc
Copy link

sffc commented Nov 27, 2023

This week's slides discuss precedent for decimals from other programming languages. I did some quick research on the listed programming languages to see if they support trailing zeros.

Java: YES

https://docs.oracle.com/javase/8/docs/api/java/math/BigDecimal.html

The rounding mode determines how any discarded trailing digits affect the returned result.

stripTrailingZeros()
Returns a BigDecimal which is numerically equal to this one but with any trailing zeros removed from the representation.

C#: YES

https://learn.microsoft.com/en-us/dotnet/api/system.decimal?view=net-8.0

The scaling factor also preserves any trailing zeros in a Decimal number. Trailing zeros do not affect the value of a Decimal number in arithmetic or comparison operations. However, trailing zeros might be revealed by the ToString method if an appropriate format string is applied.

Python: YES

https://docs.python.org/3/library/decimal.html

The decimal module incorporates a notion of significant places so that 1.30 + 1.20 is 2.50. The trailing zero is kept to indicate significance. This is the customary presentation for monetary applications. For multiplication, the “schoolbook” approach uses all the figures in the multiplicands. For instance, 1.3 * 1.2 gives 1.56 while 1.30 * 1.20 gives 1.5600.

Ruby: Wasn't able to determine for sure in the short amount of time I took to research, but I think it does not support them. Ruby defines decimals as: "arbitrary-precision floating point decimal arithmetic". When I play with them in irb I can't seem to get a trailing zero to roundtrip.

Postgresql: YES

https://www.postgresql.org/docs/8.1/datatype.html
https://stackoverflow.com/questions/26920157/postgresql-adds-trailing-zeros-to-numeric

An example of the data I am inserting is 0.75 in the numeric(9,3) column and the value stored is 0.750. Another example for the numeric(9,4) column: I insert the value 12 and the DB is holding 12.0000.

@jessealama

@sffc
Copy link
Author

sffc commented Nov 27, 2023

I didn't go this deep but it would be interesting to see how the prior art handles the "middle ground" proposal suggested in the slide deck, namely whether lessThan and equals pay attention to the trailing zeros.

@jirkamarsik
Copy link
Collaborator

So in conclusion, Java, C# and Python's behavior is closest to the "middle ground" proposal. The primary comparison operators care only about the mathematical value and ignore trailing zeroes. The string conversion exposes trailing zeroes. On the other hand, Ruby adopts the always-normalize strategy.

On top of that, Python offers a compare_total method that can provide a total ordering of the decimal values by taking into account trailing zeroes and Java's equals method compares both value and scale, so that BigDecimals which are equal according to equals are indistinguishable using the other BigDecimal methods.

@sffc
Copy link
Author

sffc commented Dec 29, 2023

In Temporal we had similar questions about what fields to consider in .compare and .equals; what we landed on was .compare being more lenient and .equals being more strict, meaning .compare() == 0 doesn't imply .equals().

tc39/proposal-temporal#523

@ljharb @ptomato

@jessealama
Copy link
Collaborator

Just to update this discussion with the status quo:

  • We propose =, ≠, <, ≤, >, and ≥ that all compare mathematical values;
  • We propose a compare method that works on all decimal values (even NaNs and infinities) and compares the full decimal data, allowing the programmer to distinguish mathematically equal but distinct-in-Decimal128-world values. The range will be { -1, 0, 1, NaN } (...the latter because of NaN pollution).

@sffc
Copy link
Author

sffc commented Jul 22, 2024

It looks like Python has a compare_total function

$ python3
Python 3.11.8 (main, May 26 2024, 00:40:16) [GCC 13.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from decimal import Decimal
>>> Decimal("1.00") == Decimal("1.0")
True
>>> Decimal("1.00") < Decimal("1.0")
False
>>> Decimal("1.00").compare_total(Decimal("1.0"))
Decimal('-1')

@nicolo-ribaudo
Copy link
Member

The answer for Postgresql is "NO", not "YES". Postgres does not store the precision together with the number, and instead it's part of the type of the column (so, there is no difference between trying to store 12.0 or 12.00 in any given column).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants