Skip to content

IntRoot

Description

Compute the integer degree-th root of a value (floor).

Parameters

Name Direction Description
result out Destination for the root
value in Input value
degree in Root degree

Usage example (from documentation)

  IntRoot(&root, &value, 3);

Success

Returns true. *result holds floor(value^(1/degree)).

Failure

Returns false on degree == 0 or allocator OOM. *result is left untouched.

Usage example (Cross-references)

Usage examples (Cross-references)
    }
    
    bool IntRoot(Int *result, const Int *value, u64 degree) {
        Int root      = IntInit(IntAllocator(result));
        Int remainder = IntInit(IntAllocator(result));
    
    bool IntSqrt(Int *result, const Int *value) {
        return IntRoot(result, value, 2);
    }
    
    bool test_int_root(void) {
        WriteFmt("Testing IntRoot\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        Int result_value = IntInit(&alloc.base);
    
        IntRoot(&result_value, &value, 4);
    
        bool result = IntToU64(&result_value) == 8;
    
    bool test_int_root_zero_degree(void) {
        WriteFmt("Testing IntRoot zero-degree handling\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
    ///
    bool test_m26_root_value(void) {
        WriteFmt("Testing IntRoot success contract\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        Int result_value = IntInit(&alloc.base);
    
        bool ok = IntRoot(&result_value, &value, 4);
    
        bool result = ok;
        Int r = IntFrom(7u, a);
    
        bool ok = IntRoot(&r, &v, 3); // frees remainder on success (1877)
        ok      = ok && IntToU64(&r) == 100u;
Last updated on