bc
http://www.maclife.com/taxonomy/term/11702/all
enTerminal 101: Solving Math Equations
http://www.maclife.com/article/columns/terminal_101_solving_math_equations
<!--paging_filter--><p><em>Every Monday, we'll show you how to do something new and simple with Apple's built-in command line application. You don't need any fancy software, or a knowledge of coding to do any of these. All you need is a keyboard to type 'em out!</em></p><p>This week’s Terminal 101 tip will help you solve both simple and complex equations through the command line without having to launch a dedicated application. With the Terminal and <em>bc</em> (a command line-based basic calculator), you’ll be able to perform quick calculations in long string formats, and even set variables and refer to them throughout the equation.</p><h3>Opening <em>bc</em></h3><p><em><a class="thickbox" href="/files/u12635/bc_1.png"><img src="/files/u12635/bc_1.png" width="620" height="426" class="thickbox" /></a></em></p><p>To open the <em>bc</em> environment, you’ll need to open the Terminal and type in <em>bc</em> followed by the enter key. Once inside the environment, you’ll be able to type mathematical statements followed by the enter key to instantly see the answer. To exit <em>bc</em>, type quit, followed by the Enter key.</p><h3>Basic Calculations</h3><p><em>Bc</em> uses the same standard computer arithmetic conventions also used in programming languages to perform calculations. See the list below: <br /><br />+ Addition<br />- Subtraction<br />* Multiplication<br />/ Division <br />^ Exponent <br /><br />Just like in algebra, you can use parentheses "()" to separate the mathematical statement for order of evaluation. Otherwise, the statement will be evaluated with the typical mathematical precedence (multiplication and division first, followed by addition and subtraction).</p><p><a class="thickbox" href="/files/u12635/bc_2.png"><img src="/files/u12635/bc_2.png" width="620" height="426" class="thickbox" /></a><br />So, if you wanted to multiply 12 by 6, and divide the total by 12 minus 4, then you’d type the following statement into <em>bc</em>:</p><pre>((12 * 6) / 12) - 4</pre><h3>Setting and Using Variables</h3><p>As you can already see, <em>bc</em> is a powerful calculator that allows you to string multiple mathematical statements together and instantly see the results, but sometimes you may want to set a variable. A variable is a placeholder for a number that is used frequently in your calculations.</p><p><a class="thickbox" href="/files/u12635/bc_3.png"><img src="/files/u12635/bc_3.png" width="620" height="426" class="thickbox" /></a><br />Setting a variable is as easy as typing a name, followed by an equal sign (=), followed by the value to be stored in the variable, like this:</p><pre>m = 600</pre><pre>a = 20</pre><p>You can then perform a calculation and store it’s value in a variable, like this:</p><pre>f = m * a</pre><p>You can print out the contents of a variable to the screen by simply typing the variable name into <em>bc</em> and pressing enter. <br /><br />That’s not all <em>bc</em> can do! To find out all how to use all that <em>bc</em> has to offer, including loops, type "<em>man bc</em>" in the Terminal.<br /><br /><em>Cory Bohon is a freelance technology writer, indie Mac and iOS developer, and amateur photographer. Follow this article's author, <a href="http://twitter.com/coryb" target="_blank">Cory Bohon on Twitter</a>.</em></p>http://www.maclife.com/article/columns/terminal_101_solving_math_equations#commentsbccalculationscalculatorColumnscommand linemathTerminalTerminal 101MacMon, 04 Jun 2012 19:30:02 +0000Cory Bohon14215 at http://www.maclife.com