File: frapp.c
   1 /*
   2 The MIT License (MIT)
   3 
   4 Copyright © 2020-2025 pacman64
   5 
   6 Permission is hereby granted, free of charge, to any person obtaining a copy of
   7 this software and associated documentation files (the “Software”), to deal
   8 in the Software without restriction, including without limitation the rights to
   9 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  10 of the Software, and to permit persons to whom the Software is furnished to do
  11 so, subject to the following conditions:
  12 
  13 The above copyright notice and this permission notice shall be included in all
  14 copies or substantial portions of the Software.
  15 
  16 THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22 SOFTWARE.
  23 */
  24 
  25 /*
  26 You can build this command-line app by running
  27 
  28 cc -Wall -s -O2 -o ./frapp ./frapp.c
  29 */
  30 
  31 #include <math.h>
  32 #include <stdio.h>
  33 #include <stdlib.h>
  34 #include <string.h>
  35 
  36 #ifdef _WIN32
  37 #include <windows.h>
  38 #endif
  39 
  40 const char* info = ""
  41 "frapp [decimal number...]\n"
  42 "\n"
  43 "FRactions APProximations tries to find fractions which closely approximate\n"
  44 "the decimal number given.\n"
  45 "";
  46 
  47 void frapp(const double value) {
  48     const double pos_value = fabs(value);
  49     double min_diff = pos_value;
  50 
  51     for (double den = 1; den < 100 * 1000 * 1000; den++) {
  52         const double start = floor(pos_value * den);
  53         const double stop = ceil(pos_value * den);
  54 
  55         for (double num = start; num <= stop; num++) {
  56             const double v = num / den;
  57             const double diff = (v - pos_value) / pos_value;
  58             const double abs_diff = fabs(diff);
  59             if (abs_diff < min_diff) {
  60                 if (value < 0) {
  61                     putc('-', stdout);
  62                 }
  63                 fprintf(stdout, "%.0f/%.0f\t%.15f\t", num, den, v);
  64                 if (abs_diff == 0) {
  65                     putc('~', stdout);
  66                 }
  67                 fprintf(stdout, "%g\n", diff);
  68 
  69                 min_diff = abs_diff;
  70             }
  71         }
  72     }
  73 }
  74 
  75 int main(int argc, char** argv) {
  76 #ifdef _WIN32
  77     setmode(fileno(stdin), O_BINARY);
  78     // ensure output lines end in LF instead of CRLF on windows
  79     setmode(fileno(stdout), O_BINARY);
  80     setmode(fileno(stderr), O_BINARY);
  81 #endif
  82 
  83     if (argc < 2) {
  84         // fprintf(stdout, "%s", info);
  85         fprintf(stderr, "%s", info);
  86         return 0;
  87     } else {
  88         if (
  89             strcmp(argv[1], "-h") == 0 ||
  90             strcmp(argv[1], "-help") == 0 ||
  91             strcmp(argv[1], "--h") == 0 ||
  92             strcmp(argv[1], "--help") == 0
  93         ) {
  94             fprintf(stdout, "%s", info);
  95             return 0;
  96         }
  97     }
  98 
  99     size_t errors = 0;
 100 
 101     for (size_t i = 1; i < argc; i++) {
 102         char* end;
 103         double v = strtod(argv[i], &end);
 104 
 105         if (*end != 0) {
 106             const char* fmt = "\x1b[31m'%s' isn't a valid number\x1b[0m\n";
 107             fprintf(stderr, fmt, argv[i]);
 108             errors++;
 109             continue;
 110         }
 111 
 112         frapp(v);
 113     }
 114 
 115     return errors == 0 ? 0 : 1;
 116 }