Author Topic: Code::Blocks don't recognize float and double types !!!!  (Read 19525 times)

jb2005

  • Guest
Code::Blocks don't recognize float and double types !!!!
« 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

Offline tiwag

  • Developer
  • Lives here!
  • *****
  • Posts: 1196
  • sailing away ...
    • tiwag.cb
Re: Code::Blocks don't recognize float and double types !!!!
« Reply #1 on: October 22, 2005, 12:53:44 pm »
the compiler is right - look in your C tutorial why !

use this
Code: [Select]
#include <stdio.h>
#include <stdlib.h>

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

i=0.857143

Offline thomas

  • Administrator
  • Lives here!
  • *****
  • Posts: 3979
Re: Code::Blocks don't recognize float and double types !!!!
« Reply #2 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"
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."

Offline tiwag

  • Developer
  • Lives here!
  • *****
  • Posts: 1196
  • sailing away ...
    • tiwag.cb

jb2005

  • Guest
Re: Code::Blocks don't recognize float and double types !!!!
« Reply #4 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

Offline thomas

  • Administrator
  • Lives here!
  • *****
  • Posts: 3979
Re: Code::Blocks don't recognize float and double types !!!!
« Reply #5 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).

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!
« Last Edit: October 22, 2005, 05:36:40 pm by thomas »
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."