Can AM–GM fail on a digital computer?

110 Views Asked by At

It is well-known that valid mathematical formulas can fail on digital computers, due to rounding error, catastrophic cancellation, or whatever. So, it seems that the celebrated AM–GM inequality can also fail on a digital computer. So, can anyone supply such a fairly minimal and real-world-ly plausible scenario?

1

There are 1 best solutions below

2
On BEST ANSWER
#include <math.h>
#include <stdio.h>
int main() {
    double x = 1.5;
    double y = 1.5000000000000002;
    printf("AM: %.16f\n", (x+y)/2);
    printf("GM: %.16f\n", sqrt(x*y));
    return 0;
}

Compile with gcc foo.c -lm, run with ./a.out; output is

AM: 1.5000000000000000
GM: 1.5000000000000002

This failure of AM/GM is due to rounding errors.

Here's the details of the analogous problem in decimal, where it's easier to follow: using floating-point representations, in decimal, with 3 digits of precision, rounding to nearest (and rounding to even for ties), we have \begin{align*} 580\times 581 &= 336980 \approx 337000 \\ \sqrt{337000} &= 580.517\dots \approx 581 \end{align*} but \begin{align*} 580+581 &= 1161 \approx 1160 \\ 1160/2 &= 580 \end{align*}