[SOLVED] BASH - algorithm to ensure any division is even
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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."
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.
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:
Obviously you got what you were searching for because your thread is marked as [SOLVED] now but:
do you want to allow division only if the result is even?
OR
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?
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).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.