DEV Community

Discussion on: What are your command line tips?

Collapse
abhinav1217 profile image
Abhinav Kulshreshtha

One of my recently acquired command line tip, create this function

? () { echo "$*" | bc ; }
Enter fullscreen mode Exit fullscreen mode

usage

$> ? 8*5
40

$> ? 2+2+9*9+(2-8)
79
Enter fullscreen mode Exit fullscreen mode

No need to create a python process or node process ( or any other interactive shell ) for quick calculation. It just takes the mathematical query and throw it to BC which then return the result.

Collapse
abhinav1217 profile image
Abhinav Kulshreshtha • Edited on

In case it isn't obvious, Question mark ? is just a name of function. You can choose anything else as long as it doesn't conflict the system.

Also bc is a mathematical tool, it follows bodmas or pemdas order of operation. So 6 ÷ 2(1+2)= is equal to 1, not 9, not 7, or any other thing you saw on facebook. I should have checked on my machine before bashing out here, My casio calculator outputs 1 because of implied multiplication taking precedence, so does what we are taught in schools in India, but apparently precedence of implied multiplication is standardized to be ignored since 1917.

Collapse
lucassperez profile image
Lucas Perez • Edited on

Sorry, but 6 ÷ 2(1+2) is just 9, indisputably.

Bodmas, pemdas or any other weird word is just a pitfall to trick people into mistakes, and gladly I have never heard such terms in school. Division and multiplication are performed in the order they appear, it could not be simpler. Division, in fact, is multiplication, just like subtraction is the same as addition. When you divide by 5, you are in fact multiplying by the inverse of 5. There is no reason to perform multiplication before division, since they are the same thing.

I couldn't believe BC would output 1, so I opened a terminal and ran it, and of course it showed 9 in the screen.

Thread Thread
abhinav1217 profile image
Abhinav Kulshreshtha

Well apparently, In India, we were taught the older method. I did some search and find this video from "mind-your-decision" and what we were taught is the older 1917 order of operation, where parentheses next to number has special exception. My old casio calculator actually outputs "1", because they have implied multiplication

I know that division and multiplication have same level of order and hence calculated left to right. Bodmas doesn't mean addition before subtraction, but it does mean division(or multiplication) happens before subtraction(or addition) and exponents(or log) happens before them, and brackets always have higher priority. The operators with same specificity are solved left-to-right.

Thanks for correcting me. I wrote it as a sarcasm on all the stupid fb posts and twitter polls that have been going around my feeds, but turns out it was a facepalm moment. "Implied multiplication" is still taught in schools in India.