Arbitrary Precision Decimal Calculator for School Homework
The 4 fundamental arithmetic operations + - x : by Arduino and much more.
Components and supplies
1
n7 Arbitary Precision Decimal Calculator
1
arduino zero
Apps and platforms
1
Arduino IDE 2.0 (beta)
Project description
Code
n7 Arbitrary Precision Decimal Calculator list
cpp
c++ language
1#include <num7.h> 2using namespace num7; 3//PROTOTYPES 4void display_NOT_VALID_OPERATOR(char*); 5void display_help(void); 6 7int n7(int n_args, char* args[]) { 8 if (n_args < 2) { display_help(); return 2; } //ONLY FILE_NAME 9 NUM result = 0; //FINAL RESULT 10 i32 result_int = 0; //FINAL INTEGER RESULT FOR RELATIONAL OPERATORS 11 NUM OP2 = 0; //SECOND OPERAND 12 i32 SIGN = 0, //SIGN FLAG (1 => '+' OR '-') 13 P = 32, //PRECISION DEFAULT 14 L = 0; //LENGTH ARGUMENT 15 //ARITHMETIC OPERATION 16 if (!strcmp("<", args[1]) || !strcmp("lt", args[1]) || !strcmp("-lt", args[1])) { 17 if (args[2] == NULL) { display_NOT_VALID_OPERATOR(args[1]); return 2; } 18 SIGN = args[2][0] == '+' || args[2][0] == '-'; 19 if (!SIGN && is_strfmt_int(args[2])) result = i32int(args[2]); 20 else if (SIGN && is_strfmt_int(args[2] + 1)) result = i32int(args[2]); 21 else result = args[2]; 22 SIGN = args[3][0] == '+' || args[3][0] == '-'; 23 if (!SIGN && is_strfmt_int(args[3])) OP2 = i32int(args[3]); 24 else if (SIGN && is_strfmt_int(args[3] + 1)) OP2 = i32int(args[3]); 25 else OP2 = args[3]; 26 result = (result < OP2); 27 } 28 else if (!strcmp("<=", args[1]) || !strcmp("le", args[1]) || !strcmp("-le", args[1])) { 29 if (args[2] == NULL) { display_NOT_VALID_OPERATOR(args[1]); return 2; } 30 SIGN = args[2][0] == '+' || args[2][0] == '-'; 31 if (!SIGN && is_strfmt_int(args[2])) result = i32int(args[2]); 32 else if (SIGN && is_strfmt_int(args[2] + 1)) result = i32int(args[2]); 33 else result = args[2]; 34 SIGN = args[3][0] == '+' || args[3][0] == '-'; 35 if (!SIGN && is_strfmt_int(args[3])) OP2 = i32int(args[3]); 36 else if (SIGN && is_strfmt_int(args[3] + 1)) OP2 = i32int(args[3]); 37 else OP2 = args[3]; 38 result = (result <= OP2); 39 } 40 else if (!strcmp(">", args[1]) || !strcmp("gt", args[1]) || !strcmp("-gt", args[1])) { 41 if (args[2] == NULL) { display_NOT_VALID_OPERATOR(args[1]); return 2; } 42 SIGN = args[2][0] == '+' || args[2][0] == '-'; 43 if (!SIGN && is_strfmt_int(args[2])) result = i32int(args[2]); 44 else if (SIGN && is_strfmt_int(args[2] + 1)) result = i32int(args[2]); 45 else result = args[2]; 46 SIGN = args[3][0] == '+' || args[3][0] == '-'; 47 if (!SIGN && is_strfmt_int(args[3])) OP2 = i32int(args[3]); 48 else if (SIGN && is_strfmt_int(args[3] + 1)) OP2 = i32int(args[3]); 49 else OP2 = args[3]; 50 result = (result > OP2); 51 } 52 else if (!strcmp(">=", args[1]) || !strcmp("ge", args[1]) || !strcmp("-ge", args[1])) { 53 if (args[2] == NULL) { display_NOT_VALID_OPERATOR(args[1]); return 2; } 54 SIGN = args[2][0] == '+' || args[2][0] == '-'; 55 if (!SIGN && is_strfmt_int(args[2])) result = i32int(args[2]); 56 else if (SIGN && is_strfmt_int(args[2] + 1)) result = i32int(args[2]); 57 else result = args[2]; 58 SIGN = args[3][0] == '+' || args[3][0] == '-'; 59 if (!SIGN && is_strfmt_int(args[3])) OP2 = i32int(args[3]); 60 else if (SIGN && is_strfmt_int(args[3] + 1)) OP2 = i32int(args[3]); 61 else OP2 = args[3]; 62 result = (result >= OP2); 63 } 64 else if (!strcmp("=", args[1]) || !strcmp("==", args[1]) || !strcmp("eq", args[1]) || !strcmp("-eq", args[1])) { 65 if (args[2] == NULL) { display_NOT_VALID_OPERATOR(args[1]); return 2; } 66 SIGN = args[2][0] == '+' || args[2][0] == '-'; 67 if (!SIGN && is_strfmt_int(args[2])) result = i32int(args[2]); 68 else if (SIGN && is_strfmt_int(args[2] + 1)) result = i32int(args[2]); 69 else result = args[2]; 70 SIGN = args[3][0] == '+' || args[3][0] == '-'; 71 if (!SIGN && is_strfmt_int(args[3])) OP2 = i32int(args[3]); 72 else if (SIGN && is_strfmt_int(args[3] + 1)) OP2 = i32int(args[3]); 73 else OP2 = args[3]; 74 result = (result == OP2); 75 } 76 else if (!strcmp("!=", args[1]) || !strcmp("ne", args[1]) || !strcmp("-ne", args[1])) { 77 if (args[2] == NULL) { display_NOT_VALID_OPERATOR(args[1]); return 2; } 78 SIGN = args[2][0] == '+' || args[2][0] == '-'; 79 if (!SIGN && is_strfmt_int(args[2])) result = i32int(args[2]); 80 else if (SIGN && is_strfmt_int(args[2] + 1)) result = i32int(args[2]); 81 else result = args[2]; 82 SIGN = args[3][0] == '+' || args[3][0] == '-'; 83 if (!SIGN && is_strfmt_int(args[3])) OP2 = i32int(args[3]); 84 else if (SIGN && is_strfmt_int(args[3] + 1)) OP2 = i32int(args[3]); 85 else OP2 = args[3]; 86 result = (result != OP2); 87 } 88 else if (!strcmp("add", args[1])) result = add(args[2], args[3]); 89 else if (!strcmp("sub", args[1])) result = sub(args[2], args[3]); 90 else if (!strcmp("mul", args[1])) result = mul(args[2], args[3]); 91 else if (!strcmp("div", args[1])) result = div(args[2], args[3], n_args > 4 ? i32int(args[4], 10, 0) : 40); 92 else if (!strcmp("mod", args[1])) result = mod(args[2], args[3]); 93 else if (!strcmp("inv", args[1])) result = inv(args[2], n_args > 3 ? i32int(args[3], 10, 0) : 32); 94 else if (!strcmp("x2", args[1])) result = x2(args[2]); 95 else if (!strcmp("x3", args[1])) result = x3(args[2]); 96 else if (!strcmp("xy", args[1])) result = xy(args[2], args[3]); 97 else if (!strcmp("10y", args[1])) result = _10y(args[2]); 98 else if (!strcmp("2y", args[1])) result = _2y(args[2]); 99 else if (!strcmp("ey", args[1])) result = _ey(args[2]); 100 else if (!strcmp("abs", args[1]))result = Abs(args[2]); 101 else if (!strcmp("10x", args[1]))result = _10x(args[2]); 102 else if (!strcmp("100x", args[1]))result = _100x(args[2]); 103 else if (!strcmp("1000x", args[1]))result = _1000x(args[2]); 104 else if (!strcmp("10div", args[1]))result = _10div(args[2]); 105 else if (!strcmp("100div", args[1]))result = _100div(args[2]); 106 else if (!strcmp("1000div", args[1]))result = _1000div(args[2]); 107 else if (!strcmp("shift", args[1])) { NUM T(args[2]); result = shift(T, args[3]); } 108 else if (!strcmp("pct", args[1])) result = pct(args[2], (n_args > 3 ? args[3] : "1.0")); 109 else if (!strcmp("pth", args[1])) result = pth(args[2], (n_args > 3 ? args[3] : "1.0")); 110 else if (!strcmp("spoff", args[1])) result = spinoff(args[2], (n_args > 3 ? args[3] : "1.0")); 111 else if (!strcmp("spon", args[1])) result = spinon(args[2], (n_args > 3 ? args[3] : "1.0")); 112 113 else if (!strcmp("sqr", args[1])) result = sqr(args[2], i32int(n_args > 3 ? args[3] : "6", 10, 0)); 114 else if (!strcmp("fact", args[1])) result = fact(i32int(args[2])); 115 else if (!strcmp("rnd", args[1])) result = round(args[2], i32int(n_args > 3 ? args[3] : "2", 10, 0)); 116 else if (!strcmp("rndb", args[1])) result = round_bank(args[2], i32int(n_args > 3 ? args[3] : "2", 10, 0)); 117 else if (!strcmp("rndc", args[1])) result = round_ceil(args[2], i32int(n_args > 3 ? args[3] : "0", 10, 0)); 118 else if (!strcmp("rndf", args[1])) result = round_floor(args[2],i32int(n_args > 3 ? args[3] : "0", 10, 0)); 119 120 else if (!strcmp("is_zero", args[1])) { 121 NUM T(args[2]); 122 result = is_zero(&T); 123 } 124 else if (!strcmp("is_pos", args[1])) { 125 NUM T(args[2]); 126 result = T.is_positive(); 127 } 128 else if (!strcmp("is_neg", args[1])) { 129 NUM T(args[2]); 130 result = T.is_negative(); 131 } 132 else if (!strcmp("is_even", args[1])) { 133 NUM T(args[2]); 134 result = T.is_even(); 135 } 136 else if (!strcmp("is_odd", args[1])) { 137 NUM T(args[2]); 138 result = T.is_odd(); 139 } 140 else if (!strcmp("is_int", args[1])) { 141 NUM T(args[2]); 142 result = T.is_integer(); 143 } 144 else if (!strcmp("is_float", args[1])) { 145 NUM T(args[2]); 146 result = T.is_floating(); 147 } 148 else if (!strcmp("is_prime", args[1])) { 149 NUM T(args[2]); 150 result = T.is_prime(); 151 } 152 else if (!strcmp("sum", args[1])) { 153 SIGN = args[2][0] == '+' || args[2][0] == '-'; 154 if (!SIGN && is_strfmt_int(args[2])) result = i32int(args[2]); 155 else if (SIGN && is_strfmt_int(args[2] + 1)) result = i32int(args[2]); 156 else result = args[2]; 157 for (int i = 3; i < n_args; i++) { 158 SIGN = args[i][0] == '+' || args[i][0] == '-'; 159 if (!SIGN && is_strfmt_int(args[i])) OP2 = i32int(args[i]); 160 else if (SIGN && is_strfmt_int(args[i] + 1)) OP2 = i32int(args[i]); 161 else OP2 = args[i]; 162 result = add(result, OP2); 163 } 164 } 165 else if (!strcmp("mean", args[1])) { 166 SIGN = args[2][0] == '+' || args[2][0] == '-'; 167 if (!SIGN && is_strfmt_int(args[2])) result = i32int(args[2]); 168 else if (SIGN && is_strfmt_int(args[2] + 1)) result = i32int(args[2]); 169 else result = args[2]; 170 for (int i = 3; i < n_args; i++) { 171 SIGN = args[i][0] == '+' || args[i][0] == '-'; 172 if (!SIGN && is_strfmt_int(args[i])) OP2 = i32int(args[i]); 173 else if (SIGN && is_strfmt_int(args[i] + 1)) OP2 = i32int(args[i]); 174 else OP2 = args[i]; 175 result = add(result, OP2); 176 } 177 result /= (n_args - 2); 178 } 179 else if (!strcmp("min", args[1])) { 180 SIGN = args[2][0] == '+' || args[2][0] == '-'; 181 if (!SIGN && is_strfmt_int(args[2])) result = i32int(args[2]); 182 else if (SIGN && is_strfmt_int(args[2] + 1)) result = i32int(args[2]); 183 else result = args[2]; 184 for (int i = 3; i < n_args; i++) { 185 SIGN = args[i][0] == '+' || args[i][0] == '-'; 186 if (!SIGN && is_strfmt_int(args[i])) OP2 = i32int(args[i]); 187 else if (SIGN && is_strfmt_int(args[i] + 1)) OP2 = i32int(args[i]); 188 else OP2 = args[i]; 189 result = (result < OP2 ? result : OP2); 190 } 191 } 192 else if (!strcmp("max", args[1])) { 193 SIGN = args[2][0] == '+' || args[2][0] == '-'; 194 if (!SIGN && is_strfmt_int(args[2])) result = i32int(args[2]); 195 else if (SIGN && is_strfmt_int(args[2] + 1)) result = i32int(args[2]); 196 else result = args[2]; 197 for (int i = 3; i < n_args; i++) { 198 SIGN = args[i][0] == '+' || args[i][0] == '-'; 199 if (!SIGN && is_strfmt_int(args[i])) OP2 = i32int(args[i]); 200 else if (SIGN && is_strfmt_int(args[i] + 1)) OP2 = i32int(args[i]); 201 else OP2 = args[i]; 202 result = (result > OP2 ? result : OP2); 203 } 204 } 205 else if (!strcmp("pi", args[1])) { result = pi(); } 206 else if (!strcmp("e", args[1])) { result = e(); } 207 ///////////////////////// PRINT AND DISPLAY ///////////////////////// 208 else if (!strcmp("sci", args[1])) { //CODE: n7 sci 200.0e-5 => 2.0e-3 209 result = args[2]; 210 result.into_exp(); //SCIENTIFIC NOTATION 211 if (error()) return 2; 212 print_exp(result, "\n"); //OUTPUT 213 return 0; 214 } 215 else if (!strcmp("exp", args[1])) { //CODE: n7 exp 200.0e-5 => 200.0e-5 216 result = args[2]; 217 if (error()) return 2; 218 print_exp(result, "\n"); //EXPONENTIAL AND SCIENTIFIC NOTATION OUTPUT 219 return 0; 220 } 221 else if (!strcmp("bits", args[1])) { 222 result = args[2]; 223 if (error()) return 2; //FAILURE 224 char* ram = result.bits(args[3] ? i32int(args[3]) : 1); 225 print(ram, "\n"); //OUTPUT 226 free(ram); 227 return 0; 228 } 229 else if (!strcmp("-h", args[1]) || !strcmp("--help", args[1])) { //INSTRUCTION HELP 230 display_help(); 231 return 0; 232 } 233 else if (strlen(args[1]) > 1) { 234 display_NOT_VALID_OPERATOR(args[1]); 235 return 2; 236 } 237 else switch (args[1][0]) { ///////////////// SWITCH CASE ///////////////// 238 case '+': 239 if (args[2] == NULL) { display_NOT_VALID_OPERATOR(args[1]); return 2; } 240 SIGN = args[2][0] == '+' || args[2][0] == '-'; 241 if (!SIGN && is_strfmt_int(args[2])) result = i32int(args[2]); 242 else if (SIGN && is_strfmt_int(args[2] + 1)) result = i32int(args[2]); 243 else result = args[2]; 244 for (int i = 3; i < n_args; i++) { 245 SIGN = args[i][0] == '+' || args[i][0] == '-'; 246 if (!SIGN && is_strfmt_int(args[i])) OP2 = i32int(args[i]); 247 else if (SIGN && is_strfmt_int(args[i] + 1)) OP2 = i32int(args[i]); 248 else OP2 = args[i]; 249 result = add(result, OP2); 250 } 251 break; 252 case '-': 253 if (args[2] == NULL) { display_NOT_VALID_OPERATOR(args[1]); return 2; } 254 SIGN = args[2][0] == '+' || args[2][0] == '-'; 255 if (!SIGN && is_strfmt_int(args[2])) result = i32int(args[2]); 256 else if (SIGN && is_strfmt_int(args[2] + 1)) result = i32int(args[2]); 257 else result = args[2]; 258 for (int i = 3; i < n_args; i++) { 259 SIGN = args[i][0] == '+' || args[i][0] == '-'; 260 if (!SIGN && is_strfmt_int(args[i])) OP2 = i32int(args[i]); 261 else if (SIGN && is_strfmt_int(args[i] + 1)) OP2 = i32int(args[i]); 262 else OP2 = args[i]; 263 result = sub(result, OP2); 264 } 265 break; 266 case '*': 267 case 'x': 268 case 'X': 269 if (args[2] == NULL) { display_NOT_VALID_OPERATOR(args[1]); return 2; } 270 SIGN = args[2][0] == '+' || args[2][0] == '-'; 271 if (!SIGN && is_strfmt_int(args[2])) result = i32int(args[2]); 272 else if (SIGN && is_strfmt_int(args[2] + 1)) result = i32int(args[2]); 273 else result = args[2]; 274 for (int i = 3; i < n_args; i++) { 275 SIGN = args[i][0] == '+' || args[i][0] == '-'; 276 if (!SIGN && is_strfmt_int(args[i])) OP2 = i32int(args[i]); 277 else if (SIGN && is_strfmt_int(args[i] + 1)) OP2 = i32int(args[i]); 278 else OP2 = args[i]; 279 result = mul(result, OP2); 280 } 281 break; 282 case '/': 283 case ':': 284 if (args[2] == NULL) { display_NOT_VALID_OPERATOR(args[1]); return 2; } 285 SIGN = args[2][0] == '+' || args[2][0] == '-'; 286 if (!SIGN && is_strfmt_int(args[2])) result = i32int(args[2]); 287 else if (SIGN && is_strfmt_int(args[2] + 1)) result = i32int(args[2]); 288 else result = args[2]; 289 for (int i = 3; i < n_args; i++) { 290 SIGN = args[i][0] == '+' || args[i][0] == '-'; 291 if (!SIGN && is_strfmt_int(args[i])) OP2 = i32int(args[i]); 292 else if (SIGN && is_strfmt_int(args[i] + 1)) OP2 = i32int(args[i]); 293 else OP2 = args[i]; 294 result = div(result, OP2); 295 } 296 break; 297 case '%': 298 if (args[2] == NULL) { display_NOT_VALID_OPERATOR(args[1]); return 2; } 299 SIGN = args[2][0] == '+' || args[2][0] == '-'; 300 if (!SIGN && is_strfmt_int(args[2])) result = i32int(args[2]); 301 else if (SIGN && is_strfmt_int(args[2] + 1)) result = i32int(args[2]); 302 else result = args[2]; 303 for (int i = 3; i < n_args; i++) { 304 SIGN = args[i][0] == '+' || args[i][0] == '-'; 305 if (!SIGN && is_strfmt_int(args[i])) OP2 = i32int(args[i]); 306 else if (SIGN && is_strfmt_int(args[i] + 1)) OP2 = i32int(args[i]); 307 else OP2 = args[i]; 308 result = mod(result, OP2); 309 } 310 break; 311 case '^': 312 if (args[2] == NULL) { display_NOT_VALID_OPERATOR(args[1]); return 2; } 313 SIGN = args[2][0] == '+' || args[2][0] == '-'; 314 if (!SIGN && is_strfmt_int(args[2])) result = i32int(args[2]); 315 else if (SIGN && is_strfmt_int(args[2] + 1)) result = i32int(args[2]); 316 else result = args[2]; 317 for (int i = 3; i < n_args; i++) { 318 SIGN = args[i][0] == '+' || args[i][0] == '-'; 319 if (!SIGN && is_strfmt_int(args[i])) OP2 = i32int(args[i]); 320 else if (SIGN && is_strfmt_int(args[i] + 1)) OP2 = i32int(args[i]); 321 else OP2 = args[i]; 322 result = xy(result, OP2); 323 } 324 break; 325 case '!': 326 if (args[2] == NULL) { display_NOT_VALID_OPERATOR(args[1]); return 2; } 327 SIGN = args[2][0] == '+' || args[2][0] == '-'; 328 if (!SIGN && is_strfmt_int(args[2])) result = i32int(args[2]); 329 else if (SIGN && is_strfmt_int(args[2] + 1)) result = i32int(args[2]); 330 else result = args[2]; 331 result = fact(result.to_i32()); 332 break; 333 default: 334 display_NOT_VALID_OPERATOR(args[2]); 335 return 2; 336 } //SWITCH END 337 if (error()) return 2; //FAILURE 338 print(result, "\n"); //OUTPUT 339 Serial.flush(); 340 return 0; 341} 342 343void display_NOT_VALID_OPERATOR(char* args) { raise("n7 CALCULATOR: NOT VALID OPERATOR/OPERATION", args); } 344 345void display_help(void) { 346 print("-------------------------\n"); 347 print("n7 (Calculator-num7 version 1.0.0) helps you with arithmetic operations:\n\n"); 348 print(" addition: n7 + 2 3.5 REM5.5\n"); 349 print(" addition: n7 + 2 3.5 4.5 REM10.0\n"); 350 print(" subtraction: n7 - 2 3.5 REM-1.5\n"); 351 print(" subtraction: n7 - 2 3.5 4.5 REM-6.0\n"); 352 print(" multiplication: n7 x 2 3.5 4.5 REM31.5\n"); 353 print(" multiplication: n7 X 2 3.5 4.5 REM31.5\n"); 354 print(" multiplication: n7 * 2 3.5 4 REM28.0\n"); 355 print(" division: n7 / 120 2.0 5 REM12.0\n"); 356 print(" division: n7 / 2.5 3.5 REM0.7142857142857142857142857142857142857142\n"); 357 print(" division: n7 : 2.5 3.5 REM0.7142857142857142857142857142857142857142\n"); 358 print(" remainder: n7 % 17.0 10 4 REM3.0\n"); 359 print(" factorial: n7 ! 5 REM120.0\n\n"); 360 361 print(" absolute: n7 abs -5.0 REM5.0\n\n"); 362 363 print(" addition: n7 add 3.0 5.5 REM8.5\n"); 364 print(" subtraction: n7 sub 3.0 5.5 REM-2.5\n"); 365 print(" multiplication: n7 mul 3.0 5.5 REM16.5\n"); 366 print(" division: n7 div 3.0 5.5 6 REM0.545454\n"); 367 print(" remainder: n7 mod 10.0 7.0 REM3.0\n"); 368 print(" inverse: n7 inv 3.0 6 REM0.333333\n"); 369 print(" square root: n7 sqr 2.0 6 REM1.414213\n\n"); 370 371 print(" square: n7 x2 5.0 REM25.0\n"); 372 print(" cube: n7 x3 5.0 REM125.0\n"); 373 print(" power: n7 xy 10.5 3.0 REM1157.625\n"); 374 print(" power: n7 ^ 10.5 3 REM1157.625\n"); 375 print(" 10y: n7 10y 3.0 REM1000.0\n"); 376 print(" 2y: n7 2y 32.0 REM4294967296.0\n"); 377 print(" ey: n7 ey 1.0 REM2.7182818284590452353602874713527\n"); 378 print(" factorial: n7 fact 5 REM120.0\n\n"); 379 380 print(" 10x: n7 10x 5.25 REM52.5\n"); 381 print(" 100x: n7 100x 5.25 REM525.0\n"); 382 print(" 1000x: n7 1000x 5.25 REM5250.0\n"); 383 print(" 10div: n7 10div 5.25 REM0.525\n"); 384 print(" 100div: n7 100div 5.25 REM0.0525\n"); 385 print(" 1000div: n7 1000div 5.25 REM0.00525\n\n"); 386 print(" shift: n7 shift 5.25 3.0 REM5250.0\n"); 387 print(" shift: n7 shift 5.25 -3.0 REM0.00525\n\n"); 388 389 print(" percentage: n7 pct 3.725 150.0 REM5.5875\n"); 390 print(" perthousand: n7 pth 2.0 24_000.0 REM48.0\n"); 391 print(" spin-off: n7 spoff 22.0 1_299.0 REM1064.75409836065573770491803278688524\n"); 392 print(" spin-on: n7 rnd 1298.995 2 REM1299.0\n\n"); 393 394 print(" rnd: n7 rnd 3.141592654 4 REM3.1416\n"); 395 print(" rndb: n7 rndb 3.141592654 7 REM3.1415926\n"); 396 print(" rndc: n7 rndc 3.141592654 0 REM4.0\n"); 397 print(" rndf: n7 rndf 3.74 0 REM3.0\n\n"); 398 399 print(" sum: n7 sum 3.74 0.26 4 2.0 REM10.0\n"); 400 print(" min: n7 min 3.74 0.26 4 2.0 REM0.26\n"); 401 print(" max: n7 max 3.74 0.26 4 2.0 REM4.0\n"); 402 print(" mean: n7 mean 3.74 0.26 4 2.0 REM2.5\n\n"); 403 404 print(" e: n7 e REM2.7182818284590452353602874713527\n"); 405 print(" pi: n7 pi REM3.1415926535897932384626433832795\n"); 406 print(" exp: n7 exp 200.0e-5 REM200.0e-5\n"); 407 print(" scientific: n7 sci 200.0e-5 REM2.0e-3\n"); 408 print(" bits: n7 bits 10.0 REM1010\n"); 409 print(" bits: n7 bits 65535.0 0 REMFFFF\n\n"); 410 411 print(" gt: n7 gt -5.0 -4.0 REM0.0\n"); 412 print(" ge: n7 ge -6.0 -6.0 REM1.0\n"); 413 print(" lt: n7 lt -5.0 0.0 REM1.0\n"); 414 print(" le: n7 le -5.0 -5.0 REM1.0\n"); 415 print(" eq: n7 eq -5.0 0.0 REM0.0\n"); 416 print(" ne: n7 ne -5.0 0.0 REM1.0\n\n"); 417 418 print(" is_zero: n7 is_zero 0.0 REM1.0\n"); 419 print(" is_pos: n7 is_pos 1.0 REM1.0\n"); 420 print(" is_neg: n7 is_neg -1.0 REM1.0\n"); 421 print(" is_even: n7 is_even 3.0 REM0.0\n"); 422 print(" is_odd: n7 is_odd 2.0 REM0.0\n"); 423 print(" is_int: n7 is_int -2.1 REM0.0\n"); 424 print(" is_prime: n7 is_prime +3.0 REM1.0\n\n"); 425 426} 427 428const int BUFFER_SIZE = 1024; 429char inputBuffer[BUFFER_SIZE]; 430int bufferIndex = 0; 431 432#define MAX_TOKENS 1000 433static char* tokens[MAX_TOKENS]; 434String inputString = ""; // A String to hold incoming data 435bool stringComplete = false; // Whether the string is complete 436 437i32 n7_args(char data[]) { 438 439 // Get the first token (split by comma) 440 char* token = strtok(data, " "); 441 int tokenCount = 0; 442 443 // Loop through the string to get all tokens 444 while (token != NULL && tokenCount < MAX_TOKENS-1) { 445 tokens[tokenCount] = token; 446 tokenCount++; 447 token = strtok(NULL, " "); 448 } 449 return tokenCount; 450} 451 452void setup() { 453 Serial.begin(115200); 454 while (Serial.available()) Serial.read(); //Buffer cleared. Ready to receive. 455 Serial.println("Buffer cleared. Ready to receive."); 456} 457 458void loop() { 459 delay(1); //it waits for ...ms 460 461 while (Serial.available()) { 462 char incomingChar = Serial.read(); 463 // Check for end-of-line character 464 if (incomingChar == '\n') { 465 inputBuffer[bufferIndex] = '\0'; // Null-terminate the string 466 // Process the complete input line 467 error_clear(); //RESET n7 ERRORs 468 n7(n7_args(inputBuffer), tokens); 469 // Reset buffer 470 bufferIndex = 0; 471 } else { 472 // Add to buffer if not full 473 if (bufferIndex < BUFFER_SIZE - 1) { 474 inputBuffer[bufferIndex++] = incomingChar; 475 } else { 476 // Buffer overflow, reset 477 bufferIndex = 0; 478 Serial.println("Buffer overflow!"); 479 } 480 } 481 } 482}
Downloadable files
num7
C++ ARBITRARY PRECISION ARITHMETIC-LOGIC LIBRARY
https://docs.arduino.cc/libraries/num7/
n7 Arbitrary Precision Decimal Calculator sketch
c++ language
n7_ZERO.ino
n7 client for Arduino n7 calculator
n7 command line interface - DOS
Arduino-n7-calc.py
Comments
Only logged in users can leave comments