comparison between float numbers


 
Thread Tools Search this Thread
Top Forums Programming comparison between float numbers
# 1  
Old 03-31-2009
comparison between float numbers

Hi, i have a simple control like this:
if(sum>1.0)...
If i try to print sum i get 1.000000 but the check returns true. I think it depends on float precision. How can i modify the check?
thanks
# 2  
Old 03-31-2009
7.12.14 Comparison macros - in C standards require that there be macros to handle float comparisons correctly. I think you want isgreater(). Review your math man page for details.

Also when you print a float, depending on the format specification, the [f]printf routine rounds numbers. Example 1.999999 becomes 2
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Comparison of floating point numbers in bash

I have the following code snippet in bash if ]; then minm=`echo "$diff" | bc` fi It works well for most of the cases. However lets say diff is -0.17 and minm is -0.0017. In such a case the comparison seems to fail. Is the correct way to compare a mixture of positive and... (12 Replies)
Discussion started by: ngabrani
12 Replies

2. Shell Programming and Scripting

Help about using variables of float numbers in sed

Hi, I need to run a Fortran program which reads a input file with a fixed name many times, each time I need to change a number (real) in that input file, this is how I currently do it and I know it is not elegent at all: cp inputfile.dat backup.dat sed -i 's/28.0/0.01/g' inputfile.dat ./myCode... (3 Replies)
Discussion started by: dypang
3 Replies

3. Shell Programming and Scripting

Random float numbers in BASH

Hi people :) I'm learning shell scripting using bash and I want to generate 4 floating point number with 5 decimal places and write them to a file and a variable. I've done all this except the $RAMDOM enviroment variable does not generate a float number but a integrer. I hope you could... (3 Replies)
Discussion started by: pharaoh
3 Replies

4. Shell Programming and Scripting

numbers comparison in fields of a file and print least value of them

Hi , I'm trying to compare fields in the file, I want compare the numbers in each column and get the least value of it. > cat input_file 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 -0.2050 -0.6629 -0.6407 -0.6599 -0.4085 -0.3959 -0.2526 -0.3597 0.3439 0.2275 0.2780 ... (5 Replies)
Discussion started by: novice_man
5 Replies

5. Solaris

can array store float point numbers

Hi all, I have doubt can array in a shell script can store floating point numbers. i have tired. but i unable to work it out. Please help me regarding this Thank U Naree (1 Reply)
Discussion started by: naree
1 Replies

6. Solaris

i cannot assign float point numbers to an array in solaris

total=0 declare -a sum limit=`iostat -En | grep -i size | awk '{print $2}' | sed -e 's/GB//g' | wc -l` echo "Limit is equal to $limit" ara="`iostat -En | grep -i size | awk '{print $2}' | sed -e 's/GB//g'`" for (( i=1; i<=$limit; i++ )) do sum=`echo $ara | cut -d " " -f $i` echo ${sum}... (11 Replies)
Discussion started by: naree
11 Replies

7. UNIX for Advanced & Expert Users

perl: string comparison as numbers

$name = 'Mark'; $goodguy = 'Tody'; if ($name == $goodguy) { print "Hello, Sir.\n"; } else { print "Begone, evil peon!\n"; } The output is Hello, Sir...... Even if the Mark is made to MarkSpencer.....then also it shows as Hello, Sir.... Can any one explain the... (7 Replies)
Discussion started by: bishweshwar
7 Replies

8. Shell Programming and Scripting

float comparison

I had a variable 1:80 8 in one varialbe say var=1:80 8 i comapred the below if then -- else ---- thought if condition is true its always going in the else condition....its not giving any compilation errror also....is there any problem bcoz of space... can some one help... (3 Replies)
Discussion started by: mkan
3 Replies

9. Shell Programming and Scripting

adding float numbers

how to add 2 float values to each other? i tried this and it doesnt work: #!bin/bash numone=1.234 numtwo=0.124 total=`expr $numone + $numtwo` echo $total thanks (5 Replies)
Discussion started by: strike
5 Replies
Login or Register to Ask a Question