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?
LVL 35
YZlatAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gheistCommented:
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.
0
TintinCommented:
total=$(echo "total + $incr_num" | bc)

Open in new window


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

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ozoCommented:
Where is  $incr_num coming from?
0
YZlatAuthor Commented:
Thank you! Worked like a charm
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.