User Tools

Site Tools


maths

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
maths [2015/05/09 21:15] – created luke7858maths [2024/05/23 07:26] (current) – external edit 127.0.0.1
Line 1: Line 1:
-You can use bc in bash to do math calculations with decimals. +=== bc === 
-\\+You can use bc in bash to do maths calculations with decimals. The man pages can be found:
 <sxh bash> <sxh bash>
 http://unixhelp.ed.ac.uk/CGI/man-cgi?bc+1 http://unixhelp.ed.ac.uk/CGI/man-cgi?bc+1
 </sxh> </sxh>
 \\ \\
 +== Example Calulation ==
 <sxh bash> <sxh bash>
 bc -l <<< "20 / ( 20 + 80) * 100 " bc -l <<< "20 / ( 20 + 80) * 100 "
Line 12: Line 13:
 20.00000000000000000000 20.00000000000000000000
 </sxh> </sxh>
 +<sxh bash>
 +-l = --mathlib = Defines the standard math library
 +</sxh>
 +\\
 +=== Limiting the decimal place ===
 You can then use this with printf to limit the number of decimals shown: You can then use this with printf to limit the number of decimals shown:
 \\The example below shows the results to 3 decimal places. You can replace the 3 with what ever value you wish to place \\The example below shows the results to 3 decimal places. You can replace the 3 with what ever value you wish to place
Line 21: Line 27:
 <sxh bash> <sxh bash>
 bc -l <<< "($used / ( $free + $used) ) * 100 ") bc -l <<< "($used / ( $free + $used) ) * 100 ")
 +</sxh>
 +\\
 +=== Comparing Decimals with bc ===
 +You are able to compare decimal numbers using bc. The output will return 1 or 0 depending on the results. You can then use this with an if statment -le / -ge etc
 +<sxh bash>
 +1.4 '<' 1.3 | bc -l
 +</sxh>
 +\\
 +<sxh bash>
 +example= $(echo 1.4 '<' 1.3 | bc -l)
 +if [ $example-eq 0 ]; then
 +elif [ $example-eq 1  ]; then
 +fi
 </sxh> </sxh>
maths.1431206101.txt.gz · Last modified: 2024/05/23 07:26 (external edit)

Except where otherwise noted, content on this wiki is licensed under the following license: Public Domain
Public Domain Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki