You are not logged in.

## #1 2012-09-11 20:28:38

zacariaz
Member
Registered: 2012-01-18
Posts: 492
Website

### [Solved] Division by zero. What to do?

This is all about one variable (\$progress) which, sadly, will rarely equal zero, resulting in an annoying "division by zero!" error.

``expectedCompletion="\$( echo 3k \$elapsedTime 10000 \* \$progress / \$totalTime / 0k 0.5 + 1 / f | dc )"``

The only "fix" I've been able to think about so far, is to add one to \$progress when it's equal to zero.

However, as I am a bash noob, I am wondering if there's a better solution. If for example I could catch the error, not output an error massage, but instead return a value of my choice, that's be cool.

There may also be a mathematical solution to the problem, but I certainly don't see it.

In any case, thanks in advance.

Last edited by zacariaz (2012-09-11 21:08:24)

Offline

## #2 2012-09-11 20:39:08

progandy
Member
Registered: 2012-05-17
Posts: 2,470

### Re: [Solved] Division by zero. What to do?

Don't do the calculation if progress is zero?

``expectedCompletion="\$( [ \$progress -eq 0 ] && echo "progress is 0" || echo 3k \$elapsedTime 10000 \* \$progress / \$totalTime / 0k 0.5 + 1 / f | dc )"``

Online

## #3 2012-09-11 20:57:33

zacariaz
Member
Registered: 2012-01-18
Posts: 492
Website

### Re: [Solved] Division by zero. What to do?

progandy wrote:

Don't do the calculation if progress is zero?

``expectedCompletion="\$( [ \$progress -eq 0 ] && echo "progress is 0" || echo 3k \$elapsedTime 10000 \* \$progress / \$totalTime / 0k 0.5 + 1 / f | dc )"``

But how to achieve that? Won't it still be evaluated, even if surrounded by an if statement or similar?

Offline

## #4 2012-09-11 21:05:16

progandy
Member
Registered: 2012-05-17
Posts: 2,470

### Re: [Solved] Division by zero. What to do?

What I added is something like an if statement, and works as expected. It relies on some implementation details like that the expression is grouped and evaluated from left to right and that the execution is aborted as soon as the endresult can be determined.
Edit: It also relies on echo never failing. Otherwise you could end up with some unexpected results like "progress 0, echo fail, so calculate result"

``````var=\$( [ cond -eq 0 ] && echo a || echo b )
-> set var to the stdout of the shell command:
test if cond equals 0
&&: If true (exitcode 0)
echo a
||: test and echo both with exitcode 0, so the or is already true, do nothing
&&: False (exitcode not 0), so skip echo a
||: we arent't successful yet, so
echo b``````

If you want to use a more straight forward if construct, it would be something like this:

``````if [ \$progress -eq 0]; then
expectedCompletion="progress is 0"
else
expectedCompletion="\$( ... YOURCOMMAND ...)"
fi``````

Last edited by progandy (2012-09-11 21:16:29)

Online

## #5 2012-09-11 21:07:39

zacariaz
Member
Registered: 2012-01-18
Posts: 492
Website

### Re: [Solved] Division by zero. What to do?

progandy wrote:

What I added is something like an if statement, and works as expected. It relies on some implementation details like that the expression is grouped and evaluated from left to right and that the execution is aborted as soon as the endresult can be determined.
If you want to use a more straight forward if construct, it would be something like this:

``````if [ \$progress -eq 0]; then
expectedCompletion="progress is 0"
else
expectedCompletion="\$( ... YOURCOMMAND ...)"
fi``````

Mkay, I basically didn't think it worke that way, though of course it should.

Bash is very confusing.

Thanks a bunch.

Offline

## #6 2012-09-28 05:15:37

synthead
Member
From: Seattle
Registered: 2006-05-09
Posts: 1,328

### Re: [Solved] Division by zero. What to do?

Could also do:

``expectedCompletion="\$( ((progress == 0)) && echo 0 || YOURCOMMAND )"``

Touch my kernel

Offline

## Board footer

Powered by FluxBB