Skip to content

Completely remove NaNs, infinities, signed zeroes and quantums. Remember their original motivation #172

Open
@safinaskar

Description

@safinaskar

Current version of the spec includes NaNs, infinities and signed zeroes. So, we would like to ask a question: why they was introduced originally? Here is the answer.

First, they was designed for calculations, which often appear in physics, engineering and calculus, for example ( https://en.wikipedia.org/wiki/IEEE_754 ):

Moreover, the choices of special values returned in exceptional cases were designed to give the correct answer in many cases. For instance, under IEEE 754 arithmetic, continued fractions such as R(z) := 7 − 3/[z − 2 − 1/(z − 7 + 10/[z − 2 − 2/(z − 3)])] will give the correct answer on all inputs, as the potential divide by zero, e.g. for z = 3, is correctly handled by giving +infinity, and so such exceptions can be safely ignored.

In other words, if you calculate 1 / (1 / 0), then you will get 0, and this is what you likely expected (at least in physics).

Second, they are designed for programming languages without exceptions, so "always produce some answer" was a requirement.

Neither of these reasons applies to our proposal. Decimal type is designed not for calculating engineering formulas such as above (there are normal binary floating point numbers for this), but for calculating money. And JavaScript does have exceptions.

So, I propose full removal of NaNs, infinities and signed zeroes.

You may say "but IEEE 754 Decimal128 has NaNs, etc". Yes, it does. This is just cargo cult. It seems they just copied everything from binary floating point numbers without thinking. This is just an error. So, let's fix this error! This will not create significant burden for implementors. They just will throw exception if they get NaNs or infinities.

Also, I propose removal of quantums. I don't know what is their original motivation, but I suppose their motivation was engineering, too.

Let's assume I measured length of pencil and said "it's length is 10.00 cm". What does 10.00 mean? Why there are extra zeroes here? "10.00 cm" means that length of pencil is between 9.995 cm and 10.005 cm (see https://en.wikipedia.org/wiki/Significant_figures ). I. e. extra zeroes here convey information about precision. But, again, all these applies to physics and engineering. All these doesn't make any sense for money. There is no difference between 10.00 usd and 10.000 usd. Both numbers mean exactly 10 usd with infinite precision.

So my proposal is: completely remove quantums. I. e. make them absolutely unobservable (it is okay if implementation uses them internally if they are not exposed).

You may say: "but what if user wants to convert monetary number to string with extra zeroes? What if he wants to get string "10.00 usd"?" This question is completely unrelated to quantums in their original form. Again: quantums are needed for tracking precision/accuracy as result of physical measurements. They have nothing to do with printing strings like "10.00 usd" on a screen. If you want to get string "10.00", just create custom formatting function. But don't carry quantum with number.

You may say: "But other libraries for decimal numbers have quantums, for example rust_decimal ( https://crates.io/crates/rust_decimal )". Yes, it does. And this is error. They copied well-known engineering convention of significant figures to their library without thinking.

Now useful links:

  • IEEE floating point is like having a built-in Stradivari in each home appliance ( Consistency: how to defeat the purpose of IEEE floating point) https://yosefk.com/blog/consistency-how-to-defeat-the-purpose-of-ieee-floating-point.html . Excellent article, describing why IEEE floating point designed so strange
  • https://en.wikipedia.org/wiki/IEEE_754 . Good article, especially part on rationale ( https://en.wikipedia.org/wiki/IEEE_754#Design_rationale ). I suggest reading the article in full and following links. Don't be confused by the phrase "In 1977 those features were designed into the Intel 8087 to serve the widest possible market... Error-analysis tells us how to design floating-point arithmetic, like IEEE Standard 754, moderately tolerant of well-meaning ignorance among programmers". Yes, IEEE 754 is not for experts only. It is also for "ignorant" programmers. But it is not for money! All these NaNs and infinities are good both for experts, performing some physics-related calculations, and for newbies, performing some physics-related calculations. But not for monetary calculations!!!
  • Already mentioned https://en.wikipedia.org/wiki/Significant_figures

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions