Skip to content

IntFrom

IntFrom

Description

Convert a native integer into an arbitrary-precision integer. Dispatches on the type of value.

Aborts when a signed input is negative.

Parameters

Name Direction Description
value in Signed or unsigned integer source value

Usage example (from documentation)

  Int value = IntFrom(1234u);

Returns

Integer holding the same non-negative value.

Usage example (Cross-references)

Usage examples (Cross-references)
        Float a            = FloatFromStr("1.25");
        Float b            = FloatFromStr("0.75");
        Int   whole        = IntFrom(2);
        Float result_value = FloatInit();
        Str   text         = StrInit();
        Float a            = FloatFromStr("5.5");
        Float b            = FloatFromStr("0.5");
        Int   whole        = IntFrom(2);
        Float result_value = FloatInit();
        Str   text         = StrInit();
        Float a            = FloatFromStr("1.5");
        Float b            = FloatFromStr("2");
        Int   whole        = IntFrom(2);
        Float result_value = FloatInit();
        Str   text         = StrInit();
        Float a            = FloatFromStr("7.5");
        Float b            = FloatFromStr("2.5");
        Int   whole        = IntFrom(3);
        Float result_value = FloatInit();
        Str   text         = StrInit();
    
    bool test_int_from_unsigned_integer(void) {
        WriteFmt("Testing IntFrom with unsigned integer\n");
    
        Int value = IntFrom(13);
        WriteFmt("Testing IntFrom with unsigned integer\n");
    
        Int value = IntFrom(13);
        Str text  = IntToBinary(&value);
        WriteFmt("Testing Int uppercase radix conversion\n");
    
        Int value = IntFrom(0xBEEF);
        Str text  = IntToStrRadix(&value, 16, true);
    
        Int lhs = IntFromBinary("0001011");
        Int rhs = IntFrom(11);
    
        bool result = IntCompare(&lhs, &rhs) == 0;
        WriteFmt("Testing IntToU64 overflow handling\n");
    
        Int value = IntFrom(1);
        IntShiftLeft(&value, 64);
        IntToU64(&value);
        WriteFmt("Testing IntToStrRadix invalid radix handling\n");
    
        Int value = IntFrom(255);
    
        IntToStrRadix(&value, 37, false);
        WriteFmt("Testing IntToBytesLE NULL handling\n");
    
        Int value = IntFrom(1);
        IntToBytesLE(&value, NULL, 1);
        return false;
        WriteFmt("Testing IntToBytesBE zero max_len handling\n");
    
        Int value = IntFrom(1);
        u8  byte  = 0;
        WriteFmt("Testing IntCompare\n");
    
        Int a = IntFrom(41);
        Int b = IntFrom(42);
        Int c = IntFromBinary("000101010");
    
        Int a = IntFrom(41);
        Int b = IntFrom(42);
        Int c = IntFromBinary("000101010");
        WriteFmt("Testing Int compare wrappers\n");
    
        Int a = IntFrom(41);
        Int b = IntFrom(42);
        Int c = IntFromBinary("000101010");
    
        Int a = IntFrom(41);
        Int b = IntFrom(42);
        Int c = IntFromBinary("000101010");
        WriteFmt("Testing IntCompare generic dispatch\n");
    
        Int value = IntFrom(42);
        Int same  = IntFromBinary("00101010");
        Int big   = IntFrom(1);
        Int value = IntFrom(42);
        Int same  = IntFromBinary("00101010");
        Int big   = IntFrom(1);
    
        IntShiftLeft(&big, 80);
        Float value = FloatFromStr("12.5");
        Float same  = FloatFromStr("12.5");
        Int   whole = IntFrom(12);
        Int   next  = IntFrom(13);
        Float same  = FloatFromStr("12.5");
        Int   whole = IntFrom(12);
        Int   next  = IntFrom(13);
    
        bool result = (FloatCompare(&value, same) == 0);
        Int hex_val = IntFromHexStr("deadbeefcafebabe1234");
        Int bin_val = IntFromBinary("10100011");
        Int oct_val = IntFrom(493);
    
        StrWriteFmt(&output, "{}", big_dec);
    
        Int zero     = IntInit();
        Int non_zero = IntFrom(1);
    
        bool result = IntIsZero(&zero);
        WriteFmt("Testing IntIsOne\n");
    
        Int one = IntFrom(1);
        Int two = IntFrom(2);
    
        Int one = IntFrom(1);
        Int two = IntFrom(2);
    
        bool result = IntIsOne(&one);
        WriteFmt("Testing Int parity helpers\n");
    
        Int even = IntFrom(42);
        Int odd  = IntFrom(43);
    
        Int even = IntFrom(42);
        Int odd  = IntFrom(43);
    
        bool result = IntIsEven(&even);
        WriteFmt("Testing IntFitsU64\n");
    
        Int small = IntFrom(UINT64_MAX);
        Int big   = IntFrom(1);
    
        Int small = IntFrom(UINT64_MAX);
        Int big   = IntFrom(1);
    
        IntShiftLeft(&big, 64);
        WriteFmt("Testing IntLog2\n");
    
        Int value = IntFrom(1025);
    
        bool result = IntLog2(&value) == 10;
        WriteFmt("Testing IntIsPowerOfTwo\n");
    
        Int one   = IntFrom(1);
        Int power = IntFrom(1);
        Int other = IntFrom(24);
    
        Int one   = IntFrom(1);
        Int power = IntFrom(1);
        Int other = IntFrom(24);
        Int zero  = IntInit();
        Int one   = IntFrom(1);
        Int power = IntFrom(1);
        Int other = IntFrom(24);
        Int zero  = IntInit();
    
        Float value        = FloatFromStr("123.45");
        Int   result_value = IntFrom(99);
    
        bool result = !FloatToInt(&result_value, &value);
    
        Float value        = FloatFromStr("-42");
        Int   result_value = IntFrom(99);
    
        bool result = !FloatToInt(&result_value, &value);
        WriteFmt("Testing IntShiftLeft\n");
    
        Int value = IntFrom(3);
    
        IntShiftLeft(&value, 4);
        WriteFmt("Testing IntAdd\n");
    
        Int a      = IntFrom(255);
        Int b      = IntFrom(1);
        Int result_value = IntInit();
    
        Int a      = IntFrom(255);
        Int b      = IntFrom(1);
        Int result_value = IntInit();
        Str text   = StrInit();
        WriteFmt("Testing IntAdd generic dispatch\n");
    
        Int base         = IntFrom(40);
        Int rhs          = IntFrom(2);
        Int result_value = IntInit();
    
        Int base         = IntFrom(40);
        Int rhs          = IntFrom(2);
        Int result_value = IntInit();
        Int huge         = IntFromStr("123456789012345678901234567890");
        WriteFmt("Testing IntSub\n");
    
        Int a      = IntFrom(256);
        Int b      = IntFrom(1);
        Int result_value = IntInit();
    
        Int a      = IntFrom(256);
        Int b      = IntFrom(1);
        Int result_value = IntInit();
        WriteFmt("Testing IntSub generic dispatch\n");
    
        Int base         = IntFrom(40);
        Int rhs          = IntFrom(2);
        Int result_value = IntInit();
    
        Int base         = IntFrom(40);
        Int rhs          = IntFrom(2);
        Int result_value = IntInit();
        Int preserved    = IntFrom(99);
        Int rhs          = IntFrom(2);
        Int result_value = IntInit();
        Int preserved    = IntFrom(99);
        Int huge         = IntFromStr("12345678901234567890");
        Str text         = StrInit();
        WriteFmt("Testing IntSub underflow handling\n");
    
        Int a      = IntFrom(3);
        Int b      = IntFrom(5);
        Int result_value = IntFrom(99);
    
        Int a      = IntFrom(3);
        Int b      = IntFrom(5);
        Int result_value = IntFrom(99);
        Int a      = IntFrom(3);
        Int b      = IntFrom(5);
        Int result_value = IntFrom(99);
    
        bool result = !IntSub(&result_value, &a, &b);
        WriteFmt("Testing IntMul\n");
    
        Int a      = IntFrom(21);
        Int b      = IntFrom(6);
        Int result_value = IntInit();
    
        Int a      = IntFrom(21);
        Int b      = IntFrom(6);
        Int result_value = IntInit();
        WriteFmt("Testing IntMul with zero\n");
    
        Int a      = IntFrom(0);
        Int b      = IntFrom(12345);
        Int result_value = IntInit();
    
        Int a      = IntFrom(0);
        Int b      = IntFrom(12345);
        Int result_value = IntInit();
        WriteFmt("Testing IntSquare\n");
    
        Int value = IntFrom(12345);
        Int result_value = IntInit();
        WriteFmt("Testing IntPow generic dispatch\n");
    
        Int base = IntFrom(7);
        Int exponent = IntFrom(20);
        Int result_value = IntInit();
    
        Int base = IntFrom(7);
        Int exponent = IntFrom(20);
        Int result_value = IntInit();
        Str text = StrInit();
        WriteFmt("Testing IntDiv generic dispatch\n");
    
        Int dividend = IntFrom(126);
        Int result_value = IntInit();
        WriteFmt("Testing IntDivExact failure handling\n");
    
        Int dividend = IntFrom(10);
        Int divisor = IntFrom(3);
        Int result_value = IntFrom(99);
    
        Int dividend = IntFrom(10);
        Int divisor = IntFrom(3);
        Int result_value = IntFrom(99);
        Int dividend = IntFrom(10);
        Int divisor = IntFrom(3);
        Int result_value = IntFrom(99);
    
        bool result = !IntDivExact(&result_value, &dividend, &divisor);
        WriteFmt("Testing IntMod generic dispatch\n");
    
        Int dividend = IntFrom(126);
        Int result_value = IntInit();
        WriteFmt("Testing IntGCD\n");
    
        Int a = IntFrom(48);
        Int b = IntFrom(18);
        Int result_value = IntInit();
    
        Int a = IntFrom(48);
        Int b = IntFrom(18);
        Int result_value = IntInit();
        WriteFmt("Testing IntLCM\n");
    
        Int a = IntFrom(21);
        Int b = IntFrom(6);
        Int result_value = IntInit();
    
        Int a = IntFrom(21);
        Int b = IntFrom(6);
        Int result_value = IntInit();
        WriteFmt("Testing IntRoot\n");
    
        Int value = IntFrom(4096);
        Int result_value = IntInit();
        WriteFmt("Testing IntRootRem\n");
    
        Int value = IntFrom(200);
        Int root = IntInit();
        Int remainder = IntInit();
        WriteFmt("Testing IntSqrt\n");
    
        Int value = IntFrom(200);
        Int result_value = IntInit();
        WriteFmt("Testing IntSqrtRem\n");
    
        Int value = IntFrom(200);
        Int root = IntInit();
        Int remainder = IntInit();
        WriteFmt("Testing IntIsPerfectSquare\n");
    
        Int square = IntFrom(144);
        Int non_square = IntFrom(145);
    
        Int square = IntFrom(144);
        Int non_square = IntFrom(145);
    
        bool result = IntIsPerfectSquare(&square);
        WriteFmt("Testing IntIsPerfectPower\n");
    
        Int power = IntFrom(81);
        Int non_power = IntFrom(82);
        Int one = IntFrom(1);
    
        Int power = IntFrom(81);
        Int non_power = IntFrom(82);
        Int one = IntFrom(1);
        Int power = IntFrom(81);
        Int non_power = IntFrom(82);
        Int one = IntFrom(1);
    
        bool result = IntIsPerfectPower(&power);
        WriteFmt("Testing IntJacobi\n");
    
        Int a = IntFrom(5);
        Int p = IntFrom(7);
        Int b = IntFrom(9);
    
        Int a = IntFrom(5);
        Int p = IntFrom(7);
        Int b = IntFrom(9);
        Int n = IntFrom(21);
        Int a = IntFrom(5);
        Int p = IntFrom(7);
        Int b = IntFrom(9);
        Int n = IntFrom(21);
        Int p = IntFrom(7);
        Int b = IntFrom(9);
        Int n = IntFrom(21);
    
        bool result = IntJacobi(&a, &p) == -1;
        WriteFmt("Testing IntSquareMod\n");
    
        Int value = IntFrom(12345);
        Int mod = IntFrom(97);
        Int result_value = IntInit();
    
        Int value = IntFrom(12345);
        Int mod = IntFrom(97);
        Int result_value = IntInit();
        WriteFmt("Testing IntModAdd\n");
    
        Int a = IntFrom(100);
        Int b = IntFrom(250);
        Int m = IntFrom(13);
    
        Int a = IntFrom(100);
        Int b = IntFrom(250);
        Int m = IntFrom(13);
        Int result_value = IntInit();
        Int a = IntFrom(100);
        Int b = IntFrom(250);
        Int m = IntFrom(13);
        Int result_value = IntInit();
        WriteFmt("Testing IntModSub\n");
    
        Int a = IntFrom(5);
        Int b = IntFrom(9);
        Int m = IntFrom(13);
    
        Int a = IntFrom(5);
        Int b = IntFrom(9);
        Int m = IntFrom(13);
        Int result_value = IntInit();
        Int a = IntFrom(5);
        Int b = IntFrom(9);
        Int m = IntFrom(13);
        Int result_value = IntInit();
        WriteFmt("Testing IntModMul\n");
    
        Int a = IntFrom(123);
        Int b = IntFrom(456);
        Int m = IntFrom(97);
    
        Int a = IntFrom(123);
        Int b = IntFrom(456);
        Int m = IntFrom(97);
        Int result_value = IntInit();
        Int a = IntFrom(123);
        Int b = IntFrom(456);
        Int m = IntFrom(97);
        Int result_value = IntInit();
        WriteFmt("Testing IntModDiv\n");
    
        Int a = IntFrom(10);
        Int b = IntFrom(3);
        Int m = IntFrom(13);
    
        Int a = IntFrom(10);
        Int b = IntFrom(3);
        Int m = IntFrom(13);
        Int result_value = IntInit();
        Int a = IntFrom(10);
        Int b = IntFrom(3);
        Int m = IntFrom(13);
        Int result_value = IntInit();
        Int check = IntInit();
        WriteFmt("Testing IntPowMod scalar-exponent dispatch\n");
    
        Int base = IntFrom(7);
        Int mod = IntFrom(13);
        Int result_value = IntInit();
    
        Int base = IntFrom(7);
        Int mod = IntFrom(13);
        Int result_value = IntInit();
        WriteFmt("Testing IntPowMod Int-exponent dispatch\n");
    
        Int base = IntFrom(4);
        Int exp = IntFrom(13);
        Int mod = IntFrom(497);
    
        Int base = IntFrom(4);
        Int exp = IntFrom(13);
        Int mod = IntFrom(497);
        Int result_value = IntInit();
        Int base = IntFrom(4);
        Int exp = IntFrom(13);
        Int mod = IntFrom(497);
        Int result_value = IntInit();
        WriteFmt("Testing IntModInv\n");
    
        Int value = IntFrom(3);
        Int mod = IntFrom(11);
        Int result_value = IntInit();
    
        Int value = IntFrom(3);
        Int mod = IntFrom(11);
        Int result_value = IntInit();
        Int check = IntInit();
        WriteFmt("Testing IntModSqrt\n");
    
        Int value = IntFrom(10);
        Int mod = IntFrom(13);
        Int root = IntInit();
    
        Int value = IntFrom(10);
        Int mod = IntFrom(13);
        Int root = IntInit();
        Int check = IntInit();
        WriteFmt("Testing IntModSqrt no-solution case\n");
    
        Int value = IntFrom(3);
        Int mod = IntFrom(7);
        Int root = IntFrom(99);
    
        Int value = IntFrom(3);
        Int mod = IntFrom(7);
        Int root = IntFrom(99);
        Int value = IntFrom(3);
        Int mod = IntFrom(7);
        Int root = IntFrom(99);
    
        bool result = !IntModSqrt(&root, &value, &mod);
    
        Int prime = IntFromStr("1000000007");
        Int composite = IntFrom(561);
    
        bool result = IntIsProbablePrime(&prime);
        WriteFmt("Testing IntModInv no-solution case\n");
    
        Int value = IntFrom(6);
        Int mod = IntFrom(15);
        Int result_value = IntFrom(99);
    
        Int value = IntFrom(6);
        Int mod = IntFrom(15);
        Int result_value = IntFrom(99);
        Int value = IntFrom(6);
        Int mod = IntFrom(15);
        Int result_value = IntFrom(99);
    
        bool result = !IntModInv(&result_value, &value, &mod);
        WriteFmt("Testing IntModDiv no-solution case\n");
    
        Int a = IntFrom(1);
        Int b = IntFrom(6);
        Int m = IntFrom(15);
    
        Int a = IntFrom(1);
        Int b = IntFrom(6);
        Int m = IntFrom(15);
        Int result_value = IntFrom(99);
        Int a = IntFrom(1);
        Int b = IntFrom(6);
        Int m = IntFrom(15);
        Int result_value = IntFrom(99);
        Int b = IntFrom(6);
        Int m = IntFrom(15);
        Int result_value = IntFrom(99);
    
        bool result = !IntModDiv(&result_value, &a, &b, &m);
        WriteFmt("Testing IntAdd NULL result handling\n");
    
        Int a = IntFrom(1);
        Int b = IntFrom(2);
    
        Int a = IntFrom(1);
        Int b = IntFrom(2);
    
        IntAdd(NULL, &a, &b);
        WriteFmt("Testing Int division by zero handling\n");
    
        Int dividend  = IntFrom(1);
        Int divisor   = IntInit();
        Int quotient  = IntInit();
        WriteFmt("Testing IntRoot zero-degree handling\n");
    
        Int value = IntFrom(16);
        Int root = IntInit();
        Int remainder = IntInit();
        WriteFmt("Testing IntDiv scalar zero-divisor handling\n");
    
        Int dividend = IntFrom(10);
        Int quotient = IntInit();
        WriteFmt("Testing IntMod scalar zero-modulus handling\n");
    
        Int value = IntFrom(10);
        Int result_value = IntInit();
        WriteFmt("Testing IntModDiv zero modulus handling\n");
    
        Int a = IntFrom(10);
        Int b = IntFrom(3);
        Int m = IntInit();
    
        Int a = IntFrom(10);
        Int b = IntFrom(3);
        Int m = IntInit();
        Int result_value = IntInit();
        WriteFmt("Testing IntJacobi even denominator handling\n");
    
        Int a = IntFrom(3);
        Int n = IntFrom(8);
    
        Int a = IntFrom(3);
        Int n = IntFrom(8);
    
        (void)IntJacobi(&a, &n);
        WriteFmt("Testing IntPowMod scalar-exponent zero modulus handling\n");
    
        Int base = IntFrom(2);
        Int mod = IntInit();
        Int result_value = IntInit();
        WriteFmt("Testing IntPowMod Int-exponent zero modulus handling\n");
    
        Int base = IntFrom(2);
        Int exp = IntFrom(8);
        Int mod = IntInit();
    
        Int base = IntFrom(2);
        Int exp = IntFrom(8);
        Int mod = IntInit();
        Int result_value = IntInit();
Last updated on