LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-29-2018, 10:45 AM   #1
GPGAgent
Senior Member
 
Registered: Oct 2018
Location: Surrey UK
Distribution: Mint 20 xfce 64bit
Posts: 1,026
Blog Entries: 3

Rep: Reputation: 133Reputation: 133
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?
 
Old 12-29-2018, 10:54 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,930

Rep: Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320
$[ ] 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
 
1 members found this post helpful.
Old 12-29-2018, 10:56 AM   #3
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,328
Blog Entries: 3

Rep: Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726
Have you tried modulus?

Code:
r=3; 
echo $(($r-$r%2));
 
Old 12-29-2018, 11:13 AM   #4
GPGAgent
Senior Member
 
Registered: Oct 2018
Location: Surrey UK
Distribution: Mint 20 xfce 64bit
Posts: 1,026

Original Poster
Blog Entries: 3

Rep: Reputation: 133Reputation: 133
Quote:
Originally Posted by Turbocapitalist View Post
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
 
Old 12-29-2018, 11:15 AM   #5
GPGAgent
Senior Member
 
Registered: Oct 2018
Location: Surrey UK
Distribution: Mint 20 xfce 64bit
Posts: 1,026

Original Poster
Blog Entries: 3

Rep: Reputation: 133Reputation: 133
Quote:
Originally Posted by pan64 View Post
$[ ] 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
 
Old 12-29-2018, 11:16 AM   #6
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,748

Rep: Reputation: 5927Reputation: 5927Reputation: 5927Reputation: 5927Reputation: 5927Reputation: 5927Reputation: 5927Reputation: 5927Reputation: 5927Reputation: 5927Reputation: 5927
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)
 
Old 12-29-2018, 11:18 AM   #7
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,869
Blog Entries: 1

Rep: Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870
Maybe this (with integer aritmethic): (p+q)/(2q)*2
 
Old 12-29-2018, 11:20 AM   #8
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,328
Blog Entries: 3

Rep: Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726
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.
 
1 members found this post helpful.
Old 12-29-2018, 11:22 AM   #9
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,930

Rep: Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320
Quote:
Originally Posted by GPGAgent View Post
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.
 
Old 12-29-2018, 11:29 AM   #10
ehartman
Senior Member
 
Registered: Jul 2007
Location: Delft, The Netherlands
Distribution: Slackware
Posts: 1,674

Rep: Reputation: 888Reputation: 888Reputation: 888Reputation: 888Reputation: 888Reputation: 888Reputation: 888
Quote:
Originally Posted by michaelk View Post
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.
 
Old 12-29-2018, 11:40 AM   #11
GPGAgent
Senior Member
 
Registered: Oct 2018
Location: Surrey UK
Distribution: Mint 20 xfce 64bit
Posts: 1,026

Original Poster
Blog Entries: 3

Rep: Reputation: 133Reputation: 133
Quote:
Originally Posted by pan64 View Post
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
 
Old 12-29-2018, 11:42 AM   #12
GPGAgent
Senior Member
 
Registered: Oct 2018
Location: Surrey UK
Distribution: Mint 20 xfce 64bit
Posts: 1,026

Original Poster
Blog Entries: 3

Rep: Reputation: 133Reputation: 133
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
 
Old 12-29-2018, 12:00 PM   #13
GPGAgent
Senior Member
 
Registered: Oct 2018
Location: Surrey UK
Distribution: Mint 20 xfce 64bit
Posts: 1,026

Original Poster
Blog Entries: 3

Rep: Reputation: 133Reputation: 133
Quote:
Originally Posted by michaelk View Post
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

Last edited by astrogeek; 12-29-2018 at 09:58 PM. Reason: Fixed broken quote
 
Old 12-29-2018, 06:25 PM   #14
l0f4r0
Member
 
Registered: Jul 2018
Location: Paris
Distribution: Debian
Posts: 900

Rep: Reputation: 290Reputation: 290Reputation: 290
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?
 
Old 12-29-2018, 08:50 PM   #15
ehartman
Senior Member
 
Registered: Jul 2007
Location: Delft, The Netherlands
Distribution: Slackware
Posts: 1,674

Rep: Reputation: 888Reputation: 888Reputation: 888Reputation: 888Reputation: 888Reputation: 888Reputation: 888
Quote:
Originally Posted by l0f4r0 View Post
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).
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash simple math (division) question babag Programming 7 08-01-2018 06:31 AM
[SOLVED] Any distro with similar division of softwares like debian? rubankumars Linux - Desktop 5 07-15-2017 06:13 AM
how to ensure that my Linux password can't be cracked by any user or even superuser prasanta dutta Linux - Newbie 28 03-15-2014 09:49 PM
token bucket algorithm vs Leaky bucket algorithm xeon123 Linux - Networking 2 03-26-2007 04:57 AM
bash and math division problem bennethos Programming 5 10-17-2004 01:51 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:56 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration