Round floor behavior


 
Thread Tools Search this Thread
Top Forums Programming Round floor behavior
# 1  
Old 04-18-2018
Round floor behavior

Hello,

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

Code:
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:
Code:
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:
Code:
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

# 2  
Old 04-18-2018
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.
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 04-18-2018
@Don Cragun..
You were right.!

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

It works fine now.

Thanks.!
# 4  
Old 04-18-2018
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Round up the decimals

Hi All, I would like to do the following in the shell script 561.76 to 562 I tried using this echo 'scale=0; 749 * 75 /100 ' | bc but just returned only 561 Please help me . I appreciate your help Thanks rajeevm (13 Replies)
Discussion started by: rajeevm
13 Replies

2. Shell Programming and Scripting

CEILING and FLOOR functions

Hi all, Does anyone know how to simulate a ceiling or floor function in UNIX? OS is Solaris. I tried the suggestion from an old forum but it is giving me error as below: server01/tmp$: echo "7.2" | awk '{printf("%d\n",$0+=$0<0?0:0.999)}' awk: syntax error near line 1 awk: illegal... (3 Replies)
Discussion started by: newbie_01
3 Replies

3. Shell Programming and Scripting

How to round up on fives in unix?

i'm a newbie here, i need help with a shell script. for a given number, if it is greater than ten round to the nearest 10 same for 100, if it is greater than 100 round to the nearest 100, and same for 1000. i'm confused how to start this... its supposed to look like this input ... (11 Replies)
Discussion started by: CRAZYLITTLELOU
11 Replies

4. Shell Programming and Scripting

Round off the a Decimal value.

HI, I have a script which is used to calculate the Memory & CPU utilization a server. memx=`ssh -l siebel1 ${f} /usr/sbin/prtconf|grep -i 'Memory size'|tr -s " "|/usr/xpg4/bin/awk -F" " '{print $3 * 1024}'` v5=`ssh -l siebel1 ${f} vmstat 1 2 | tail -1 | tr -s " " | /usr/xpg4/bin/awk -v... (3 Replies)
Discussion started by: dear_abhi2007
3 Replies

5. Shell Programming and Scripting

Round with awk

Hi, I have a problem. Basically I dont know how to use awk. I have a script (below) which works fine. What I want to do is somehow "pipe" in the input say 4.5 and have it give the anwer, I dont want ot have to type it in, since it will be running in a script. Any ideas how to do this???? ... (1 Reply)
Discussion started by: AnnaLynn
1 Replies

6. Shell Programming and Scripting

Round the column value :

Hi .... Iam having the file ....in which 3rd column is numerical having 8 decimal part... i want that to cut to 2 decimal part ... Source File : E100|0|19940.10104030|0|1ABC E103|1|19942.10195849|3|0ABC E100|0|19943.10284858|0|1ABC I want to be ...... Reulst: ... (4 Replies)
Discussion started by: satyam_sat
4 Replies

7. Shell Programming and Scripting

FLOOR Func

Hello Experts, Is there any inbuild FLOOR function to do FLOOR func in mathmetics in awk script like in FlOOR Func in C. Ex:- floor(2.9) = 2 floor(2.1) = 2 floor(2.0) = 2 floor(-2.0) = 2 floor(-2.1) = -3 floor(-2.9) =... (1 Reply)
Discussion started by: user_prady
1 Replies

8. Shell Programming and Scripting

round a number

In a shell script - How do I round a decimal number (contained in a variable) to the nearest whole number? (2 Replies)
Discussion started by: achieve
2 Replies

9. Shell Programming and Scripting

round in KSH

Is there an easy way to round a number up in Korn shell? ie. 10.4 --> 11 Thanks. (6 Replies)
Discussion started by: here2learn
6 Replies

10. UNIX for Dummies Questions & Answers

how to round a value

Hello, In a unix shell script,i want to round a variabele to a nearest number Ex: set count=104.4 How can i round that to 105.? Thanks, Sateesh (2 Replies)
Discussion started by: kotasateesh
2 Replies
Login or Register to Ask a Question