{"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 \u201c20+5\u201d literally produces the text \u201c20+5\u201d.<\/li>\n<li>What command you use to get the numeric sum, 25 in this case?<\/li>\n<li>Also, what\u2019s 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 (\u2018command line\u2019) 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[pastacode lang=\u201dbash\u201d manual=\u201d%24%20echo%20%2225%20%2B%205%22%20%7C%20bc%0A30%0A\u201d message=\u201dBash Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>Alternatively bc <<< 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[pastacode lang=\u201dbash\u201d manual=\u201d%24%20bc%0Abc%201.06.95%0ACopyright%201991-1994%2C%201997%2C%201998%2C%202000%2C%202004%2C%202006%20Free%20Software%20Foundation%2C%20Inc.%0AThis%20is%20free%20software%20with%20ABSOLUTELY%20NO%20WARRANTY.%0AFor%20details%20type%20%60warranty\u2019.%20%0A25%20%2B%205%0A30%0A\u201d message=\u201dBash Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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[pastacode lang=\u201dbash\u201d manual=\u201dc%20%2225%20%2B%205%22%20%0A\u201d message=\u201dBash Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\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[pastacode lang=\u201dbash\u201d manual=\u201dC-style%20arbitrary%20precision%20calculator%20(version%202.12.3.3)%0ACalc%20is%20open%20software.%20For%20license%20details%20type%3A%20%20help%20copyright%0A%5BType%20%22exit%22%20to%20exit%2C%20or%20%22help%22%20for%20help.%5D%0A%3B%202%2B4%0A6%0A%3B%203%2B5%0A8%0A%3B%203.4%2B5%0A8.4%0A%3B%202%5E4%0A16%0A%3B%20exit%0A\u201d message=\u201dBash Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>Or you use it with the expression as an argument and it will provide the answer and then exit<\/p>\n[pastacode lang=\u201dbash\u201d manual=\u201d%24calc%202%20%2B%204%0A%20%20%20%206%0A%24%0A\u201d message=\u201dBash Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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[pastacode lang=\u201dbash\u201d manual=\u201decho%20%24((20%2B5))\u201d message=\u201dBash Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<h4 id=\"or-expr\"><span style=\"color: #000000;\"><strong>or expr:<\/strong><\/span><\/h4>\n[pastacode lang=\u201dbash\u201d manual=\u201dexpr%2020%20%2B%205%0A\u201d message=\u201dBAsh Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\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[pastacode lang=\u201dbash\u201d manual=\u201d%20%20%20%20echo%20%2220%2B5%22%20%7C%20bc\u201d message=\u201dBash Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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[pastacode lang=\u201dbash\u201d manual=\u201dMac-n-Cheese%3A~%20nicolas%24%20expr%201%20%2B%201%0A2%0AMac-n-Cheese%3A~%20nicolas%24%20myvar%3D%24(expr%201%20%2B%201)%0AMac-n-Cheese%3A~%20nicolas%24%20echo%20%24myvar%0A2%0AMac-n-Cheese%3A~%20nicolas%24%20expr%20%24myvar%20%2B%201%0A3%0AMac-n-Cheese%3A~%20nicolas%24%20expr%20%24myvar%20%2F%203%0A1%0AMac-n-Cheese%3A~%20nicolas%24%20expr%20%24myvar%20%5C*%203%0A9%0A\u201d message=\u201dBash Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>When doing a \u201cmultiply by\u201d make sure to backslash the \u201casterisk\u201d 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[pastacode lang=\u201dbash\u201d manual=\u201dMac-n-Cheese%3A~%20nicolas%24%20echo%20%24myvar%0A6%0AMac-n-Cheese%3A~%20nicolas%24%20let%20myvar%2B%3D1%0AMac-n-Cheese%3A~%20nicolas%24%20echo%20%24myvar%0A7%0AMac-n-Cheese%3A~%20nicolas%24%20let%20myvar%2B1%0AMac-n-Cheese%3A~%20nicolas%24%20echo%20%24myvar%0A7%0AMac-n-Cheese%3A~%20nicolas%24%20let%20myvar2%3Dmyvar%2B1%0AMac-n-Cheese%3A~%20nicolas%24%20echo%20%24myvar2%0A8%0A\u201d message=\u201dBash Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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[pastacode lang=\u201dbash\u201d manual=\u201dMac-n-Cheese%3A~%20nicolas%24%20echo%20%24myvar%0A3%0AMac-n-Cheese%3A~%20nicolas%24%20echo%20%24((myvar%2B2))%0A5%0AMac-n-Cheese%3A~%20nicolas%24%20echo%20%24%5Bmyvar%2B2%5D%0A5%0AMac-n-Cheese%3A~%20nicolas%24%20myvar%3D%24((myvar%2B3))%0A\u201d message=\u201dBash Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\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[pastacode lang=\u201dbash\u201d manual=\u201dMac-n-Cheese%3A~%20nicolas%24%20echo%20%24myvar%0A3%0AMac-n-Cheese%3A~%20nicolas%24%20echo%20%24((myvar%2B%2B))%0A3%0AMac-n-Cheese%3A~%20nicolas%24%20echo%20%24myvar%0A4%0AMac-n-Cheese%3A~%20nicolas%24%20echo%20%24((%2B%2Bmyvar))%0A5%0AMac-n-Cheese%3A~%20nicolas%24%20echo%20%24myvar%0A5%0A\u201d message=\u201dBash Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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 \u2013 An arbitrary precision calculator language\u201d.<\/p>\n[pastacode lang=\u201dbash\u201d manual=\u201dMac-n-Cheese%3A~%20nicolas%24%20bc%0Abc%201.06%0ACopyright%201991-1994%2C%201997%2C%201998%2C%202000%20Free%20Software%20Foundation%2C%20Inc.%0AThis%20is%20free%20software%20with%20ABSOLUTELY%20NO%20WARRANTY.%0AFor%20details%20type%20%60warranty\u2019.%0A3*5.2%2B7%2F8%0A15.6%0A15.6%2B299.33*2.3%2F7.4%0A108.6%0A\u201d message=\u201dBash Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>you can use the STDIN to send your formula to \u201cbc\u201d then get the output on STDOUT.<\/p>\n[pastacode lang=\u201dbash\u201d manual=\u201dMac-n-Cheese%3A~%20nicolas%24%20echo%20%223.4%2B7%2F8-(5.94*3.14)%22%20%7C%20bc%0A-15.25%0A\u201d message=\u201dBash Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<h4 id=\"using-the-here-doc-notation\"><span style=\"color: #000000;\"><strong> using the here-doc notation:<\/strong><\/span><\/h4>\n[pastacode lang=\u201dbash\u201d manual=\u201dMac-n-Cheese%3A~%20nicolas%24%20bc%20%3C%3C%3C%20%223.4%2B7%2F8-(5.94*3.14)%22%0A-15.25%0A\u201d message=\u201dBash Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\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[pastacode lang=\u201dbash\u201d manual=\u201d%0AMac-n-Cheese%3A~%20nicolas%24%20echo%20%222%2F3%22%20%7C%20bc%0A0%0AMac-n-Cheese%3A~%20nicolas%24%20echo%20%22scale%3D2%3B%202%2F3%22%20%7C%20bc%0A.66%0AMac-n-Cheese%3A~%20nicolas%24%20echo%20%22(2%2F3)%2B(7%2F8)%22%20%7C%20bc%0A0%0AMac-n-Cheese%3A~%20nicolas%24%20echo%20%22scale%3D2%3B(2%2F3)%2B(7%2F8)%22%20%7C%20bc%0A1.53%0AMac-n-Cheese%3A~%20nicolas%24%20echo%20%22scale%3D4%3B(2%2F3)%2B(7%2F8)%22%20%7C%20bc%0A1.5416%0AMac-n-Cheese%3A~%20nicolas%24%20echo%20%22scale%3D6%3B(2%2F3)%2B(7%2F8)%22%20%7C%20bc%0A1.541666%0AMac-n-Cheese%3A~%20nicolas%24%20echo%20%22(2%2F3)%2B(7%2F8)%22%20%7C%20bc%20-l%0A1.54166666666666666666%0A\u201d message=\u201dBash Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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[pastacode lang=\u201dbash\u201d manual=\u201dMac-n-Cheese%3A~%20nicolas%24%20awk%20%22BEGIN%20%7Bprint%20100%2F3%7D%22%0A33.3333%0A\u201d message=\u201dBash Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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[pastacode lang=\u201dbash\u201d manual=\u201dMac-n-Cheese%3A~%20nicolas%24%20awk%20%22BEGIN%20%7Bprintf%20%5C%22%25.2f%5Cn%5C%22%2C%20100%2F3%7D%22%0A33.33%0A\u201d message=\u201dBash Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>When using negative values, make sure to leave a white space between signs.<\/p>\n[pastacode lang=\u201dbash\u201d manual=\u201dMac-n-Cheese%3A~%20nicolas%24%20awk%20%22BEGIN%20%7Bprint%20-8.4%20-%20-8%7D%22%0A-0.4%0A\u201d message=\u201dBash Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n","protected":false},"excerpt":{"rendered":"<p>PROBLEM: Using echo \u201c20+5\u201d literally produces the text \u201c20+5\u201d. What command you use to get the numeric sum, 25 in this case? Also, what\u2019s 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 (\u2018command [&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}]}}