There is a reliable way to compute the exact number of floating-point "intervals" (one less than the number of FP numbers) between any two FP numbers. It is a long-ago solved problem. I have attached a C++ version. You can't define closeness by a "distance" in a FP system - you should use this measure instead (called "ulps" - units in the last place). The distance between large FP numbers may always be greater than the tolerance you prescribe. The spacing between adjacent FP numbers at the top of the scale for IEEE double precision numbers is 2^(972) (approx. 10^(293))! I doubt you're gping to make your tolerance this big. I don't believe newbies can grasp this, but they can be taught to get a "feel" for floating-point number systems. You can't write reliable FP code without this understanding. See http://uvsc.freshsources.com/decimals.pdf.
Sunday, February 12, 2006, 11:44:51 AM, you wrote:
> |
I've been thinking about a function that was recently proposed at python-dev named 'areclose'. It is a function that is meant to tell whether two (or possible more) numbers are close to each other. It is a function similar to one that exists in Numeric. One such implementation is
def areclose(x,y,abs_tol=1e-8,rel_tol=1e-5): diff = abs(x-y) return diff <= ans_tol or diff <= rel_tol*max(abs(x),abs(y))
(This is the form given by Scott Daniels on python-dev.)
Anyway, one of the rationales for including such a function was:
When teaching some programming to total newbies, a common frustration is how to explain why a==b is False when a and b are floats computed by different routes which ``should'' give the same results (if arithmetic had infinite precision). Decimals can help, but another approach I've found useful is embodied in Numeric.allclose(a,b) -- which returns True if all items of the arrays are ``close'' (equal to within certain absolute and relative tolerances) The problem with the above function, however, is that it *itself* has a comparison between floats and it will give undesired result for something like the following test:
### >>> print areclose(2, 2.1, .1, 0) #see if 2 and 2.1 are within 0.1 of each other False >>> ###
Here is an alternative that might be a nice companion to the repr() and round() functions: nice(). It is a combination of Tim Peter's delightful 'case closed' presentation in the thread, "Rounding to n significant digits?" [1] and the hidden magic of "prints" simplification of floating point numbers when being asked to show them.
It's default behavior is to return a number in the form that the number would have when being printed. An optional argument, however, allows the user to specify the number of digits to round the number to as counted from the most significant digit. (An alternative name, then, could be 'lround' but I think there is less baggage for the new user to think about if the name is something like nice()--a function that makes the floating point numbers "play nice." And I also think the name...sounds nice.)
Here it is in action:
### >>> 3*1.1==3.3 False >>> nice(3*1.1)==nice(3.3) True >>> x=3.21/0.65; print x 4.93846153846 >>> print nice(x,2) 4.9 >>> x=x*1e5; print nice(x,2) 490000.0 ###
Here's the function: ### def nice(x,leadingDigits=0): """Return x either as 'print' would show it (the default) or rounded to the specified digit as counted from the leftmost non-zero digit of the number,
e.g. nice(0.00326,2) --> 0.0033""" assert leadingDigits>=0 if leadingDigits==0: return float(str(x)) #just give it back like 'print' would give it leadingDigits=int(leadingDigits) return float('%.*e' % (leadingDigits,x)) #give it back as rounded by the %e format ###
Might something like this be useful? For new users, no arguments are needed other than x and floating points suddenly seem to behave in tests made using nice() values. It's also useful for those computing who want to show a physically meaningful value that has been rounded to the appropriate digit as counted from the most significant digit rather than from the decimal point.
Some time back I had worked on the significant digit problem and had several math calls to figure out what the exponent was. The beauty of Tim's solution is that you just use built in string formatting to do the work. Nice.
/c
[1] http://mail.python.org/pipermail/tutor/2004-July/030324.html |
--
Best regards,
Chuck
// ulps.h: Counts the FP intervals between two FP numbers #include <algorithm> #include <cassert> #include <cstdlib> #include <cmath> #include <limits>
template<typename FType> int sign(const FType& x) { return x < FType(0) ? -1 : x > FType(0) ? 1 : 0; } template<typename FType> int lowerexp(FType z, FType& glb) { int base = std::numeric_limits<FType>::radix; // Find largest exponent of base <= z ("glb" = "base"^"exp"). // z is bracketed by [glb, lub) // First, search above 1 FType lub = 1; int exp = 0; while (lub <= z) { lub *= base; ++exp; } glb = lub/base; --exp; // In case z < 1, search there while (glb > z) { lub = glb; glb /= base; --exp; } assert(glb <= z && z < lub); return exp; } template<typename FType> long double ulps(FType x, FType y) { // Handle special cases first long double inf = std::numeric_limits<long double>::infinity(); if (x == y) return 0; else if (sign(x)*sign(y) <= 0 || x == inf || y == inf) return inf; // Ignore sign for the result; make x <= y x = std::abs(x); y = std::abs(y); if (x > y) std::swap(x, y); // Get needed floating-point parameters // (The cast is to force long double arithmetic below) long double eps = static_cast<long double>(std::numeric_limits<FType>::epsilon()); int base = std::numeric_limits<FType>::radix; // Get lower exponent boundaries for x and y FType xglb, yglb; int xexp = lowerexp(x, xglb); int yexp = lowerexp(y, yglb); if (xexp == yexp) return (y - x) / (xglb * eps); // In same interval else return (yexp - xexp - x/xglb + y/yglb + base - 2) / eps; /* NOTE: To derive this computation, assume x's exponent is e and y's is f in the following: The intervals between x and its ceiling boundary (B^(e+1)) are (B^(e+1) - x) / (eps*B^e) (the denominator is the spacing there). Likewise for y, the formula is (y - B^f) / (eps*B^f). The intervals between is calculated by the formula (f - e - 1) / eps. All three factors have eps in the denominator. This yields: [B - x/B^e + y/B^f - 1 + f - e - 1] / eps which simplifies to [f - e - x/B^e + y/B^f + B - 2] / eps or in terms of the variables in the program: [yexp - xexp - x/xglb + y/yglb + base - 2] / eps If you assume the base is 2, then you get [yexp - xexp - x/xglb + y/yglb] / eps because two "-1"'s cancel with base (being 2). These simple formulas yields the total number of ulps between x and y except in the case where x and y lie in the same floating-point interval. */ }
#include <iostream> #include "ulps.h" using namespace std; int main() { // Test for float cout.precision(8); float feps = numeric_limits<float>::epsilon(); cout << ulps(.5f, 1.0f) << endl; cout << ulps(.5f, 1.0f+feps) << endl; cout << ulps(.5f+feps, 1.0f) << endl; cout << ulps(.5f, 2.0f) << endl; cout << ulps(.5f, 2.0f+feps*2) << endl; cout << ulps(.5f, 4.0f) << endl; cout << ulps(.5f, 4.0f+feps*4) << endl; cout << ulps(0.0f, 1.0f) << endl; cout << ulps(-1.0f, 1.0f) << endl; cout << endl; // Test for double cout.precision(17); double deps = numeric_limits<double>::epsilon(); cout << ulps(.5, 1.0) << endl; cout << ulps(.5, 1.0+deps) << endl; cout << ulps(.5+deps, 1.0) << endl; cout << ulps(.5, 2.0) << endl; cout << ulps(.5, 2.0+deps*2) << endl; cout << ulps(.5, 4.0) << endl; cout << ulps(.5, 4.0+deps*4) << endl; cout << ulps(0.0, 1.0) << endl; cout << ulps(-1.0, 1.0) << endl; cout << endl; } /* Output: 8388608 8388609 8388606 16777216 16777217 25165824 25165825 inf inf 4503599627370496 4503599627370497 4503599627370494 9007199254740992 9007199254740993 13510798882111488 13510798882111489 inf inf */
_______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor