User forums > Help
Code::Blocks don't recognize float and double types !!!!
jb2005:
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
tiwag:
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;
}
--- End code ---
i=0.857143
thomas:
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"
--- End quote ---
tiwag:
--- Quote from: jb2005 on October 22, 2005, 12:43:20 pm ---...
Thanks for any help,
JB
--- End quote ---
read this file
http://www.literateprogramming.com/ctraps.pdf
jb2005:
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
Navigation
[0] Message Index
[#] Next page
Go to full version