File: props.sh 1 #!/bin/sh 2 3 # The MIT License (MIT) 4 # 5 # Copyright © 2024 pacman64 6 # 7 # Permission is hereby granted, free of charge, to any person obtaining a copy 8 # of this software and associated documentation files (the “Software”), to deal 9 # in the Software without restriction, including without limitation the rights 10 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 # copies of the Software, and to permit persons to whom the Software is 12 # furnished to do so, subject to the following conditions: 13 # 14 # The above copyright notice and this permission notice shall be included in 15 # all copies or substantial portions of the Software. 16 # 17 # THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 # SOFTWARE. 24 25 26 # props [x] [y...] 27 # 28 # Show various PROPortionS involving 2 numbers; when given only 1 number, 29 # show its inverse. 30 31 32 # handle help options 33 case "$1" in 34 -h|--h|-help|--help) 35 awk '/^# +props/, /^$/ { gsub(/^# ?/, ""); print }' "$0" 36 exit 0 37 ;; 38 esac 39 40 awk ' 41 BEGIN { 42 if (ARGC < 2 || ARGV[1] + 0 == 0) { 43 print "props [x] [y...]"; 44 print ""; 45 print "Show various PROPortionS involving 2 numbers;" 46 print "when given only 1 number, show its inverse."; 47 exit(1); 48 } 49 50 # handle the 1-number case 51 if (ARGC == 2) { 52 x = ARGV[1] + 0.0; 53 printf("x = %f\n", x); 54 printf("1 / x ~= %f\n", div(1, x)); 55 exit(0); 56 } 57 58 # get values and sort them 59 x = ARGV[1] + 0.0; # min 60 y = ARGV[2] + 0.0; # max 61 delete ARGV[1]; 62 delete ARGV[2]; 63 if (x > y) { 64 temp = x; 65 x = y; 66 y = temp; 67 } 68 69 printf("min: x = %f\n", x); 70 printf("max: y = %f\n", y); 71 # printf("sum: %f\n", x + y); 72 # printf("diff: %f\n", y - x); 73 # printf("avg: %f\n", (x + y) / 2); 74 print; 75 printf(" x / y ~= %f\n", div(x, y)); 76 printf("1 - (x / y) ~= %f\n", div(y - x, y)); 77 printf(" y / x ~= %f\n", div(y, x)); 78 print; 79 printf("x / (x + y) ~= %f\n", div(x, x + y)); 80 printf("y / (x + y) ~= %f\n", div(y, x + y)); 81 print; 82 printf("x / (y - x) ~= %f\n", div(x, y - x)); 83 printf("y / (y - x) ~= %f\n", div(y, y - x)); 84 # printf("(y - x) / y ~= %f\n", div(y - x, y)); 85 } 86 87 function div(a, b) { 88 if (b != 0) { 89 return a / b; 90 } 91 return (a >= 0 ? "+inf" : "-inf") + 0; 92 } 93 ' "$@"