[gambit-list] API to parse floats in Scheme
hendrik at topoi.pooq.com
Wed May 12 18:05:00 EDT 2021
On Wed, May 12, 2021 at 10:43:16PM +0300, Lassi Kortela wrote:
> We are planning a simple SRFI to mae floats from integers (or from strings),
> since that's useful when parsing any kind of text syntax that has real
> numbers. The details are implementation-dependent and tricky to get right.
> Here's the spec we have so far: https://github.com/pre-srfi/make-float
> The API is:
> (inexact-real-from-strings int frac precision exp) -> real
> (inexact-real-from-integers int frac frac-length precision exp) -> real
> Example usage:
> (inexact-real-from-strings "-123" "45" 'e "6") ; => -123.45e6
> (inexact-real-from-integers -123 45 2 'e 6) ; => -123.45e6
> The 2 in the last example means two decimal places. It's needed to
> disambiguate between .6 .06 .006 .0006 etc.
> There are both math and Scheme wizards on this list. Could someone verify
> whether this is a sane design before we submit the SRFI draft? It's been
> ok'd by one mathematician already so I'm hopeful :)
> There's also an inverse API to serve as a backend for string formatting
> (strings-from-inexact-real real frac-length) -> int; frac; exp
> (integers-from-inexact-real real frac-length) -> int; frac; exp
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.
And there's the question of what to do with he various not-a-number
floating point values.
And what if you do not want an exponent in the resulting string?
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'.
More information about the Gambit-list