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