Hi all,

I'm working with VC++6.0 in WinNT4 SP4 on a Pentium II 300. With following code I get strange results when casting the result of a floating point operation to a long.

This is my code:

--------------------------------

#include <stdio.h>

#include <math.h>

void func1()

{

float x1, x2, x3;

long res;

x1 = 23.8f;

x2 = 100.0f;

res = (long)(x1 * x2);

printf( "Result : %i.\n", res );

x3 = x1 * x2;

res = (long)x3;

printf( "Result : %i.\n", res );

}

void func2()

{

float x1, x2, x3;

long res;

x1 = 23.8f;

x2 = 100.0f;

res = (long)(x1 * x2);

x3 = x1 * x2; // same function as func1 except these

printf( "Result : %i.\n", res ); // two lines are in different order.

res = (long)x3;

printf( "Result : %i.\n", res );

}

int main(int argc, char* argv[])

{

printf("Output of func1 : \n");

func1();

printf("Output of func2 : \n");

func2();

return 0;

}

--------------------------------

This is the output:

--------------------------------

Output of func1 :

Result : 2379.

Result : 2379.

Output of func2 :

Result : 2379.

Result : 2380.

--------------------------------

My Questions:

Why does 'res=(long)(x1*x2);' differ from 'x3=x1*x2,res=(long)x3' in func2() and why does func1() produce a different output than func2() ???

any help appreciated,

ZOPPO

>> from 'x3=x1*x2,res=(long)x3'

To the best f my knowledge there should be no difference. (Assuming you have the parenthesis correct, which you do in the code, but not in you comment).

>> why does func1() produce a different

>> output than func2()

It shouldn't. I don't have time now, but I will try this later and see if I get the same results.