Edit:
I personally think that in terms of the usability of your program using the small epsilon value comparison is appropriate. If the user is testing the program and wants to pass 0.2 to the program they should be able to do that without having to think to themselves 'oh yea floating point numbers blah blah blah'
It's good to educate yourself because there's lots of documentation. I liked learning about the representation of the floating point recently, at large values the floating point representation can't do very accurate values. For example for values of 2^23 the smallest +/- value for the float is 1.0
Code:
#include <stdio.h>
#include <string.h>
union number {
// bitwise representation
struct float_field
{
unsigned int mantissa : 23;
unsigned int exponent : 8;
unsigned int sign : 1;
} field;
float float_rep;
int int_rep;
};
int main()
{
printf("%d\n", sizeof(number));
number n;
n.float_rep = 1.0f;
printf("%f : sign %x exp %x mantissa %x\n\n",
n.float_rep, n.field.sign, n.field.exponent, n.field.mantissa);
n.field.sign = 0;
n.field.mantissa = 1;
n.field.exponent = 0x7f+23;
// Excess-N notation on the exponent
// 0 = 7f, 127 = ff, -128 = 0
for(int i = 0; i < 10; i++)
{
printf("\t%f : sign %x exp %x mantissa %x\n",
n.float_rep, n.field.sign, n.field.exponent, n.field.mantissa);
n.field.mantissa++;
}
return 0;
}
Code:
4
1.000000 : sign 0 exp 7f mantissa 0
8388609.000000 : sign 0 exp 96 mantissa 1
8388610.000000 : sign 0 exp 96 mantissa 2
8388611.000000 : sign 0 exp 96 mantissa 3
8388612.000000 : sign 0 exp 96 mantissa 4
8388613.000000 : sign 0 exp 96 mantissa 5
8388614.000000 : sign 0 exp 96 mantissa 6
8388615.000000 : sign 0 exp 96 mantissa 7
8388616.000000 : sign 0 exp 96 mantissa 8
8388617.000000 : sign 0 exp 96 mantissa 9
8388618.000000 : sign 0 exp 96 mantissa a