Help on Modulus


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Help on Modulus
# 1  
Old 05-22-2011
Help on Modulus

Hello All,
I am trying to do a simple calculation using modulus (%) as shown below. But some how it is not showing me correct result. It says "0.166667" is equal to "0" which is wrong. Could you please help me how can i make it work. Thanks a lot.

Code:
#!/bin/ksh
attempt_count=10
SLEEP=60
poll_time=`nawk 'BEGIN { print ('${attempt_count}'*'${SLEEP}')/3600 }'`

echo "poll time" $poll_time
echo "mod time" $((poll_time%5))

if [ "$((poll_time%5))" = "0" ] ; then
        echo correct
else
        echo wrong
fi

Wrong Result:
Code:
poll time 0.166667
mod time 0
correct

Expected output as per Oracle sql statement:

Code:
select mod(0.166667,5) from dual

output:
  0.166667


Last edited by pludi; 05-22-2011 at 02:51 PM..
# 2  
Old 05-22-2011
Simple reason: ksh (and bash, not sure about zsh) doesn't do floating point arithmetics. For any calculation everything after the comma is ignored. Use bc instead.
# 3  
Old 05-22-2011
i tried using bc as below but i am unsuccessful, it still returning me the integer 0, can you please help.

Code:
option 1 as-- x=`echo $(('$poll_time'%5))|bc`
option 2 as-- x=`echo $((poll_time%5))|bc`

if [ "$x" = "0" ] ; then
        echo correct
else
        echo wrong
fi


Last edited by pludi; 05-23-2011 at 08:58 AM..
# 4  
Old 05-23-2011
First set the floating point precision using the bc command's "scale"
Code:
echo "scale=1; 100/200" | bc

# Ans: 0.5
# 5  
Old 05-23-2011
Quote:
Originally Posted by pludi
Simple reason: ksh (and bash, not sure about zsh) doesn't do floating point arithmetics.
ksh93 and zsh do support floating point arithmetic. Very very old versions of ksh, i.e. ksh88 and pdksh, a clone of ksh88, do not support floating point.
# 6  
Old 05-23-2011
Quote:
Originally Posted by royalibrahim
First set the floating point precision using the bc command's "scale"
Code:
echo "scale=1; 100/200" | bc

# Ans: 0.5
It is not working when i am doing modulus operation.
Quote:
echo "scale=1; $((0.166667%5))" | bc
Ans:0
expected output:
Quote:
select mod(0.166667,5) from dual
0.166667
# 7  
Old 05-23-2011
Code:
$ echo 'scale=1; 0.166667%5' | bc
.166667

This User Gave Thanks to vgersh99 For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

3 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk Division and modulus

I need to read the file divide 3 column with 2nd and run a modulus of 10 and check whether the remainder is zero or not if not print the entire line. cat filename | awk '{ if ($3 / $2 % 10 != 0) print $0}' Whats wrong with it ? (4 Replies)
Discussion started by: dinjo_jo
4 Replies

2. Shell Programming and Scripting

Modulus operator

What is the modulus operator in korn shell?? (5 Replies)
Discussion started by: manash.paul
5 Replies

3. UNIX for Dummies Questions & Answers

Help in modulus operator in Bash

Hi, I would like to know given that i have two columns and I would like to take the positive integer of the differences between the two columns. which means |3-2|=1; |2-3|=1 as well. I would like to know do Bash recognize | | as well for this purposes? Thanks. -Jason (2 Replies)
Discussion started by: ahjiefreak
2 Replies
Login or Register to Ask a Question