65 points · by jwmerrill · 11y ago · 26 comments
The library CGAL has many clever algorithms with floating point numbers. Its strength lies in the mixture of numerical approximation and exact computation it does to solve problems.
It may interest you that William Kahan has written quite extensively on the pitfalls of iterative algorithms on floating point numbers; his discussion of Muller's pathological recurrence on pp. 14+ of this paper seems particularly relevant.
julia> bisect_root((x) -> rand() - 1/2, 1.0, 2.0)
(1.1980511149266218,1.198051114926622)
julia> bisect_root((x) -> rand() - 1/2, 1.0, 2.0)
(1.595600613503659,1.5956006135036591)Well, to take an example from some of my past work as a structural engineer, say you wanted to find out how much wind load a structure could sustain until it buckled under its own weight. So you create a finite element model of the structure that applies the wind load and then ramps up the gravity until the structure fails. The input is the magnitude of the wind pressure. The output is the total gravity at failure (in units of g). subtract 1 from the output, and you have a function where the root is the level of wind load you're looking for.
You're point about it always terminating when you use the bisection method is well taken, however. I was using a hybrid that included some Newton's method iterations to reduce the total number of steps, since each "function evaluation" took a lot of computer time.
Here's some more fun you can have:
int main() { float a = 0x7FFFFFFF; printf("%f %f %f\n", a, a - 64, a - 65); return 0; }
Here's a program:
int main(int argc, char* argv[]) {
double x = 0.75;
double t1 = 0.50000025;
int i, j;
int n8 = 93 * 100;
for (i = 0;; i++) {
for (j = 0; j<n8; j++)
x = sqrt(exp(log(x) / t1));
printf("%d %f\n", i, x);
}
}
Watch what happens to the performance as i passes 4632.The full saga is here; AFAICT it's a pathological edge case in glibc. https://plus.google.com/118274112180368285367/posts/ZPL5LTK9...
The fact that scientists had so much input on the early design of hardware and programming languages means that floats are often a performance hack that exchanges semantics for speed. But that's hardly necessary in most applications.
And if hardware and language support was better for fixed point numbers, floats would be seen as the bizarre special case that's only appropriate in rare cases.
But fixed point numbers have many similar issues, require more thought to find a reasonable significand, and are useful over a far narrower range of values.