amazon-ion / ion-docs

Source for the GitHub Pages for Ion.
https://amazon-ion.github.io/ion-docs/
Apache License 2.0
22 stars 22 forks source link

Swaps the order of the exponent and coefficient components of decimals #286

Closed tgregg closed 8 months ago

tgregg commented 8 months ago

Description of changes:

Alternative considered:

One downside of the new order of decimal components is that integer-value decimals (other than 0d0) require one more byte to encode because an exponent of 0 can no longer be encoded implicitly. It is possible, instead of reclaiming 0x6F for length-15 decimals as proposed here, to use 0x6F to encode decimals with implicit exponent 0. However, this has the drawback of increased complexity, as the coefficient would have to be a FlexInt, requiring the reverse-and-shift logic for arbitrarily large values that the other changes in this PR propose to avoid. Discussion on this topic is welcomed.


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

zslayton commented 8 months ago

Another alternative: the set of built-in macros is not finalized, but we've often discussed having a make_decimal macro that took two variable-width integers: the coefficient and the exponent. If we follow through with that, it becomes trivial to define a macro like this:

(macro int_to_dec (x)
    (make_decimal x 0))

This macro can cost a single byte to invoke and as little as a single byte to write out the int value, making it as efficient as having a purpose-built opcode to achieve the same.

popematt commented 8 months ago

That's a good point, Zack. I think that fully mitigates any concern about integer values being enlarged by the proposed change.