# Use Fixed-Points
To compute decimals, there are two options. One is fixed-point and
the other is floating-point. Instead of explaining, I'll give you
examples.
Ex) $123.45
Fixed: 12345 cents
Float: $(1.2345 * 10^2)
Ex) $123.45 + $67.89
Fixed: 12345 cents + 6789 cents
Float: $(1.2345 * 10^2) + $(6.789 * 10^1)
Ex) 123.45 * 67.89
Fixed: (12345 * 6789 / 100) cents
Float: (1.2345 * 6.789) * 10^(2+1)
You might already understand why floating-point sucks. It's comple
x. Because of that, it's also slow and more error-prone. You have
to use float type instead of integer. So use fixed points whenever
you can.
Here's an example code. I rewrote 'FixedPointAdd()' function in Te
mpleOS for UNIX.
``````````````````````````````````````````````````````````````````
#include
#define SCALE 0x100000000
typedef long int I64;
typedef double F64;
typedef void U0;
U0 main()
{
// (32 bit) + (32 bit) = (Integer) + (Fraction)
I64 a = 1.5 * SCALE;
I64 b = 3.8 * SCALE;
I64 sum = a + b;
printf("Fixed point calculation: %.2f \n",
sum / (F64)SCALE );
}
``````````````````````````````````````````````````````````````````
``````````````````````````````````````````````````````````````````
; run add.c
Fixed point calculation: 5.30
``````````````````````````````````````````````````````````````````
'run' means 'tcc -run'.