Log (or number of digits) of an UInt

Added by: ~Moberg, 2013 VI 05

Added by: ~Moberg, 2013 VI 05

Hi, how can I most easily see which of two UInts have the most amount of digits?

For normal ints I would use

(int) log_10(a) > (int) log_10(b)

Added by: tomek, 2013 VI 06

You can use the logarithm for UInt too. But in such a case you have to make use from Big class because UInt is not able to calculate the logarithm. Sample program:

#include "ttmath/ttmath.h"
#include <iostream>

using namespace ttmath;

int main()
UInt<2> a, b;
Big<1, 2> aa, bb;

a = "245345345";
b = "78934534";

aa.Log(a, 10);
bb.Log(b, 10);

// test the exception for the zero

if( aa > bb )
std::cout << "a has more decimal digits than b" << std::endl;
std::cout << "b has more (or equal to) decimal digits than a" << std::endl;