Link to home
Start Free TrialLog in
Avatar of YZlat
YZlatFlag for United States of America

asked on

Handling float or double in calculations

I am having some issues with floating-point variables in ksh scripting.

I am doing some calculations - adding values within the loop. I know the total adds up to one number bu I see a different (smaller) number printed to a screen. So I started echoing all the numbers being added and realized that ksh script rounds of floating point. If a number is greater then 1 but less than 2, for example (1.2), it uses 1. If number is 0.9, it uses 0, etc.

here is an example:

my code:

echo $incr_num
total=$((total + $incr_num))

echo "total so far "$total

output:

1.02
total so far is 1

0.83
total so far is 1

3.03
total so far is 4

0.90
total so far is 4

22.13
total so far is 26

So I get 26, instead of 27.91


I also tried using bc

total=$(echo "total + $incr_num" | bc)


but that gives me an error "syntax error on line 1 stdin"

Can someone help me fix this?
Avatar of gheist
gheist
Flag of Belgium image

it is not really float... just shift ledt, do integercalculations and shift back right.... no loss of INTEGER precision.


PSi'm afraid shell does not process float, you need perl or python or anything that knows floats.
ASKER CERTIFIED SOLUTION
Avatar of Tintin
Tintin

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Where is  $incr_num coming from?
Avatar of YZlat

ASKER

Thank you! Worked like a charm