Skip to content

IntIsZero

Description

Test whether the integer equals zero.

Parameters

Name Direction Description
value in Integer to inspect.

Success

Returns true when the integer represents zero.

Failure

Returns false for any non-zero value.

Usage example (Cross-references)

Usage examples (Cross-references)
    static void sint_normalize(SignedInt *value) {
        int_normalize(&value->magnitude);
        if (IntIsZero(&value->magnitude)) {
            value->negative = false;
        }
        SignedInt neg_b = sint_clone(b);
    
        if (!IntIsZero(&neg_b.magnitude)) {
            neg_b.negative = !neg_b.negative;
        }
        ValidateInt(value);
    
        if (IntIsZero(value)) {
            LOG_ERROR("log2 undefined for zero");
            return false;
    }
    
    bool IntIsZero(const Int *value) {
        return IntBitLength(value) == 0;
    }
        ValidateInt(value);
    
        return !IntIsZero(value) && IntBitLength(value) == IntTrailingZeroCount(value) + 1;
    }
        }
    
        if (IntIsZero(value)) {
            return StrPushBackR(out, '0');
        }
    
        current = IntClone(value);
        if (IntIsZero(&current)) {
            return false;
        }
        }
    
        while (!IntIsZero(&current)) {
            u64  rem  = 0;
            bool last = false;
            }
            rem  = IntToU64(&remainder);
            last = IntIsZero(&quotient);
    
            // A non-final chunk emits exactly chunk_digits digits (zero-padded for
        ValidateInt(b);
    
        if (IntIsZero(a) || IntIsZero(b)) {
            IntClear(result);
            return true;
            LOG_FATAL("quotient and remainder must be different objects");
        }
        if (IntIsZero(divisor)) {
            LOG_ERROR("Division by zero");
            return false;
        ValidateInt(divisor);
    
        if (IntIsZero(divisor)) {
            LOG_ERROR("Division by zero");
            return false;
            return false;
        }
        if (!IntIsZero(&remainder)) {
            IntDeinit(&quotient);
            IntDeinit(&remainder);
        }
    
        while (!IntIsZero(&y)) {
            Int r = IntInit(IntAllocator(result));
        ValidateInt(b);
    
        if (IntIsZero(a) || IntIsZero(b)) {
            Int zero = IntInit(IntAllocator(result));
            int_replace(result, &zero);
        }
    
        if (IntIsZero(value)) {
            Int zero_root = IntInit(IntAllocator(root));
            Int zero_rem  = IntInit(IntAllocator(remainder));
                Int next = IntInit(IntAllocator(root));
    
                if (IntEQ(&mid, &one) || IntIsZero(&mid)) {
                    IntDeinit(&high);
                    high = IntInit(IntAllocator(root));
            return false;
        }
        result = IntIsZero(&remainder);
    
        IntDeinit(&root);
        ValidateInt(value);
    
        if (IntIsZero(value) || IntBitLength(value) == 1) {
            return true;
        }
                return false;
            }
            exact = IntIsZero(&remainder);
    
            IntDeinit(&root);
        }
    
        if (IntIsZero(n) || IntIsEven(n)) {
            LOG_ERROR("n must be non-zero and odd");
            return false;
        }
    
        while (!IntIsZero(&aa)) {
            while (IntIsEven(&aa)) {
                u64 n_mod_8 = 0;
        ValidateInt(modulus);
    
        if (IntIsZero(modulus)) {
            LOG_ERROR("modulus is zero");
            return false;
        ValidateInt(modulus);
    
        if (IntIsZero(modulus)) {
            LOG_ERROR("modulus is zero");
            return false;
                return false;
            }
            if (IntIsZero(&diff)) {
                Int zero = IntInit(IntAllocator(result));
                int_replace(result, &zero);
        ValidateInt(modulus);
    
        if (IntIsZero(modulus)) {
            LOG_ERROR("modulus is zero");
            return false;
        ValidateInt(modulus);
    
        if (IntIsZero(modulus)) {
            LOG_ERROR("modulus is zero");
            return false;
        ValidateInt(modulus);
    
        if (IntIsZero(modulus)) {
            LOG_FATAL("modulus is zero");
        }
        ValidateInt(modulus);
    
        if (IntIsZero(modulus)) {
            LOG_ERROR("modulus is zero");
            return false;
        Int scratch = IntInit(IntAllocator(result));
    
        while (!IntIsZero(&exp)) {
            if (int_is_odd(&exp)) {
                if (!IntModMul(&scratch, &acc, &base_mod, modulus)) {
                return false;
            }
            if (!IntIsZero(&exp)) {
                if (!IntModMul(&scratch, &base_mod, &base_mod, modulus)) {
                    IntDeinit(&acc);
        ValidateInt(modulus);
    
        if (IntIsZero(modulus)) {
            LOG_ERROR("modulus is zero");
            return false;
        }
    
        while (!IntIsZero(&new_r)) {
            Int       q       = IntInit(IntAllocator(result));
            Int       rem     = IntInit(IntAllocator(result));
                return false;
            }
            if (t.negative && !IntIsZero(&mag_mod)) {
                if (!int_sub(&positive, modulus, &mag_mod)) {
                    IntDeinit(&positive);
        ValidateInt(modulus);
    
        if (IntIsZero(modulus)) {
            LOG_ERROR("modulus is zero");
            return false;
        }
    
        if (IntIsZero(&a)) {
            Int zero = IntInit(IntAllocator(result));
            int_replace(result, &zero);
        ValidateFloat(value);
    
        if (IntIsZero(&value->significand)) {
            value->negative = false;
            value->exponent = 0;
    
            if (int_try_from_u64(&ten, 10, alloc)) {
                while (int_div_mod(&q, &r, &value->significand, &ten) && IntIsZero(&r)) {
                    Int tmp            = value->significand;
                    value->significand = q;
    bool FloatIsZero(const Float *value) {
        ValidateFloat(value);
        return IntIsZero(&value->significand);
    }
        }
    
        result.negative = negative && !IntIsZero(&result.significand);
        result.exponent = explicit_exp - fractional;
    
        bool ok    = FloatToInt(&result, &value);
        bool right = ok && IntIsZero(&result);
    
        IntDeinit(&result);
        Int value = IntInit(&alloc.base);
    
        bool result = IntIsZero(&value);
        result      = result && (IntBitLength(&value) == 0);
        IntClear(&value);
    
        bool result = IntIsZero(&value);
        result      = result && (IntBitLength(&value) == 0);
        IntMul(&result_value, &a, &b);
    
        bool result = IntIsZero(&result_value);
        result      = result && (IntToU64(&result_value) == 0);
        bool ok = IntMul(&product, &a, &zero);
    
        bool result = ok && IntIsZero(&product);
        result      = result && (IntToU64(&product) == 0u);
        result      = result && (IntBitLength(&product) == 0u);
    
        bool result = IntLCM(&result_value, &a, &b);
        result      = result && IntIsZero(&result_value);
        result      = result && (IntCompare(&result_value, 0) == 0);
        bool result = ok;
        result      = result && (IntToU64(&quotient) == 123456789u);
        result      = result && IntIsZero(&remainder);
    
        IntDeinit(&dividend);
    
        bool result = ok;
        result      = result && IntIsZero(&quotient);
        result      = result && (IntToU64(&remainder) == 42u);
    
        bool ok = IntMul(&r, &x, &y);
        ok      = ok && IntIsZero(&r);
    
        IntDeinit(&x);
    
        bool ok = IntDivMod(&q, &r, &dvd, &dvs);
        ok      = ok && IntIsZero(&q) && IntToU64(&r) == 50u;
    
        IntDeinit(&dvd);
    
        bool ok = IntRootRem(&root, &rem, &v, 3); // 100^3 = 1e6
        ok      = ok && IntToU64(&root) == 100u && IntIsZero(&rem);
    
        IntDeinit(&v);
    
        bool ok = IntRootRem(&root, &rem, &v, 3); // cbrt(1000)=10 exact actually
        ok      = ok && IntToU64(&root) == 10u && IntIsZero(&rem);
    
        // and a genuinely inexact one
    
        bool ok = IntModSub(&r, &x, &y, &m);
        ok      = ok && IntIsZero(&r);
    
        IntDeinit(&x);
        Int r = IntFrom(9u, a);
    
        bool ok = IntModSqrt(&r, &v, &m) && IntIsZero(&r);
    
        IntDeinit(&v);
        // equal branch: 5 - 12 = 0 (mod 7)
        Int a3 = IntFrom(5u, alloc), b3 = IntFrom(12u, alloc);
        ok = ok && IntModSub(&r, &a3, &b3, &m) && IntIsZero(&r);
    
        IntDeinit(&a1);
    
        bool result = IntBitLength(&zero) == 0;
        result      = result && IntIsZero(&zero);
        result      = result && (IntToU64(&zero, &error) == 0);
        result      = result && !error;
        bool result = !IntTryFromBinary(&value, "10a1");
    
        result = result && IntIsZero(&parsed);
        result = result && IntIsZero(&value);
    
        result = result && IntIsZero(&parsed);
        result = result && IntIsZero(&value);
    
        IntDeinit(&parsed);
        bool result = !IntTryFromStr(&value, "12x3");
    
        result = result && IntIsZero(&parsed);
        result = result && IntIsZero(&value);
    
        result = result && IntIsZero(&parsed);
        result = result && IntIsZero(&value);
    
        IntDeinit(&parsed);
        bool result = !IntTryFromHexStr(&value, "12g3");
    
        result = result && IntIsZero(&parsed);
        result = result && IntIsZero(&value);
    
        result = result && IntIsZero(&parsed);
        result = result && IntIsZero(&value);
    
        IntDeinit(&parsed);
        bool result = !IntTryFromStrRadix(&value, "102", 2);
    
        result = result && IntIsZero(&parsed);
        result = result && IntIsZero(&value);
    
        result = result && IntIsZero(&parsed);
        result = result && IntIsZero(&value);
    
        IntDeinit(&parsed);
        bool result = !IntTryFromStrRadix(&value, "10", 1);
    
        result = result && IntIsZero(&parsed);
        result = result && IntIsZero(&value);
    
        result = result && IntIsZero(&parsed);
        result = result && IntIsZero(&value);
    
        IntDeinit(&parsed);
        bool result = !IntTryFromStrRadix(&value, "_", 10);
    
        result = result && IntIsZero(&value);
    
        IntDeinit(&value);
        bool result = ok_valid && (IntToU64(&valid) == 5);
        result      = result && rejected_radix;
        result      = result && IntIsZero(&invalid);
    
        IntDeinit(&valid);
        Int value = IntFromBytesLE(NULL, 0, ALLOCATOR_OF(&alloc));
    
        bool result = IntIsZero(&value);
    
        IntDeinit(&value);
    
        bool result = !ok;
        result      = result && IntIsZero(&value);
    
        StrDeinit(&hex);
    
    bool test_int_is_zero(void) {
        WriteFmt("Testing IntIsZero\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        Int non_zero = IntFrom(1, &alloc.base);
    
        bool result = IntIsZero(&zero);
        result      = result && !IntIsZero(&non_zero);
    
        bool result = IntIsZero(&zero);
        result      = result && !IntIsZero(&non_zero);
    
        IntDeinit(&zero);
Last updated on