Repository: TheAlgorithms/C-Plus-Plus
File: math/finding_number_of_digits_in_a_number.cpp (C++)

Code before:
        ++count;
    }

    return count;
}

/**
 * @brief This function finds the number of digits
 * in constant time
 * TC: O(1)
 * @param n the number to check its digits
 * @returns the digits count
 */
uint64_t count_digits(uint64_t n) {
    // to handle the negative numbers
    if (n < 0) {
        n = -n;
    }
    // log(0) is undefined
    else if (n == 0) {
        // handling the value 0
        return 0;
    }
    uint64_t count = floor(log10(n) + 1);

    return count;
}

/**
 * @brief Self-test implementations
 * @returns void
 */
static void test() {
    assert(finding_number_of_digits_in_a_number(5492) == 4);
    assert(finding_number_of_digits_in_a_number(-0) == 0);
    assert(finding_number_of_digits_in_a_number(10000) == 5);
    assert(finding_number_of_digits_in_a_number(9) == 1);
    assert(finding_number_of_digits_in_a_number(100000) == 6);
    assert(finding_number_of_digits_in_a_number(13) == 2);
    assert(finding_number_of_digits_in_a_number(564) == 3);
}

static void test_count_digits() {
    assert(count_digits(5492) == 4);
    assert(count_digits(-0) == 0);
    assert(count_digits(10000) == 5);
    assert(count_digits(9) == 1);
    assert(count_digits(100000) == 6);
    assert(count_digits(13) == 2);
    assert(count_digits(564) == 3);
}

Review comment (suggestion):
```suggestion
    return floor(log10(n) + 1); // NOLINT(cppcoreguidelines-narrowing-conversions)
```

Though I would say this is not a good practice, and I would rather you make the function return type `count_digits` to be `double` instead. But I leave that up to @Panquesito7 to decide 😄 

Code after:
        // each time the loop is running, `count` will be incremented by 1.
        ++count;
    }

    return count;
}

/**
 * @brief This function finds the number of digits
 * in constant time using logarithmic function
 * TC: O(1)
 * @param n the number to check its digits
 * @returns the digits count
 */
double finding_number_of_digits_in_a_number_using_log(double n) {
    // log(0) is undefined
    if (n == 0) {
        return 0;
    }

    // to handle the negative numbers
    if (n < 0) {
        n = -n;
    }

    double count = floor(log10(n) + 1);

    return count;
}

/**
 * @brief Self-test implementations
 * @returns void
 */
static void first_test() {
    assert(finding_number_of_digits_in_a_number(5492) == 4);
    assert(finding_number_of_digits_in_a_number(-0) == 0);
    assert(finding_number_of_digits_in_a_number(10000) == 5);
    assert(finding_number_of_digits_in_a_number(9) == 1);
    assert(finding_number_of_digits_in_a_number(100000) == 6);
    assert(finding_number_of_digits_in_a_number(13) == 2);
    assert(finding_number_of_digits_in_a_number(564) == 3);
}

static void second_test() {
    assert(finding_number_of_digits_in_a_number_using_log(5492) == 4);
    assert(finding_number_of_digits_in_a_number_using_log(-0) == 0);
    assert(finding_number_of_digits_in_a_number_using_log(10000) == 5);
    assert(finding_number_of_digits_in_a_number_using_log(9) == 1);
    assert(finding_number_of_digits_in_a_number_using_log(100000) == 6);
    assert(finding_number_of_digits_in_a_number_using_log(13) == 2);