Code::Blocks Forums

User forums => Help => Topic started by: jb2005 on October 22, 2005, 12:43:20 pm

Title: Code::Blocks don't recognize float and double types !!!!
Post by: jb2005 on October 22, 2005, 12:43:20 pm
Hi everybody,

I recently manage to give a try to Code::Blocks and overall it has been a great experience. However, it seems to have a little probllem:

- when typing " float i; i=6/7; printf("i=%f\n",i);" it compiles and return i=0 !! Same thing when declaring 'i' as a double......

I'm using a HP Pavilion Notebook with AMD 64 processo, and same results using several compilers (GNU, Visual Toolkit...)

Thanks for any help,

JB
Title: Re: Code::Blocks don't recognize float and double types !!!!
Post by: tiwag on October 22, 2005, 12:53:44 pm
the compiler is right - look in your C tutorial why !

use this
Code
#include <stdio.h>
#include <stdlib.h>

int main()
{
    float i;
    i=6.0/7;
    printf("i=%f\n",i);
    return 0;
}

i=0.857143
Title: Re: Code::Blocks don't recognize float and double types !!!!
Post by: thomas on October 22, 2005, 01:44:01 pm
That reminds me of this classic:

Quote from: http://www.possibility.com/Cpp/const.html
For example, C programmers learn early on that the result of dividing an integer by an integer is always an integer:
int x = 37;
int y = 8;

double quotient = x / y;   // classic mistake, result is rounded to an int
cout << quotient;          // prints " 4.000000"
double quotient = (double)x/y;   // cast result as double so it's not rounded
cout << quotient;          // prints "4.625000"
Title: Re: Code::Blocks don't recognize float and double types !!!!
Post by: tiwag on October 22, 2005, 02:17:36 pm
...
Thanks for any help,
JB

read this file
http://www.literateprogramming.com/ctraps.pdf
Title: Re: Code::Blocks don't recognize float and double types !!!!
Post by: jb2005 on October 22, 2005, 03:57:24 pm
Many thanks for all of your tips. I thought that if 'i' is declared as a float, i=6/7 would be implicitly sized as a float...

I guess it's because I'm new to this  :D
Title: Re: Code::Blocks don't recognize float and double types !!!!
Post by: thomas on October 22, 2005, 05:20:05 pm
It is, but if x and y are both integers, it calculates
i = (float) (6/7) = (float)(0) = 0.0f

EDIT:
Besides, there are many other possible pitfalls that you can encounter using float or double.
Make sure you read and understand exactly what is going on there, or you will almost certainly run into problems.

For example, you should never use operator== on floats or doubles unless you know what you are doing.
Also, you should never rely on floating point operations to be exact, predictable or reproducable (example (http://www.sqlite.org/faq.html#q18)).

All that a float guarantees is that you have 32 bits of precision when it is written to memory (and double has 64 bits). Strictly speaking, you cannot even rely on that... but let's not make things too complicated and assume the 'usual'. ;)

Internally, the CPU may use 32bits, 64bits, 80bits, 96bits, or whatever the hardware happens to have as register size (usually 80+ bits). Thus, a float value that is passed from one register to another is different from one that is obtained from memory, even if you perform precisely the same operations. So a == b is almost certain to cause trouble!