# Thread: Declare float

1. ## Declare float

Hello,

I declared a float array in my program as follows:

Code:
`float example_array[] = {0.2, 0.1};`
when I look at the debugger while running my program; it says that the array holds 0.20000003....

Due to this my program does not work. Can anyone help me here?

Thanks

2. Floats are not exact. There are certain numbers for which no perfect binary equivalent exists. But as you can see the FPU is fairly precise unless you are trying to go the the moon or something.

Also try adding f to the end of your floats.

3. Ever looked at the moon and just thought about stuff. Like space stuff.

It makes me sad for some reason.

EDIT: I just googled "we live in a piece of software" and there was practically nothing. I mean it's a prefectly possible theory. Because I'm weird and have a large amount of free time (well now anyways) I've been thinking about how practically nothing in what we know to be the Universe actually proves that we aren't living in a piece of software.

The real question is who wrote it? The old cliché of a super- intelligent alien race is a bit "meh", I would say it's a more technologically advanced us wanting to simulate the world as it was. Why? How the hell should I know it's just a theory and it's more interesting than aliens.

4. What's the actual problem, are you testing for equality? Maybe try seeing if the values are close enough instead:

Code:
```#include <math.h>

int close(float a, float b)
{
if(fabsf(a - b) < .01)
return 1;
else
return 0;
}```

5. Originally Posted by joed
What's the actual problem, are you testing for equality? Maybe try seeing if the values are close enough instead:

Code:
```#include <math.h>

int close(float a, float b)
{
if(fabsf(a - b) < .01)
return 1;
else
return 0;
}```
Note that <limits.h> defines constants you may find useful for this purpose, namely "FLT_EPSILON" and "DBL_EPSILON". These are the smallest number 'x' such that 1.0 + x != 1.0 for floats and doubles respectively.

6. Originally Posted by joed
What's the actual problem, are you testing for equality? Maybe try seeing if the values are close enough instead:

Code:
```#include <math.h>

int close(float a, float b)
{
if(fabsf(a - b) < .01)
return 1;
else
return 0;
}```
I do not want to compare values but perform calculations with them...

Ex: 0.1 - 0.1 = 0 but 0.1 - 0.100009 <> 0 or does c treats them the same? I tried this:

Code:
`float example_array[] = {0.2F, 0.1F};`
but it doesn't seem to work. I am a bit confused for the moment with all these data types...

7. Ex: 0.1 - 0.1 = 0 but 0.1 - 0.100009 != 0
That's right. If you want 110% kill-yer-mutha EXACT values you might consider writing your own base-10 number object

Or you can do some rounding (maybe 2~3 digits) after calculations so the results seem "right".

8. http://en.wikipedia.org/wiki/Floating_point
Until you understand what "approximate" means, floating point usage will always confuse you, and frustrate your attempts to write programs.

9. You may consider using fraction numbers instead of floatingpoint.

Code:
```typedef struct Fract {
int     numerator;
int     denominator;
} Fract;

#define FRACTS   2
Fract    fract[ FRACTS ] = {
{ 2, 10 },
{ 1, 10 }
};```
Or a class Fract, etc. There shall be plenty of examples and code on the net to Add, Sub, Mul and Div fractions, etc. It's an alternative to floating point arithmetics and it is exact. For example 1/3 can be represented exactly { 1, 3 } while a float or double will truncate it to something relatively close to 1/3 but not exactly 1/3, etc.
You can also use a combination of fractions and floats! Use fractions during computings and convert the result to a float at the very end. That way you may get better precision in the computations. The drawback will be that it naturally takes longer time to compute fraction numbers if there is no direct hardware support, like an FPU helps computing floating point arithmetics.

10. Good idea buddy.

11. Thanks for the reply's, helped a lot! I'm sure I can make my program work now.

Popular pages Recent additions