by Peter McGoron
Submitted as SRFI 270
For editor's use only. Please do not edit this section.
??? the draft/final/withdrawn status of the SRFI, information on how to subscribe to its mailing list, and important dates in its history. The editor will add this section.
Floating-point numbers are generally in radix 2, but are written by users in radix 10. This SRFI introduces C99's hexadecimal floating point constants into Scheme syntax.
Computers generally use binary floating point to store numbers, while humans like decimals. To express binary floating point exactly, one can use hexadecimal floating point constants, which are written in base-16 instead of base-10. They represent binary floating point numbers precisely (as long as they can fit in the number).
Non-normative text is in small text.
The formal syntax of Scheme is modified to have the following new productions:
. ⟨digit 16⟩+. ⟨digit 16⟩*p ⟨sign⟩? ⟨digit 10⟩+
The exponential separator is p because it is ambiguous
if e would be a decimal separator or a hex digit.
If a system has multiple precisions of floats, then the syntax could
be extended such that ⟨exponent marker⟩ follows p.
A hexadecimal floating point number is interpreted such that
xnxn−1…x0.x−1x−2…x−mpd
where each x is a hexadecimal digit and d is a base-10 number, is equal to
(Xn×16n + Xn−1×16n−1 + … + X0 + X−1×16−1 + X−2×16−2 + … + X−m×16−m)×2d
where Xi is the numerical value of the digit xi.
The procedure number->string must understand hexadecimal
floats.
(read-hexadecimal-float [port])Read a hexadecimal float from the current input stream. A hexadecimal
may start with #x. The number is exact if it has #e
in it, an inexact otherwise.
(write-hexadecimal-float x [port])Write a floating point number in canonical format. This means:
This section is non-normative.
| Expression | Number |
|---|---|
#x9p9 |
9×29 = 4608 |
#x1.2p3 |
(1 + 2×16−1)×23 = 9 |
#xFE.FF1p1 |
(15×161 + 14 + 15×16−1 + 15×16−2)×21 = 1044465/2048 ≈ 509.992… |
#x-0.Ap-2 |
(10×16−1)×2−2 = −5/32 = −.15625 |
MIT-Scheme natively supports this syntax. A parser for numbers that incoporates this is in TODO.
Taylor Campbell suggested it to me on #scheme.
© 2026 Peter McGoron.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice (including the next paragraph) shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.