{"id":1595,"date":"2017-03-22T13:10:14","date_gmt":"2017-03-22T07:40:14","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=1595"},"modified":"2017-03-29T10:13:42","modified_gmt":"2017-03-29T04:43:42","slug":"integer-float-calculations-bash-languagesframeworks","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/integer-float-calculations-bash-languagesframeworks\/","title":{"rendered":"UNIX- How to do integer &#038; float calculations, in bash or other languages\/frameworks"},"content":{"rendered":"<p><label class=\"label label-warning\">PROBLEM:<\/label><\/p>\n<ul>\n<li>Using echo &#8220;20+5&#8221; literally produces the text &#8220;20+5&#8221;.<\/li>\n<li>What command you use to get the numeric sum, 25 in this case?<\/li>\n<li>Also, what&#8217;s the easiest way to do it just using bash for floating point? For example, echo $((3224\/3807.0)) prints 0 :(.<\/li>\n<li>You are looking for answers using either the basic command shell (&#8216;command line&#8217;) itself or through using languages that are available from the command line.<\/li>\n<\/ul>\n<p><label class=\"label label-info\">SOLUTION 1:<\/label><\/p>\n<p>You could use bc. E.g.,<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Bash Code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">$ echo &quot;25 + 5&quot; | bc<br\/>30<\/code><\/pre> <\/div>\n<p>Alternatively bc &lt;&lt;&lt; 25+5 will also work.<\/p>\n<h4 id=\"or-interactively-if-you-want-to-do-more-than-just-a-single-simple-calculation\"><span style=\"color: #000000;\"><strong>Or interactively, if you want to do more than just a single simple calculation:<\/strong><\/span><\/h4>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Bash Code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">$ bc<br\/>bc 1.06.95<br\/>Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.<br\/>This is free software with ABSOLUTELY NO WARRANTY.<br\/>For details type `warranty&#039;. <br\/>25 + 5<br\/>30<\/code><\/pre> <\/div>\n<p>bc -q will jump into bc right away without the header\/copyright info. For more information see the bc man page<\/p>\n<h4 id=\"this-page-also-shows-how-to-set-up-an-alias-or-function-to-do-calculation-like-this\"><span style=\"color: #000000;\"><strong>This page also shows how to set up an alias or function to do calculation like this:<\/strong><\/span><\/h4>\n<ul>\n<li>\u00a0get the result<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Bash Code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">c &quot;25 + 5&quot; <\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<p><label class=\"label label-info\">SOLUTION 2:<\/label><\/p>\n<h4 id=\"you-can-use-calc\"><span style=\"color: #0000ff;\"><strong>You can use calc:<\/strong><\/span><\/h4>\n<p>If you just enter calc with no other arguments it enters an interactive mode where you can just keep doing math.<\/p>\n<h4 id=\"you-exit-this-by-typing-exit\"><span style=\"color: #000000;\"><strong>You exit this by typing exit:<\/strong><\/span><\/h4>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Bash Code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">C-style arbitrary precision calculator (version 2.12.3.3)<br\/>Calc is open software. For license details type:  help copyright<br\/>[Type &quot;exit&quot; to exit, or &quot;help&quot; for help.]<br\/>; 2+4<br\/>6<br\/>; 3+5<br\/>8<br\/>; 3.4+5<br\/>8.4<br\/>; 2^4<br\/>16<br\/>; exit<\/code><\/pre> <\/div>\n<p>Or you use it with the expression as an argument and it will provide the answer and then exit<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Bash Code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">$calc 2 + 4<br\/>    6<br\/>$<\/code><\/pre> <\/div>\n<p>calc is similar to bc<\/p>\n<p><label class=\"label label-info\">SOLUTION 3:<\/label><\/p>\n<p>There are many ways to calculate.<\/p>\n<h4 id=\"for-simple-expressions-you-can-use-bash-itself\"><span style=\"color: #000000;\"><strong>For simple expressions you can use bash itself:<\/strong><\/span><\/h4>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Bash Code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">echo $((20+5))<\/code><\/pre> <\/div>\n<h4 id=\"or-expr\"><span style=\"color: #000000;\"><strong>or expr:<\/strong><\/span><\/h4>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">BAsh Code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">expr 20 + 5<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<h4 id=\"and-for-complex-cases-there-is-great-tool-bc\"><span style=\"color: #000000;\"><strong>And for complex cases there is great tool bc:<\/strong><\/span><\/h4>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Bash Code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">    echo &quot;20+5&quot; | bc<\/code><\/pre> <\/div>\n<p>bc can calculate even very complex expression with roots, logarithms, cos, sin and so on.<\/p>\n<p><label class=\"label label-info\">SOLUTION 4:<\/label><\/p>\n<h4 id=\"integer-math\"><span style=\"color: #000000;\"><strong>Integer Math:<\/strong><\/span><\/h4>\n<h4 id=\"performing-math-calculation-in-bash\"><span style=\"color: #000000;\"><strong>Performing Math Calculation in Bash:<\/strong><\/span><\/h4>\n<p>First way to do math with integer (and only integer) is to use the command \u201cexpr \u2014 evaluate expression\u201d.<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Bash Code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">Mac-n-Cheese:~ nicolas$ expr 1 + 1<br\/>2<br\/>Mac-n-Cheese:~ nicolas$ myvar=$(expr 1 + 1)<br\/>Mac-n-Cheese:~ nicolas$ echo $myvar<br\/>2<br\/>Mac-n-Cheese:~ nicolas$ expr $myvar + 1<br\/>3<br\/>Mac-n-Cheese:~ nicolas$ expr $myvar \/ 3<br\/>1<br\/>Mac-n-Cheese:~ nicolas$ expr $myvar \\* 3<br\/>9<\/code><\/pre> <\/div>\n<p>When doing a \u201cmultiply by\u201d make sure to backslash the \u201casterisk&#8221; as it\u2019s a wildcard in Bash used for expansion.<\/p>\n<p>Another alternative to expr, is to use the bash builtin command let.<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Bash Code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">Mac-n-Cheese:~ nicolas$ echo $myvar<br\/>6<br\/>Mac-n-Cheese:~ nicolas$ let myvar+=1<br\/>Mac-n-Cheese:~ nicolas$ echo $myvar<br\/>7<br\/>Mac-n-Cheese:~ nicolas$ let myvar+1<br\/>Mac-n-Cheese:~ nicolas$ echo $myvar<br\/>7<br\/>Mac-n-Cheese:~ nicolas$ let myvar2=myvar+1<br\/>Mac-n-Cheese:~ nicolas$ echo $myvar2<br\/>8<\/code><\/pre> <\/div>\n<h4 id=\"also-you-can-simply-use-the-parentheses-or-square-brackets\"><span style=\"color: #000000;\"><strong>Also, you can simply use the parentheses or square brackets :<\/strong><\/span><\/h4>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Bash Code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">Mac-n-Cheese:~ nicolas$ echo $myvar<br\/>3<br\/>Mac-n-Cheese:~ nicolas$ echo $((myvar+2))<br\/>5<br\/>Mac-n-Cheese:~ nicolas$ echo $[myvar+2]<br\/>5<br\/>Mac-n-Cheese:~ nicolas$ myvar=$((myvar+3))<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<h4 id=\"this-allow-you-to-use-c-style-programming\"><span style=\"color: #000000;\"><strong>This allow you to use C-style programming :<\/strong><\/span><\/h4>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Bash Code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">Mac-n-Cheese:~ nicolas$ echo $myvar<br\/>3<br\/>Mac-n-Cheese:~ nicolas$ echo $((myvar++))<br\/>3<br\/>Mac-n-Cheese:~ nicolas$ echo $myvar<br\/>4<br\/>Mac-n-Cheese:~ nicolas$ echo $((++myvar))<br\/>5<br\/>Mac-n-Cheese:~ nicolas$ echo $myvar<br\/>5<\/code><\/pre> <\/div>\n<p><label class=\"label label-info\">SOLUTION 5:<\/label><\/p>\n<h4 id=\"floating-point-arithmetic\"><span style=\"color: #000000;\"><strong>Floating point arithmetic:<\/strong><\/span><\/h4>\n<p>You can\u2019t do floating point arithmetic natively in bash, you will have to use a command line tool, the most common one being \u201cbc &#8211; An arbitrary precision calculator language\u201d.<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Bash Code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">Mac-n-Cheese:~ nicolas$ bc<br\/>bc 1.06<br\/>Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.<br\/>This is free software with ABSOLUTELY NO WARRANTY.<br\/>For details type `warranty&#039;.<br\/>3*5.2+7\/8<br\/>15.6<br\/>15.6+299.33*2.3\/7.4<br\/>108.6<\/code><\/pre> <\/div>\n<p>you can use the STDIN to send your formula to \u201cbc\u201d then get the output on STDOUT.<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Bash Code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">Mac-n-Cheese:~ nicolas$ echo &quot;3.4+7\/8-(5.94*3.14)&quot; | bc<br\/>-15.25<\/code><\/pre> <\/div>\n<h4 id=\"using-the-here-doc-notation\"><span style=\"color: #000000;\"><strong> using the here-doc notation:<\/strong><\/span><\/h4>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Bash Code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">Mac-n-Cheese:~ nicolas$ bc &lt;&lt;&lt; &quot;3.4+7\/8-(5.94*3.14)&quot;<br\/>-15.25<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<p>The \u201cscale\u201d variable is really important for the precision of your results, especially when using integers only (Note: you can also use \u201cbc -l\u201d to use mathlib and see the result at max scale) .<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Bash Code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\"><br\/>Mac-n-Cheese:~ nicolas$ echo &quot;2\/3&quot; | bc<br\/>0<br\/>Mac-n-Cheese:~ nicolas$ echo &quot;scale=2; 2\/3&quot; | bc<br\/>.66<br\/>Mac-n-Cheese:~ nicolas$ echo &quot;(2\/3)+(7\/8)&quot; | bc<br\/>0<br\/>Mac-n-Cheese:~ nicolas$ echo &quot;scale=2;(2\/3)+(7\/8)&quot; | bc<br\/>1.53<br\/>Mac-n-Cheese:~ nicolas$ echo &quot;scale=4;(2\/3)+(7\/8)&quot; | bc<br\/>1.5416<br\/>Mac-n-Cheese:~ nicolas$ echo &quot;scale=6;(2\/3)+(7\/8)&quot; | bc<br\/>1.541666<br\/>Mac-n-Cheese:~ nicolas$ echo &quot;(2\/3)+(7\/8)&quot; | bc -l<br\/>1.54166666666666666666<\/code><\/pre> <\/div>\n<h4 id=\"another-way-to-do-floating-point-arithmetic-is-to-use-awk\"><span style=\"color: #000000;\"><strong>Another way to do floating point arithmetic is to use AWK:<\/strong><\/span><\/h4>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Bash Code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">Mac-n-Cheese:~ nicolas$ awk &quot;BEGIN {print 100\/3}&quot;<br\/>33.3333<\/code><\/pre> <\/div>\n<h4 id=\"you-can-use-printf-to-adjust-the-precision-of-the-results\"><span style=\"color: #000000;\"><strong>You can use printf to adjust the precision of the results:<\/strong><\/span><\/h4>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Bash Code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">Mac-n-Cheese:~ nicolas$ awk &quot;BEGIN {printf \\&quot;%.2f\\n\\&quot;, 100\/3}&quot;<br\/>33.33<\/code><\/pre> <\/div>\n<p>When using negative values, make sure to leave a white space between signs.<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Bash Code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">Mac-n-Cheese:~ nicolas$ awk &quot;BEGIN {print -8.4 - -8}&quot;<br\/>-0.4<\/code><\/pre> <\/div>\n","protected":false},"excerpt":{"rendered":"<p>PROBLEM: Using echo &#8220;20+5&#8221; literally produces the text &#8220;20+5&#8221;. What command you use to get the numeric sum, 25 in this case? Also, what&#8217;s the easiest way to do it just using bash for floating point? For example, echo $((3224\/3807.0)) prints 0 :(. You are looking for answers using either the basic command shell (&#8216;command [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1331],"tags":[3259,3255,3262,3263,3251,3250,3261,3256,3258,3252,3253,3249,3254,3260,3257],"class_list":["post-1595","post","type-post","status-publish","format-standard","hentry","category-unix","tag-bash-how-to-calculate-very-long-numbers-in-bash","tag-bash-as-float-calculator","tag-bash-shell-decimal-test","tag-can-i-do-basic-maths-in-bash","tag-comparing-float-values-in-bash","tag-convert-a-float-to-the-next-integer-up-as-opposed-to-the-nearest","tag-copy-single-value-from-a-file-to-a-variable-and-perform-arithmetic-over-it","tag-floating-point-arithmetic-in-unix-shell-script","tag-force-pc-to-calculate-equation-to-infinite-decimal-places","tag-how-can-subtract-2-floats-which-have-been-extracted-from-2-other-files-with-bash","tag-how-to-express-floating-point-expressions-in-bash-scripts","tag-how-to-round-floating-point-numbers-in-shell","tag-how-to-use-the-result-from-last-calculation-in-a-new-calculation-in-wcalc","tag-is-it-possible-to-get-a-decimal-output-from-doing-division-in-bash","tag-performing-math-calculation-in-bash"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/1595","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=1595"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/1595\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=1595"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=1595"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=1595"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}