Float compile-time calculation not happening?

Posted by Klaim on Stack Overflow See other posts from Stack Overflow or by Klaim
Published on 2010-06-06T23:17:14Z Indexed on 2010/06/06 23:22 UTC
Read the original article Hit count: 382

Filed under:
|

A little test program:

#include <iostream>


const float TEST_FLOAT = 1/60;

const float TEST_A = 1;
const float TEST_B = 60;
const float TEST_C = TEST_A / TEST_B;

int main()
{
 std::cout << TEST_FLOAT << std::endl;
 std::cout << TEST_C << std::endl;

 std::cin.ignore();
 return 0;
}

Result :

0
0.0166667

Tested on Visual Studio 2008 & 2010.

  1. I worked on other compilers that, if I remember well, made the first result like the second result. Now my memory could be wrong, but shouldn't TEST_FLOAT have the same value than TEST_C? If not, why?
  2. Is TEST_C value resolved at compile time or at runtime? I always assumed the former but now that I see those results I have some doubts...

© Stack Overflow or respective owner

Related posts about c++

Related posts about compiler-optimization