[Edu-sig] Long floats?
Tim Peters
tim.one@home.com
Fri, 12 Jan 2001 04:18:48 -0500
[Kirby Urner]
> Here's something from a math teacher who hangs out on math-teach
> (where I've been cross-posting a few of my "math through
> programming" essays).
>
> He wonders if there's a way to get "long floats". I don't
> think there is, except maybe there's an add-on module I don't
> know about. Any clues?
The only language I know of with native support for adjustable (by the user)
precision floats is REXX. There are BigFloat libraries available at least
for Perl and Ruby, and IIRC also Java. Some specialized numeric
environments (e.g., Macsyma) also support them.
As far as I know, nobody ever bothered to write one for Python. Python has
something far more strange and wonderful, though:
ftp://ftp.python.org/pub/python/contrib-09-Dec-1999/
DataStructures/real-accurate.pyar
(I broke that into two lines, else my mailer would have broken it at an
uglier place; paste them together without the leading blanks on the second
line)
That's an unusual implementation of the constructive reals.
Arbitrary-precision floats are buried in there, but hard to get at as such.
I'll just quote the start of the readme file:
Real.README - documentation for real.py. Goes with version 1.11.
Written by Jurjen N.E. Bos (jurjen@q2c.nl)
Introduction
============
real.py is a library that introduces a new class, called Real, of
abitrarily precise numbers, allowing computations with "infinite"
precision.
Each number keeps an approximation of the accuracy of the number.
Routines are available to compute numbers with more and more digits.
real.py allows you to:
- compute with very high precision, e.g. 1000 digits
- compute with very large or small numbers, up to about 10**500000000
- compute without unexpected precision loss: all digits guaranteed
- compute with infinite precision, printing digits on the fly
- printing floating point numbers to their exact accuracy
Just to whet your appetite:
>>> from real import pa,exp,pi,sqrt
>>> pa('exp(pi()*sqrt(163))')
262537412640768743.99999999999925007259719818568887935385633733699086270
753741037821064791011860731295118134618606450419308388794975386404490572
871447719681485232243203911647829148864228272013117831706501045222687801
444841770346969463355707681723887681000923706539519386506362757657888558
223948114276912100830886651107284710623465811298183012459132836100064982
...
and so on.
it's-especially-fun-if-you-have-infinite-memory<wink>-ly y'rs - tim