Essential Mathematical skills?

Started by
22 comments, last by serratemplar 17 years, 4 months ago
Quote:Original post by Fred304Lots of rules that apply to real numbers do not apply to floats


True, and let's not forget the fabuloustic property of some chipsets:

float a = GetSomeValue();
float b = a;
assert (a == b);
printf("%s", "Oh, look, a side-effect");
assert (a != b);

Quote:
Avoid floats whenever precise results are necessary.


Unless you prove the result precision to be within a tolerance range.
Advertisement
Quote:Original post by ToohrVyk
True, and let's not forget the fabuloustic property of some chipsets:

float a = GetSomeValue();
float b = a;
assert (a == b);
printf("%s", "Oh, look, a side-effect");
assert (a != b);


?!

What happens there, exactly?
Quote:Original post by Zahlman
Quote:Original post by ToohrVyk
True, and let's not forget the fabuloustic property of some chipsets:

float a = GetSomeValue();
float b = a;
assert (a == b);
printf("%s", "Oh, look, a side-effect");
assert (a != b);


?!

What happens there, exactly?


The printf call flushes one or more registers, thus reducing the precision of either a or b, which makes the condition evaluate to false the second time. Not very robust or reproductible, I agree.

A more robust example that I did not remember right away, as presented in the original paper (pages 8-9):

do_nothing(double*) { }if (z != 0) {  do_nothing(&z);  assert (z != 0);}


The code forces a flush of z, reducing its precision to 64 bits (from a full 80-bit internal representation) and making the assert fail.
Here's the most "classical" example of floating point math breaking the rules.

10^60 - 10^60 + 1 = 1

You agree - I hope =) - and most compilers will agree too, because they'll group the first two together as identical symbols, zero them about due to the subtraction, and give us one. However...

10^60 + 1 - 10^60 = 0 according to machine.

Because 10^60 + 1 = 10^60 (1 just gets discarded as it's beyond the scope of the float's precision) and 10^60 - 10^60 = 0. To some degree, addition is not associative in floating point math. This isn't consistent though, because if you substitute a number "big enough" for 1, the math will start working as you expect it to.

I didn't learn this all in a math course though; I learned it in architecture and assembly class =)

This topic is closed to new replies.

Advertisement