Skip to content

i64.as-number could be implemented in EO, replicating number.as-i64 approach #5471

Description

@yegor256

The as-number object in eo-runtime/src/main/eo/i64.eo:30 ([] > as-number /number) is a native atom. The decisive point is that its inverse is already pure EO: number.as-i64 in eo-runtime/src/main/eo/number.eo:31-66 disassembles the IEEE-754 double by hand — reading the sign with and 80-.., extracting the biased exponent with and 7F-F0-.. and right 52, rebuilding the mantissa with the implicit leading bit, and shifting it into an integer. The forward direction is the mirror image of that same bit surgery, so the primitives are demonstrably sufficient.

Converting an i64 to a double is: take the sign and the magnitude, find the position of the highest set bit to get the unbiased exponent (a handful of gt/shift comparisons, no long loop needed), add the bias of 1023, drop the implicit leading one and place the remaining bits as the 52-bit mantissa, then assemble sign | exponent | mantissa into eight bytes and wrap them as number <bytes>. The number constructor already takes raw bytes (number.eo:15), so no floating-point arithmetic is needed to build the result and there is no recursion back into as-number. Values with magnitude up to 2^53 are exact.

This is the most involved of the related conversions (#5467, #5468, #5469, #5470): magnitudes at or above 2^53 must be rounded to nearest, ties to even, by inspecting the guard and sticky bits below bit 52, and a round-up can carry into the exponent — the one genuinely subtle case. Suggested approach: add a pure-EO as-number that computes the biased exponent from the magnitude’s bit length, positions and rounds the mantissa with bytes shifts and masks, and ORs the three fields into the eight-byte double. Related: #5470, #5469, #5468, #5467, and the now-closed inverse #5442.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions