Round floor behavior

Hello,

My round and floor functions in C program behaves weird. Can someone help resolve the issue..

fprintf( fp, "ROUND TEST VARIABLE 11686776.000000 %d\n", round(11686776.000000));
   fprintf( fp, "ROUND TEST VARIABLE 1168677.000000 %d\n", round(1168677.000000));
   fprintf( fp, "FLOOR TEST VARIABLE 11686776.000000 %f\n", floor(11686776.000000));
   fprintf( fp, "FLOOR TEST VARIABLE 1168677.000000 %f\n", floor(1168677.000000));

Output:
When Floor vars are printed as %d:

ROUND TEST VARIABLE 11686776.000000 23373552
ROUND TEST VARIABLE 1168677.000000 2337354
FLOOR TEST VARIABLE 11686776.000000 65598
FLOOR TEST VARIABLE 1168677.000000 65598

When Floor vars are printed as %f:

ROUND TEST VARIABLE 11686776.000000 23373552
ROUND TEST VARIABLE 1168677.000000 2337354
FLOOR TEST VARIABLE 11686776.000000 0.000000
FLOOR TEST VARIABLE 1168677.000000 0.000000

How did you declare these functions in your source code?

What compiler are you using?

What compiler options did you use when you compiled your source code?

What diagnostics were produced when you compiled your source code?

What operating system are you using?

My guess would be that you didn't #include <math.h> or specify appropriate function prototypes for floor() and round() . Without including the proper header or supplying appropriate function prototypes, the compiler will assume that those functions return values of type int instead of values of type double .

1 Like

@Don Cragun..
You were right.!

the math.h was not visible which was causing conversion issues.

It works fine now.

Thanks.!

Undeclared functions are assumed to return integers, which would cause weird results here - it would take the double result as a 32-bit integer, and so take the lower 32-bits of the double, pass that into printf, and print that.