Skip to main content

C-language

0.1 is not 0.1. Yups!

Computers can't store 0.1 accurately. It is stored as 0.1000000015! In this post we explore and take a look at what is really going on.

Don't trust me? Dont have to. Compile and run the following C code

#include <stdio.h>

void main() {
  float value = 0.1;

  printf("value: %0.10f\n", value);
}

Assuming the code above is saved in main.c, you can compile and run the compiled code as shown below -

~/
❯ gcc main.c

~/
❯ ./a.out   
0.1000000015

Computers don't store 0.1 as 0.1? That is correct!

How is 0.1 represented?

Try another experiment. Compile and run the following -

#include <stdio.h>

void main() {
  float value = 0.1;

  printf("value: %0.10f\n", value);
  printf("In Hex: 0x%x\n", *(unsigned int *)&value);
}

I see the following output.

~/
❯ gcc main.c

~/
❯ ./a.out   
value: 0.1000000015
In Hex: 0x3dcccccd