LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   BASH - algorithm to ensure any division is even (https://www.linuxquestions.org/questions/programming-9/bash-algorithm-to-ensure-any-division-is-even-4175645170/)

GPGAgent 12-29-2018 10:45 AM

BASH - algorithm to ensure any division is even
 
I need a division to always return an even number

I've tried (dividend+divisor-1)/divisor and 1+(dividend-1)/divisor with no luck
Code:

onk@XEON4 ~/LK/PROCESS $ dividend=1400
onk@XEON4 ~/LK/PROCESS $ divisor=3
onk@XEON4 ~/LK/PROCESS $ echo $dividend $divisor $[(dividend+divisor-1)/divisor] $[1+(dividend-1)/divisor]
1400 3 467 467

It must be easy enough, isn't it? is there a bash function?

pan64 12-29-2018 10:54 AM

$[ ] is deprecated in bash. Instead you can try $(( arithmetic expression )). See man bash about it.
try:
Code:

v=$(( dividend/2/divisor ))
echo $(( v*2 ))

if I understand it well

Turbocapitalist 12-29-2018 10:56 AM

Have you tried modulus?

Code:

r=3;
echo $(($r-$r%2));


GPGAgent 12-29-2018 11:13 AM

Quote:

Originally Posted by Turbocapitalist (Post 5942527)
Have you tried modulus?

Code:

r=3;
echo $(($r-$r%2));


Nope:
Code:

onk@XEON4 ~/LK/PROCESS $ r=1400
onk@XEON4 ~/LK/PROCESS $ echo $(($r-$r%3));
1398


GPGAgent 12-29-2018 11:15 AM

Quote:

Originally Posted by pan64 (Post 5942526)
$[ ] is deprecated in bash. Instead you can try $(( arithmetic expression )). See man bash about it.
try:
Code:

v=$(( dividend/2/divisor ))
echo $(( v*2 ))

if I understand it well

Looks good to me and so obvious
Code:

onk@XEON4 ~/LK/PROCESS $ echo $dividend $divisor echo $(( dividend/2/divisor*2 )) $(( dividend/divisor ))
1401 3 echo 466 467


michaelk 12-29-2018 11:16 AM

Quote:

It must be easy enough, isn't it?
No. Even or odd only applies to integers and in your example the quotient is not an integer i.e 1400/3=466.67... I'm not much of a mathematician so according to wikipedia

"when the quotient is an integer, it will be even if and only if the dividend has more factors of two than the divisor."

https://en.wikipedia.org/wiki/Parity_(mathematics)

NevemTeve 12-29-2018 11:18 AM

Maybe this (with integer aritmethic): (p+q)/(2q)*2

Turbocapitalist 12-29-2018 11:20 AM

modulus would go like this:

Code:

dividend=1400;
divisor=7;
r=$((dividend/divisor));
echo  $dividend $divisor $(($r-$r%2));

The other solutions might be better.

pan64 12-29-2018 11:22 AM

Quote:

Originally Posted by GPGAgent (Post 5942531)
Nope:
Code:

onk@XEON4 ~/LK/PROCESS $ r=1400
onk@XEON4 ~/LK/PROCESS $ echo $(($r-$r%3));
1398


I think you missed:
$r is the result $(( dividend/divisor )) and you can use $(( r-r%2 )) to make it even. This 2 is 2 in any case, not related to divisor or anything else.

ehartman 12-29-2018 11:29 AM

Quote:

Originally Posted by michaelk (Post 5942533)
No. Even or odd only applies to integers and in your example the quotient is not an integer i.e 1400/3=466.67...

You forget one thing: arithmetic expressions in bash are always calculated as integers
Code:

Evaluation is done in fixed-width integers with no check for overflow, though
division by 0 is trapped and flagged as an error.

(from the bash man page under ARITHMETIC EVALUATION).

So in bash $(( 1400 / 3 )) will return 466 without any fractional part.
I tested it to be sure and indeed, 466 was the answer.

GPGAgent 12-29-2018 11:40 AM

Quote:

Originally Posted by pan64 (Post 5942536)
I think you missed:
$r is the result $(( dividend/divisor )) and you can use $(( r-r%2 )) to make it even. This 2 is 2 in any case, not related to divisor or anything else.

Sorry, misread your reply, and yes that works just fine:

Code:

onk@XEON4 ~/LK/PROCESS $ echo $dividend $divisor $((dividend/divisor - dividend/divisor%2))
1401 3 466

10 out of 10

GPGAgent 12-29-2018 11:42 AM

Thanks to the others as well, modulus is good but I'm going with divide by 2 then multiply by 2 - the most obvious and good enough for my usage.

Happy New year everyone

GPGAgent 12-29-2018 12:00 PM

Quote:

Originally Posted by michaelk (Post 5942533)
No. Even or odd only applies to integers and in your example the quotient is not an integer i.e 1400/3=466.67... I'm not much of a mathematician....

True enough, but bash only works in integer values ;-)
Code:

onk@XEON4 ~/LK/PROCESS $ echo $dividend $divisor $((dividend/divisor))
1400 3 466

Thanks tho', and happy new year

l0f4r0 12-29-2018 06:25 PM

Obviously you got what you were searching for because your thread is marked as [SOLVED] now but:
  1. do you want to allow division only if the result is even?
    OR
  2. do you want to process all divisions and get an even result?
I'm asking this question because I can't really see a useful application for case #2 (considering the fact that your result can sometimes be mathematically wrong)...
Out of curiosity, could you share how all of this could be useful to you? :)

ehartman 12-29-2018 08:50 PM

Quote:

Originally Posted by l0f4r0 (Post 5942649)
I'm asking this question because I can't really see a useful application for case #2 (considering the fact that your result can sometimes be mathematically wrong)...

As bash arithmetic is integer only, just the result from 1400 / 3 already is mathematically wrong, although you can get the remainder with the % operator.
The result of above division will be 466, with remainder 2 (which both happen to be even).


All times are GMT -5. The time now is 10:53 PM.