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 -O3 -flto -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 #ifdef RED_ERRORS 41 #define ERROR_STYLE "\x1b[38;2;204;0;0m" 42 #ifdef __APPLE__ 43 #define ERROR_STYLE "\x1b[31m" 44 #endif 45 #define RESET_STYLE "\x1b[0m" 46 #else 47 #define ERROR_STYLE 48 #define RESET_STYLE 49 #endif 50 51 #define ERROR_LINE(MSG) (ERROR_STYLE MSG RESET_STYLE "\n") 52 53 const char* info = "" 54 "frapp [decimal number...]\n" 55 "\n" 56 "FRactions APProximations tries to find fractions which closely approximate\n" 57 "the decimal number given.\n" 58 ""; 59 60 void frapp(FILE* w, const double value) { 61 const double pos_value = fabs(value); 62 double min_diff = pos_value; 63 64 for (double den = 1; den < 100 * 1000 * 1000; den++) { 65 const double start = floor(pos_value * den); 66 const double stop = ceil(pos_value * den); 67 68 for (double num = start; num <= stop; num++) { 69 const double v = num / den; 70 const double diff = (v - pos_value) / pos_value; 71 const double abs_diff = fabs(diff); 72 if (abs_diff >= min_diff) { 73 continue; 74 } 75 76 if (value < 0) { 77 fprintf(w, "-%.0f/%.0f\t-%.15f\t", num, den, v); 78 } else { 79 fprintf(w, "%.0f/%.0f\t%.15f\t", num, den, v); 80 } 81 if (abs_diff == 0) { 82 fputc('~', w); 83 } 84 fprintf(w, "%g\n", diff); 85 86 min_diff = abs_diff; 87 if (feof(w)) { 88 return; 89 } 90 } 91 } 92 } 93 94 int main(int argc, char** argv) { 95 #ifdef _WIN32 96 setmode(fileno(stdin), O_BINARY); 97 // ensure output lines end in LF instead of CRLF on windows 98 setmode(fileno(stdout), O_BINARY); 99 setmode(fileno(stderr), O_BINARY); 100 #endif 101 102 if (argc < 2) { 103 // fprintf(stdout, "%s", info); 104 fprintf(stderr, "%s", info); 105 return 0; 106 } else { 107 if ( 108 strcmp(argv[1], "-h") == 0 || 109 strcmp(argv[1], "-help") == 0 || 110 strcmp(argv[1], "--h") == 0 || 111 strcmp(argv[1], "--help") == 0 112 ) { 113 fprintf(stdout, "%s", info); 114 return 0; 115 } 116 } 117 118 size_t errors = 0; 119 for (size_t i = 1; i < argc && !feof(stdout); i++) { 120 char* end; 121 double v = strtod(argv[i], &end); 122 123 if (*end != 0 && argv[i] != end) { 124 const char* fmt = ERROR_LINE("'%s' isn't a valid number"); 125 fprintf(stderr, fmt, argv[i]); 126 errors++; 127 continue; 128 } 129 130 frapp(stdout, v); 131 } 132 133 return errors == 0 ? 0 : 1; 134 }