File: props.sh 1 #!/bin/sh 2 3 # The MIT License (MIT) 4 # 5 # Copyright © 2020-2025 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 case "$1" in 33 -h|--h|-help|--help) 34 awk '/^# +props /, /^$/ { gsub(/^# ?/, ""); print }' "$0" 35 exit 0 36 ;; 37 esac 38 39 awk ' 40 BEGIN { 41 if (ARGC < 2 || ARGV[1] + 0 == 0) { 42 print "props [x] [y...]"; 43 print ""; 44 print "Show various PROPortionS involving 2 numbers;" 45 print "when given only 1 number, show its inverse."; 46 exit(1); 47 } 48 49 # handle the 1-number case 50 if (ARGC == 2) { 51 x = ARGV[1] + 0.0; 52 printf("x = %f\n", x); 53 printf("1 / x ~= %f\n", div(1, x)); 54 exit(0); 55 } 56 57 # get values and sort them 58 x = ARGV[1] + 0.0; # min 59 y = ARGV[2] + 0.0; # max 60 delete ARGV[1]; 61 delete ARGV[2]; 62 if (x > y) { 63 temp = x; 64 x = y; 65 y = temp; 66 } 67 68 printf("min: x = %f\n", x); 69 printf("max: y = %f\n", y); 70 # printf("sum: %f\n", x + y); 71 # printf("diff: %f\n", y - x); 72 # printf("avg: %f\n", (x + y) / 2); 73 print; 74 printf(" x / y ~= %f\n", div(x, y)); 75 printf("1 - (x / y) ~= %f\n", div(y - x, y)); 76 printf(" y / x ~= %f\n", div(y, x)); 77 print; 78 printf("x / (x + y) ~= %f\n", div(x, x + y)); 79 printf("y / (x + y) ~= %f\n", div(y, x + y)); 80 print; 81 printf("x / (y - x) ~= %f\n", div(x, y - x)); 82 printf("y / (y - x) ~= %f\n", div(y, y - x)); 83 # printf("(y - x) / y ~= %f\n", div(y - x, y)); 84 } 85 86 function div(a, b) { 87 if (b != 0) { 88 return a / b; 89 } 90 return (a >= 0 ? "+inf" : "-inf") + 0; 91 } 92 ' "$@"