[gambit-list] API to parse floats in Scheme
lassi at lassi.io
Wed May 12 18:31:38 EDT 2021
> Conversion on a round trip between float and string form should converge
> rapidly. At the least,
> string to float to string to float
> should give the same value as just
> string to float
> and similarly for float to string to float to string.
The above is above my paygrade :) Are there established methods to do
it? R7RS gives the following references which may or may not do it:
Robert G. Burger and R. Kent Dybvig. Printing floating-point numbers
quickly and accurately. In Proceedings of the ACM SIGPLAN ’96 Conference
on Programming Language Design and Implementation, pages 108–116.
William Clinger. How to read floating point numbers accurately. In
Proceedings of the ACM SIGPLAN ’90 Conference on Programming Language
Design and Implementation, pages 92–101. Proceedings published as
SIGPLAN Notices 25(6), June 1990.
> And what if you do not want an exponent in the resulting string?
As in the difference between printf("%f", x) and printf("%e", x).
Indeed, both cases should be covered.
> And finally, there are ties you want it read and written *exactly*, for
> example, using hexadecimal instead of decimal so you get every bit.
> (for this you'd want a symbol for the exponent 'e' that's different from
> the digit 'e'.
Scheme may not have an established notation for this. For a low-level
SRFI, would float<->bytevector be enough? R6RS has the following:
(bytevector-ieee-single-native-ref bytevector k)
(bytevector-ieee-single-ref bytevector k endianness)
(bytevector-ieee-double-native-ref bytevector k)
(bytevector-ieee-double-ref bytevector k endianness)
(bytevector-ieee-single-native-set! bytevector k x)
(bytevector-ieee-single-set! bytevector k x endianness)
(bytevector-ieee-double-native-set! bytevector k x)
(bytevector-ieee-double-set! bytevector k x endianness)
More information about the Gambit-list