Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How come π and π/10 appear to have the same relative error when represented as binary64?

Suppose that you quickly want to determine which of π or π/10 has the largest relative error when represented in the IEEE 754 binary64 format. Also, you only have a C compiler at hand.

You might write the C program below, or a more compact version of it:

#include <stdio.h>
#include <math.h>

volatile long double pil = 3.14159265358979323846L;
volatile double pi = 3.14159265358979323846;

volatile long double tpil = 0.314159265358979323846L;
volatile double tpi = 0.314159265358979323846;

int main() {

  volatile long double abs = pil - pi;

  printf("%La\n%La\n%La\n", pil, (long double)pi, abs);

  printf("pi:   abs err %La -> rel %La\n", abs, abs / pil);

  volatile long double abst = tpil - tpi;
  printf("pi/10: abs err %La -> rel %La\n", abst, abst / tpil);

}

The funny thing is that this program shows the relative errors of π and π/10 to be identical:

0xc.90fdaa22168c235p-2
0xc.90fdaa22168cp-2
0x8.d4p-56
pi:   abs err 0x8.d4p-56 -> rel 0xb.3d85789395e215bp-58
pi/10: abs err 0xe.2p-60 -> rel 0xb.3d85789395e215bp-58

I have added the volatile qualifiers and the intermediate computations to look at the generated assembly and make sure that this wasn't a compiler bug. It apparently isn't.

This is strange because many other values v do not have the property that the relative error of v is the same as the relative error of v/10. I checked with 0.1 and 0.3. Also this is not a question of magnitude, because 3 and 3.5 obviously have different relative errors than their respective tenths. Although 5, 10, 15, … do have the same relative error as their respective tenths, but these should be considered the exceptions.

Now, the program does not compute the exact values of the relative errors. It only has 12 bits to represent them (one sign bit plus 11 bits of difference between a 64-bit significand and a 53-bit significand). So there may have been a priori something like one chance in 4096 for the absolute error of π to turn out as 10 times the absolute error of π/10.

This still seems like an unlikely coincidence. Was the a priori chance that a real constant taken between 3 and 3.5 had the same relative error than its tenth when represented as double larger than my intuition says it should be? Or is there another way to see it, such as “this happens as soon as (double)(π/10)'s significand has enough trailing zeroes for its multiplication by ten to be exact”, which seems much more frequent (close to 1/8)?

like image 679
Pascal Cuoq Avatar asked Nov 09 '14 00:11

Pascal Cuoq


People also ask

What are the absolute and relative errors of approximation of Pi?

An approximation for pi is 3.14159 , so 3.14 is within .0016 of pi. multiplying by it would introduce an error of a factor of .0016 / 3.14 . 3,14 IS PI if that is all the precision you want. there are no “absolutes and relative errors of approximation of pi.

What is pi (π)?

Pi (π) Draw a circle with a diameter (all the way across the circle) of 1. Then the circumference (all the way around the circle) is 3.14159265... a number known as Pi. Pi is often written using the greek symbol π. The definition of π is:

Is the number π normal?

The digits of π have no apparent pattern and have passed tests for statistical randomness, including tests for normality; a number of infinite length is called normal when all possible sequences of digits (of any given length) appear equally often. The conjecture that π is normal has not been proven or disproven.

What is π as a ratio of two integers?

π is an irrational number, meaning that it cannot be written as the ratio of two integers. Fractions such as 22 113 are commonly used to approximate π, but no common fraction (ratio of whole numbers) can be its exact value.


1 Answers

A better way to look at this question is to notice that the significand of the double nearest π is a multiple of 5:

#include <stdio.h>
#include <math.h>
#include <stdint.h>
#include <inttypes.h>
#include <string.h>

volatile long double pil = 3.14159265358979323846L;
volatile double pi = 3.14159265358979323846;

volatile long double tpil = 0.314159265358979323846L;
volatile double tpi = 0.314159265358979323846;

void print_significand(double d) {
  uint64_t significand;

  memcpy(&significand, &d, 8);
  significand &= ((uint64_t)1<<52) - 1;
  significand |= (uint64_t)1<<52;

  printf("%" PRIx64 " %" PRIu64 "\n",
     significand, significand);
}

int main() {

  printf("Significand of (double)pi: ");
  print_significand(pi);

  printf("Significand of (double)(pi/10): ");
  print_significand(tpi);
  …

This shows:

Significand of (double)pi: 1921fb54442d18 7074237752028440
Significand of (double)(pi/10): 141b2f769cf0e0 5659390201622752

The integer 5659390201622752 is exactly 7074237752028440 / 5 * 4. The multiplication by four serves to keep the double representing π/10 normalized.

So given an arbitrary real r chosen uniformly between 2 and 4, approximated by the nearest double d, there is one chance in five that the significand of d is a multiple of 5. When this happens, d/10.0 is exact. This makes d/10.0 a very good candidate for the title of “double nearest to r/10”.

How good a candidate?

If the division by 10 takes us from the top of the binade (just below 4) to the bottom of the binade (just above 0.25), then the representable doubles are comparatively less dense around r/10 than they are around r, and there is no chance of finding a closer double to r/10 than d/10.0.

If the division by 10 takes us from the bottom of the binade (just above 2) to the top of the binade (just below 0.25), then doubles are comparatively denser around r/10 than they are around r. There is a chance that a better double approximation for r/10 than d/10.0 exists, especially if d is already a bad approximation for r.

The cut-off point is 2.5. The real π is above this cut-off point, so knowing that the significand of its double approximation was divisible by 5 was enough to infer that π/10 would be approximated by the tenth of the approximation of π, and that the relative errors of both approximations would be the same.

like image 169
Pascal Cuoq Avatar answered Oct 25 '22 15:10

Pascal Cuoq