{"id":396,"date":"2014-02-26T15:11:50","date_gmt":"2014-02-26T15:11:50","guid":{"rendered":"http:\/\/it.tuxie.eu\/?p=396"},"modified":"2014-03-04T13:02:22","modified_gmt":"2014-03-04T13:02:22","slug":"basic-calculations-in-bash","status":"publish","type":"post","link":"http:\/\/it.tuxie.eu\/?p=396","title":{"rendered":"Basic calculations in Bash"},"content":{"rendered":"<p>Discussion about limits in Excel (15 decimal digits maximum) with my friend caused inspiration for writing few words about basic number magic within Bash command line.<\/p>\n<p><!--more--><\/p>\n<p>As Pi precision is one of classic benchmark, let\u00b4s try it. You can find many information about Pi number (<a href=\"http:\/\/c2.com\/cgi\/wiki?ValueOfPi\">there <\/a>and <a href=\"http:\/\/numbers.computation.free.fr\/Constants\/Pi\/piclassic.html\">there<\/a>), anyway it\u00b4s always good to know something about <a href=\"http:\/\/linux.about.com\/od\/commands\/l\/blcmdl1_bc.htm\">bc<\/a> in *nix environment. Now we can write directly into command line something like this:<\/p>\n<p><span style=\"font-family: courier new,courier; font-size: 12px;\">ji@linux-bfq8:~&gt; <span style=\"color: #ff0000;\"><strong><code>time echo \"scale=1500; 4*a(1)\" | bc -l<\/code><\/strong><\/span><\/span><br \/>\n<span style=\"font-family: courier new,courier; font-size: 12px;\">3.141592653589793238462643383279502884197169399375105820974944592307\\<\/span><br \/>\n<span style=\"font-family: courier new,courier; font-size: 12px;\">81640628620899862803482534211706798214808651328230664709384460955058\\<\/span><br \/>\n<span style=\"font-family: courier new,courier; font-size: 12px;\">22317253594081284811174502841027019385211055596446229489549303819644\\<\/span><br \/>\n<span style=\"font-family: courier new,courier; font-size: 12px;\">28810975665933446128475648233786783165271201909145648566923460348610\\<\/span><br \/>\n<span style=\"font-family: courier new,courier; font-size: 12px;\">45432664821339360726024914127372458700660631558817488152092096282925\\<\/span><br \/>\n<span style=\"font-family: courier new,courier; font-size: 12px;\">40917153643678925903600113305305488204665213841469519415116094330572\\<\/span><br \/>\n<span style=\"font-family: courier new,courier; font-size: 12px;\">70365759591953092186117381932611793105118548074462379962749567351885\\<\/span><br \/>\n<span style=\"font-family: courier new,courier; font-size: 12px;\">75272489122793818301194912983367336244065664308602139494639522473719\\<\/span><br \/>\n<span style=\"font-family: courier new,courier; font-size: 12px;\">07021798609437027705392171762931767523846748184676694051320005681271\\<\/span><br \/>\n<span style=\"font-family: courier new,courier; font-size: 12px;\">45263560827785771342757789609173637178721468440901224953430146549585\\<\/span><br \/>\n<span style=\"font-family: courier new,courier; font-size: 12px;\">37105079227968925892354201995611212902196086403441815981362977477130\\<\/span><br \/>\n<span style=\"font-family: courier new,courier; font-size: 12px;\">99605187072113499999983729780499510597317328160963185950244594553469\\<\/span><br \/>\n<span style=\"font-family: courier new,courier; font-size: 12px;\">08302642522308253344685035261931188171010003137838752886587533208381\\<\/span><br \/>\n<span style=\"font-family: courier new,courier; font-size: 12px;\">42061717766914730359825349042875546873115956286388235378759375195778\\<\/span><br \/>\n<span style=\"font-family: courier new,courier; font-size: 12px;\">18577805321712268066130019278766111959092164201989380952572010654858\\<\/span><br \/>\n<span style=\"font-family: courier new,courier; font-size: 12px;\">63278865936153381827968230301952035301852968995773622599413891249721\\<\/span><br \/>\n<span style=\"font-family: courier new,courier; font-size: 12px;\">77528347913151557485724245415069595082953311686172785588907509838175\\<\/span><br \/>\n<span style=\"font-family: courier new,courier; font-size: 12px;\">46374649393192550604009277016711390098488240128583616035637076601047\\<\/span><br \/>\n<span style=\"font-family: courier new,courier; font-size: 12px;\">10181942955596198946767837449448255379774726847104047534646208046684\\<\/span><br \/>\n<span style=\"font-family: courier new,courier; font-size: 12px;\">25906949129331367702898915210475216205696602405803815019351125338243\\<\/span><br \/>\n<span style=\"font-family: courier new,courier; font-size: 12px;\">00355876402474964732639141992726042699227967823547816360093417216412\\<\/span><br \/>\n<span style=\"font-family: courier new,courier; font-size: 12px;\">19924586315030286182974555706749838505494588586926995690927210797509\\<\/span><br \/>\n<span style=\"font-family: courier new,courier; font-size: 12px;\">302952<\/span><\/p>\n<p><span style=\"font-family: courier new,courier; font-size: 12px;\">real\u00a0\u00a0 \u00a00m1.045s<\/span><br \/>\n<span style=\"font-family: courier new,courier; font-size: 12px;\">user\u00a0\u00a0 \u00a00m1.032s<\/span><br \/>\n<span style=\"font-family: courier new,courier; font-size: 12px;\">sys\u00a0\u00a0 \u00a00m0.000s<\/span><br \/>\n<span style=\"font-family: courier new,courier; font-size: 12px;\">ji@linux-bfq8:~&gt; <\/span><\/p>\n<p>Pi number with 1500 decimal places &#8211; not bad, right? \ud83d\ude42 <span style=\"color: #ff0000;\"><strong>-l<\/strong><\/span> at the end is hinting bc to use standard math library (as function arctan &#8211; a() is part of it). Ok, some basics first (without any comments, I think all will be very simple):<\/p>\n<p><span style=\"font-family: courier new,courier; font-size: 12px;\">ji@linux-bfq8:~&gt; <span style=\"color: #ff0000;\"><strong>echo 10\/3<\/strong><\/span><\/span><br \/>\n<span style=\"font-family: courier new,courier; font-size: 12px;\"> 10\/3<\/span><br \/>\n<span style=\"font-family: courier new,courier; font-size: 12px;\"> ji@linux-bfq8:~&gt; <span style=\"color: #ff0000;\"><strong>echo 10\/3 | bc -l<\/strong><\/span><\/span><br \/>\n<span style=\"font-family: courier new,courier; font-size: 12px;\"> 3.33333333333333333333<\/span><br \/>\n<span style=\"font-family: courier new,courier; font-size: 12px;\"> ji@linux-bfq8:~&gt; <span style=\"color: #ff0000;\"><strong><code>echo \"scale=5;10\/3\" | bc -l<\/code><\/strong><\/span><\/span><br \/>\n<span style=\"font-family: courier new,courier; font-size: 12px;\"> 3.33333<\/span><br \/>\n<span style=\"font-family: courier new,courier; font-size: 12px;\"> ji@linux-bfq8:~&gt; <span style=\"color: #ff0000;\"><strong><code>echo \"obase=16; 255\" | bc<\/code><\/strong><\/span><\/span><br \/>\n<span style=\"font-family: courier new,courier; font-size: 12px;\"> FF<\/span><br \/>\n<span style=\"font-family: courier new,courier; font-size: 12px;\"> ji@linux-bfq8:~&gt; <span style=\"color: #ff0000;\"><strong><code>echo \"obase=2; 255\" | bc<\/code><\/strong><\/span><\/span><br \/>\n<span style=\"font-family: courier new,courier; font-size: 12px;\"> 11111111<\/span><\/p>\n<p>With all documentation + scripting abilities + your imagination you can handle very complex math problems with Bash. If you want to get higher, try awk or\/and perl. Do you feel the infinity?&#8230;&#8230;<\/p>\n<p>-a-<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Discussion about limits in Excel (15 decimal digits maximum) with my friend caused inspiration for writing few words about basic number magic within Bash command line.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[4],"tags":[],"_links":{"self":[{"href":"http:\/\/it.tuxie.eu\/index.php?rest_route=\/wp\/v2\/posts\/396"}],"collection":[{"href":"http:\/\/it.tuxie.eu\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/it.tuxie.eu\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/it.tuxie.eu\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/it.tuxie.eu\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=396"}],"version-history":[{"count":0,"href":"http:\/\/it.tuxie.eu\/index.php?rest_route=\/wp\/v2\/posts\/396\/revisions"}],"wp:attachment":[{"href":"http:\/\/it.tuxie.eu\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=396"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/it.tuxie.eu\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=396"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/it.tuxie.eu\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=396"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}