floating point in 2.0
Chris Barker
chrishbarker at home.net
Fri Jun 8 14:59:59 EDT 2001
You know, after reading all this (I do find it interesting), I wonder
why I have never seen a language include an easy way to get/compare a
floating point number in a given precision. One of the first things we
all learn is that you should rarely use:
if fp1 == fp2
sometimes it is easy to use
if fp1 <= fp2,
but other times what you really want is:
if fp1 == fp2 (in the first 5 decimal digits.)
it's fairly easy to do:
if fp1 < fps+eps and fp1 > fps-eps
But then you need to know the order of magnitude of the numbers, leaving
you with:
if fp1 < 1.000001*fp2 and fp1 > 0.999999*fp2
which I suppose would work, but you are now doing two multiplies and a
compare, and I think it's ugly.
Since this is a very common need, I wonder why it isn't build into
languages, or even better, built into hardware. What I would like to see
are a RoundToNPlaces function, and maybe a CompareToNPlaces function
(with better names)
Does anyone have any nifty algorithms that they use for this that they
would like to share? I'd at least like to put them into my own toolbox.
-Chris
--
Christopher Barker,
Ph.D.
ChrisHBarker at home.net --- --- ---
http://members.home.net/barkerlohmann ---@@ -----@@ -----@@
------@@@ ------@@@ ------@@@
Oil Spill Modeling ------ @ ------ @ ------ @
Water Resources Engineering ------- --------- --------
Coastal and Fluvial Hydrodynamics --------------------------------------
------------------------------------------------------------------------
More information about the Python-list
mailing list