abstractness
abstractness

Reputation: 11

Exceeding largest datatype allowed on chipset. C++

I have some math that is exceeding the c++ long data type. What would be the best way to get around this? I am doing basic math +,/,and* on an embedded chipset. I am adding up numbers in a loop and dividing the large number afterwards.

Upvotes: 1

Views: 71

Answers (1)

nakiya
nakiya

Reputation: 14403

Usually, a long (identical to long int) is at least 4 bytes in size. But, check for yourself. If you find that long long int has more bytes than a long int, and if your values do not exceed the maximum value storable in a long long int, use that. use #include <limits> and use functions inside this header to determine the maximum values storable in integer types.

For example,

#include <limits>
#include <iostream>

int main()
{
    std::cout << std::numeric_limits<int>::max() << std::endl;
    std::cout << std::numeric_limits<long int>::max() << std::endl;
    std::cout << std::numeric_limits<long long int>::max() << std::endl;
}

This outputs the following on my machine:

2147483647
9223372036854775807
9223372036854775807

If, however, your values exceed what can be stored in a long long int also, use a big number library.

Upvotes: 5

Related Questions