floating_point_calculation
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| floating_point_calculation [2012/01/27 16:02] – k2patel | floating_point_calculation [2020/08/10 02:35] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== Floating Point Calculation ====== | ====== Floating Point Calculation ====== | ||
| This is an example represent how to compute Floating point in bash. | This is an example represent how to compute Floating point in bash. | ||
| + | |||
| + | ==== Using in Script for Comparision ==== | ||
| + | |||
| + | To use '' | ||
| + | |||
| + | <code oobas> | ||
| + | float_eval EXPRESSION | ||
| + | and | ||
| + | float_cond CONDITIONAL-EXPRESSION | ||
| + | </ | ||
| + | Both functions expect a single floating point expression, '' | ||
| + | |||
| + | Usage is quite simple: | ||
| + | |||
| + | <code bash> | ||
| + | float_eval '12.0 / 3.0' | ||
| + | if float_cond '10.0 > 9.0'; then | ||
| + | echo 'As expected, 10.0 is greater than 9.0' | ||
| + | fi | ||
| + | a=12.0 | ||
| + | b=3.0 | ||
| + | c=$(float_eval "$a / $b") | ||
| + | </ | ||
| + | The code for the functions follows: | ||
| + | |||
| + | <code bash> | ||
| + | #!/bin/bash | ||
| + | # | ||
| + | # Floating point number functions. | ||
| + | |||
| + | ##################################################################### | ||
| + | # Default scale used by float functions. | ||
| + | |||
| + | float_scale=2 | ||
| + | |||
| + | |||
| + | ##################################################################### | ||
| + | # Evaluate a floating point number expression. | ||
| + | |||
| + | function float_eval() | ||
| + | { | ||
| + | local stat=0 | ||
| + | local result=0.0 | ||
| + | if [[ $# -gt 0 ]]; then | ||
| + | result=$(echo " | ||
| + | stat=$? | ||
| + | if [[ $stat -eq 0 && | ||
| + | fi | ||
| + | echo $result | ||
| + | return $stat | ||
| + | } | ||
| + | |||
| + | |||
| + | ##################################################################### | ||
| + | # Evaluate a floating point number conditional expression. | ||
| + | |||
| + | function float_cond() | ||
| + | { | ||
| + | local cond=0 | ||
| + | if [[ $# -gt 0 ]]; then | ||
| + | cond=$(echo " | ||
| + | if [[ -z " | ||
| + | if [[ " | ||
| + | fi | ||
| + | local stat=$((cond == 0)) | ||
| + | return $stat | ||
| + | } | ||
| + | |||
| + | |||
| + | # Test code if invoked directly. | ||
| + | if [[ $(basename $0 .sh) == ' | ||
| + | # Use command line arguments if there are any. | ||
| + | if [[ $# -gt 0 ]]; then | ||
| + | echo $(float_eval $*) | ||
| + | else | ||
| + | # Turn off pathname expansion so * doesn' | ||
| + | set -f | ||
| + | e=" | ||
| + | echo $e is $(float_eval " | ||
| + | e=" | ||
| + | echo $e is $(float_eval " | ||
| + | if float_cond '10.0 > 9.3'; then | ||
| + | echo "10.0 is greater than 9.3" | ||
| + | fi | ||
| + | if float_cond '10.0 < 9.3'; then | ||
| + | echo " | ||
| + | else | ||
| + | echo "10.0 is not less than 9.3" | ||
| + | fi | ||
| + | a=12.0 | ||
| + | b=3.0 | ||
| + | c=$(float_eval "$a / $b") | ||
| + | echo "$a / $b" is $c | ||
| + | set +f | ||
| + | fi | ||
| + | fi | ||
| + | |||
| + | # vim: tabstop=4: shiftwidth=4: | ||
| + | # kate: tab-width 4; indent-width 4; replace-tabs false; | ||
| + | </ | ||
| + | The work of the functions is done by feeding the arguments to '' | ||
| + | |||
| + | <code bash> | ||
| + | | ||
| + | </ | ||
| + | By default '' | ||
| + | |||
| + | The main gotcha here has to do with the fact that " | ||
| + | |||
| + | If you save the script as '' | ||
| + | |||
| + | <code bash> | ||
| + | $ sh float.sh | ||
| + | 12.5 / 3.2 is 3.90 | ||
| + | 100.4 / 4.2 + 3.2 * 6.5 is 44.70 | ||
| + | 10.0 is greater than 9.3 | ||
| + | 10.0 is not less than 9.3 | ||
| + | 12.0 / 3.0 is 4.00 | ||
| + | </ | ||
| + | The one unaswered question you may have is: "and why would I want to do this?" Next time around I'll show you one place you can put this to real world use. | ||
| + | |||
| + | Ref : [[ http:// | ||
| + | |||
| + | ==== Doing Calculation ==== | ||
| + | |||
| **Details** | **Details** | ||
| Line 129: | Line 254: | ||
| </ | </ | ||
| - | Ref. [[http:// | + | Ref. [[ http:// |
floating_point_calculation.1327680141.txt.gz · Last modified: 2020/08/10 02:30 (external edit)
