online compiler and debugger for c/c++

code. compile. run. debug. share.
Source Code    Language
/* MIT License Copyright (c) 2019 awsdert Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include <limits.h> #include <inttypes.h> #include <float.h> #include <string.h> #include <stdio.h> typedef signed char schar; typedef unsigned char uchar; typedef unsigned long ulong; typedef signed long long sllong; typedef unsigned long long ullong; #define big long long typedef signed big bigi; typedef unsigned big ubigi; #define HALF_MAN_BIT 10 #define HALF_EXP_BIT 5 #define LDBL_MAN_BIT 52 #define LDBL_EXP_BIT 37 #define FLT_SCNa "%a" #define FLT_SCNf "%f" #define FLT_SCNe "%e" #define FLT_MAN_BIT 23 #define FLT_EXP_BIT 8 typedef union FLT_UNION { float fpn; uchar raw[sizeof(float)]; struct { ubigi man:FLT_MAN_BIT; ubigi exp:FLT_EXP_BIT; ubigi sig:1; }; } FLT_UNION; #define DBL_SCNa "%la" #define DBL_SCNf "%lf" #define DBL_SCNe "%le" #define DBL_MAN_BIT 52 #define DBL_EXP_BIT 11 typedef union DBL_UNION { double fpn; uchar raw[sizeof(double)]; struct { ubigi man:DBL_MAN_BIT; ubigi exp:DBL_EXP_BIT; ubigi sig:1; }; } DBL_UNION; #define FPN_PFX(VAL) FLT##_##VAL #define FPN_MAX FPN_PFX(_MAX) #define FPN_SCNa FPN_PFX(SCNa) #define FPN_SCNf FPN_PFX(SCNf) #define FPN_SCNe FPN_PFX(SCNe) #define FPN_MAN_BIT FPN_PFX(MAN_BIT) #define FPN_EXP_BIT FPN_PFX(EXP_BIT) #define FPN_MAX_EXP FPN_PFX(MAX_EXP) #define FPN_MAX_10_EXP FPN_PFX(MAX_10_EXP) typedef FPN_PFX(UNION) FPN_UNION; typedef struct FPN_RET { long pos; ulong exp; FPN_UNION val; } FPN_RET; int fpn_read (char *text); char *fpn_text[] = { "0", "1", "10", "16", "100", "101", "0.1", "0.01", "0.001", "0.101", "1.1", "1.01", "1.001", "1.101", "3.14", "1e+1", "1e+8", "1e+10", "1e+100", "3e+1", "1e-1", "1e-8", "1e-10", "1e-100", "3e-1", ".1e+1", ".1e+8", ".1e+10", ".1e+100", ".3e+1", ".1e-1", ".1e-8", ".1e-10", ".1e-100", ".3e-1", "1.1e+1", "1.1e+8", "1.1e+10", "1.1e+100", "3.1e+1", "1.1e-1", "1.1e-8", "1.1e-10", "1.1e-100", "3.1e-1", "3.14e+1", "3.14e+8", "3.14e+10", "3.14e+100", "3.14e-1", "3.14e-8", "3.14e-10", "3.14e-100", "-0", "-1", "-10", "-16", "-100", "-101", "-0.1", "-0.01", "-0.001", "-0.101", "-1.1", "-1.01", "-1.001", "-1.101", "-3.14", "-1e+1", "-1e+8", "-1e+10", "-1e+100", "-3e+1", "-1e-1", "-1e-8", "-1e-10", "-1e-100", "-3e-1", "-0.1e+1", "-0.1e+8", "-0.1e+10", "-0.1e+100", "-0.3e+1", "-0.1e-1", "-0.1e-8", "-0.1e-10", "-0.1e-100", "-0.3e-1", "-1.1e+1", "-1.1e+8", "-1.1e+10", "-1.1e+100", "-3.1e+1", "-1.1e-1", "-1.1e-8", "-1.1e-10", "-1.1e-100", "-3.1e-1", "-3.14e+1", "-3.14e+8", "-3.14e+10", "-3.14e+100", "-3.14e-1", "-3.14e-8", "-3.14e-10", "-3.14e-100", "0xA", "0xA0", "0xA6", "0xA00", "0xA0A", "0x0.A", "0x0.0A", "0x0.00A", "0x0.A0A", "0xA.A", "0xA.0A", "0xA.00A", "0xA.A0A", "0xC.A4", "0xAp+A", "0xAp+F", "0xAp+A0", "0xAp+A00", "0xCp+A", "0xAp-A", "0xAp-F", "0xAp-A0", "0xAp-A00", "0xCp-A", "0x0.Ap+A", "0x0.Ap+F", "0x0.Ap+A0", "0x0.Ap+A00", "0x0.Cp+A", "0x0.Ap-A", "0x0.Ap-F", "0x0.Ap-A0", "0x0.Ap-A00", "0x0.Cp-A", "0xA.Ap+A", "0xA.Ap+F", "0xA.Ap+A0", "0xA.Ap+A00", "0xC.Ap+A", "0xA.Ap-A", "0xA.Ap-F", "0xA.Ap-A0", "0xA.Ap-A00", "0xC.Ap-A", "0xC.A4p+A", "0xC.A4p+F", "0xC.A4p+A0", "0xC.A4p+A00", "0xC.A4p-A", "0xC.A4p-F", "0xC.A4p-A0", "0xC.A4p-A00", "-0xA", "-0xA0", "-0x10", "-0xA00", "-0xA0A", "-0x0.A", "-0x0.0A", "-0x0.00A", "-0x0.A0A", "-0xA.A", "-0xA.0A", "-0xA.00A", "-0xA.A0A", "-0xC.A4", "-0xAp+A", "-0xAp+F", "-0xAp+A0", "-0xAp+A00", "-0xCp+A", "-0xAp-A", "-0xAp-F", "-0xAp-A0", "-0xAp-A00", "-0xCp-A", "-0x0.Ap+A", "-0x0.Ap+F", "-0x0.Ap+A0", "-0x0.Ap+A00", "-0x0.Cp+A", "-0x0.Ap-A", "-0x0.Ap-F", "-0x0.Ap-A0", "-0x0.Ap-A00", "-0x0.Cp-A", "-0xA.Ap+A", "-0xA.Ap+F", "-0xA.Ap+A0", "-0xA.Ap+A00", "-0xC.Ap+A", "-0xA.Ap-A", "-0xA.Ap-F", "-0xA.Ap-A0", "-0xA.Ap-A00", "-0xC.Ap-A", "-0xC.A4p+A", "-0xC.A4p+F", "-0xC.A4p+A0", "-0xC.A4p+A00", "-0xC.A4p-A", "-0xC.A4p-F", "-0xC.A4p-A0", "-0xC.A4p-A00", NULL }; int main () { size_t i = 0, wrong = 0; while (fpn_text[i] && i < 210) wrong += fpn_read (fpn_text[i++]); printf( "Tried = %zu, ", i ); while (fpn_text[i]) ++i; printf( "Count = %zu, Wrong = %zu", i, wrong ); return 0; } void printb( char const *text, void const * addr, size_t const bits ) { size_t size = bits / CHAR_BIT, b = 0; uchar const *a = addr; uchar val, bit; if ( bits % CHAR_BIT ) ++size; printf("%s",text); while ( size-- ) { for ( val = a[size], bit = 1; bit; val <<= 1, bit <<= 1, ++b ) { //if ( b == bits ) return; putchar( '0' + !!(val & SCHAR_MIN) ); } if ( b >= bits ) return; } } FPN_RET fpn_make ( ulong base, ubigi num, ubigi fpn, long exp, long dig) { FPN_RET ret = {0}; long exp_bias = FPN_MAX_EXP, exp_dig_max = FPN_MAX_10_EXP, pos_max = FPN_MAN_BIT, pos = 0; ubigi one, NUM; if (dig < 1) return ret; if ( exp <= -exp_dig_max ) return ret; if ( exp >= exp_dig_max ) { ret.val.exp = ~0; return ret; } if ( num ) for ( NUM = num; NUM; NUM /= base, --dig ); else if ( fpn ) --dig; else return ret; /* Calculate one */ for (one = 1; one <= fpn; one *= base, --dig ); for (; dig > 0; one *= base, --dig ); /* Preserve exponent in case we need it later */ pos = exp; while ( pos > 0 ) { num *= base; fpn *= base; if ( fpn >= one ) { NUM = fpn / one; fpn -= NUM * one; num += NUM; } --pos; } while ( pos < 0 ) { NUM = num % base; fpn += NUM * one; one *= base; num /= base; ++pos; } /* Calculate normal exponent */ pos = 0; if ( num ) { for ( ; !(num % base); ret.val.exp++, num >>= 1 ); for ( NUM = num; NUM > 1; ++pos, NUM >>= 1 ); ret.exp = ret.val.exp; } else for ( NUM = fpn; NUM < one; --pos, NUM <<= 1 ); /* Set exponent and mantissa */ ret.val.man = num; ret.val.exp += exp_bias + pos - 1; ret.pos = pos; if ( pos > pos_max ) { pos_max = pos - pos_max; ++num; ret.val.man = num >> pos_max; } else { for ( ; pos < pos_max; ++pos) { fpn <<= 1; ret.val.man <<= 1; if (fpn >= one) { ret.val.man |= 1; fpn -= one; } } fpn *= 2; if ( fpn >= one ) ret.val.man++; } return ret; } int fpn_read (char *text) { static size_t i = 0; char *_text = text; ulong base = 10, c; long exp = 0, dig = 0; ubigi num = 0, fpn = 0; FPN_UNION test = { 0 }; FPN_RET ret = {0}; _Bool same = 0, neg = (*text == '-'); if ( neg || *text == '+' ) { ++text; } if ( *text =='0' ) { ++text; if ( *text == 'x' || *text == 'X' ) { base = 16; ++text; } } if ( base == 10 ) sscanf (_text, FPN_SCNf, &(test.fpn)); else sscanf (_text, FPN_SCNa, &(test.fpn)); while (*text == '0') ++text; for (; *text; ++text) { if ( *text >= '0' && *text <= '9' ) c = *text - '0'; else if ( *text >= 'A' && *text <= 'F' ) c = 10 + ( *text - 'A' ); else if ( *text >= 'a' && *text <= 'f' ) c = 10 + ( *text - 'a' ); else break; if ( c >= base ) break; num *= base; num += c; ++dig; } if (dig < 1) dig = 1; if (*text == '.') { for (++text; *text; ++text) { if ( *text >= '0' && *text <= '9' ) c = *text - '0'; else if ( *text >= 'A' && *text <= 'F' ) c = 10 + ( *text - 'A' ); else if ( *text >= 'a' && *text <= 'f' ) c = 10 + ( *text - 'a' ); else break; if ( c >= base ) break; fpn *= base; fpn += c; ++dig; } } if (*text == 'e' || *text == 'E') { if (*(++text) == '-') { for (++text; *text; ++text) { if ( *text >= '0' && *text <= '9' ) c = *text - '0'; else if ( *text >= 'A' && *text <= 'F' ) c = 10 + ( *text - 'A' ); else if ( *text >= 'a' && *text <= 'f' ) c = 10 + ( *text - 'f' ); else break; if ( c >= base ) break; exp *= base; exp -= c; } } else { for (++text; *text; ++text) { if ( *text >= '0' && *text <= '9' ) c = *text - '0'; else if ( *text >= 'A' && *text <= 'F' ) c = 10 + ( *text - 'A' ); else if ( *text >= 'a' && *text <= 'f' ) c = 10 + ( *text - 'f' ); else break; if ( c >= base ) break; exp *= base; exp += c; } } } ret = fpn_make (base, num, fpn, exp, dig); ret.val.sig = neg; same = ( test.man == ret.val.man && test.exp == ret.val.exp && test.sig == ret.val.sig ); if ( !same ) { printf("pos = %ld, exp = %lu\n", ret.pos, ret.exp ); num = test.man; printb( "gcc = ", &num, FPN_MAN_BIT ); putchar('\n'); num = ret.val.man; printb( "mcc = ", &num, FPN_MAN_BIT ); putchar('\n'); printf ("test.man = %013llX ", (ullong)(test.man) ); printf ("mine.man = %013llX ", (ullong)(ret.val.man)); printf ("exp t = %03X m = %03X\n", test.exp, ret.val.exp); printf ("fpn t = " FPN_SCNe " m = " FPN_SCNe " ", test.fpn, ret.val.fpn); printf ("value '%s'\n", _text); printf ("Index = %zu\n", i ); } ++i; return !same; }

Compiling Program...

Command line arguments:
Standard Input: Interactive Console Text
×

                

                

Program is not being debugged. Click "Debug" button to start program in debug mode.

#FunctionFile:Line
VariableValue
RegisterValue
ExpressionValue