Programming, Music, Games. Creation.

Posts tagged “explained

How does the computer save Real Numbers?

How does the computer save real numbers?

Lets look at only 2 bytes. (in reality, the computer uses more than 4 bytes to save real numbers.)


The computer uses the following equation to save real numbers

Why use this equation?

setting ‘m’ as numbers before ‘.’ and setting ‘e’ as numbers after ‘.’ is inefficient and takes a lot of memory to save real numbers.
However, using the equation, the computer is able to save real numbers efficiently, no matter how complicated they are.

Still, the computer cannot save wide range of real numbers and cannot save them accurately. It simply saves the data closest possible to the value we want to save. This is called float-point error.

Float-point error

#include <studio.h>

int main(void)
{
int i;
float f=0.0;

for(i=0; i<100; i++)
f+=0.1

printf(“%f “, f);

return 0;
}

the following is a good example of float error.

Variable f is declared,
until ‘i’ becomes 100, 0.1 is added to ‘f’
since ‘i++’ is used, ‘f’ is added by 0.1 100 times

The output must be 10, however you will see slight difference in the value. For example,

10.00002