Constexpr on a function [duplicate] - c++

This question already has answers here:
When does a constexpr function get evaluated at compile time?
(2 answers)
constexpr function not calculate value in compile time
(4 answers)
Closed 3 years ago.
constexpr unsigned long long factorial(unsigned long long n)
{
return n > 1 ? n * factorial(n - 1) : 1;
}
I have just learnt that if I call a constexpr function using a value known at compile time, the compiler will just replace that function with his final result.
So why do I get a crash at runtime after compiling this call to my function?
std::cout << factorial(10000);
If it was evaluated at compile time, I would get an error just builing my project and it wouldn't even start...
What's wrong?

Related

Why can integer type int64_t not hold this legal value? [duplicate]

This question already has answers here:
Why does the most negative int value cause an error about ambiguous function overloads?
(3 answers)
Closed 3 years ago.
I'm trying to write a test case for some corner case. For input of type int64_t, the following line won't compile:
int64_t a = -9223372036854775808LL;
The error/warning is:
error: integer constant is so large that it is unsigned [-Werror]
I thought the number was out of range, so I tried:
std::cout << std::numeric_limits<int64_t>::min() << std::endl;
It outputs exactly the same number!!! So the constant is within the range.
How can I fix this error?
You may write
int64_t a = -1 - 9223372036854775807LL;
The problem is that the - is not part of the literal, it is unary minus. So the compiler first sees 9223372036854775808LL (out of range for signed int64_t) and then finds the negative of this.
By applying binary minus, we can use two literals which are each in range.
Ben's already explained the reason, here's two other possible solutions.
Try this
int64_t a = INT64_MIN;
or this
int64_t a = std::numeric_limits<int64_t>::min();

value of parameter cannot be used as a constant c++ [duplicate]

This question already has answers here:
Does C++ support Variable Length Arrays?
(4 answers)
Why does a C/C++ compiler need know the size of an array at compile time?
(6 answers)
Closed 5 years ago.
void merge(vector<Flight>& data, int low, int high, int mid, string criteria)
{
int i, j, k, temp[high - low + 1];
...
The error that comes up is "the value of parameter "high" (declared at line 100) cannot be used as a constant". I haven't managed to find an appropriate answer to this question online.
high - low + 1 needs to be a compile time evaluable constant expression in C++. (C++ does not support variable length arrays.)
And that isn't, so the compiler issues a diagnostic.
The simple solution is to use a std::vector<int> as the type for temp.

shortest way to check if a positive integer is a power of two [duplicate]

This question already has answers here:
How to check if a number is a power of 2
(32 answers)
Closed 7 years ago.
This question is from job interview...Use this template to write a C++ function that checks if a positive integer is a power of two.
bool p(int n)
{
return ********;
}
You have to replace the 8 '*' symbols with other symbols in order to make the function work correctly.
My best approach was this:
bool p(int n)
{
return !(n&=n-1);
}
Unfortunately it was wrong, because there are 9 symbols here...
Any ideas?
Why assign to n? Just remove = and you have one character less. It will create a temporary instead, the logic won't change.

C++ Big Int algorithm [duplicate]

This question already has answers here:
Handle arbitrary length integers in C++
(3 answers)
Closed 7 years ago.
Hi I am doing an algorithm question require get the full result of
5208334^2, which is 27126743055556
I was able to do it with by represent integer using Charracter array. However can we have any better way (shorter or faster) to do that? any idea is welcome ?
Updated:
For my case, both long long and int64 work, just that I did not cast value before return:
int val (int n1, n2) {
........
return (long long) n1 * n2;
}
This number fits into long long(present in GCC and after c++11) type or int64(for some other compilers before c++11). Thus the simplest solution is to use this type.

C++ pow function get a weird result [duplicate]

This question already has answers here:
Why pow(10,5) = 9,999 in C++
(8 answers)
Closed 8 years ago.
Using some version of minGW, the following code will print 99.
int high;
high = pow(10,2);
std::cout<<high<<std::endl;
The parameter of pow function is double, but why i get 99? Someone who can tell me the process hidden inside pow function ?
Converting a double to an integer truncates the fractional part. pow(10,2) produces a slightly inaccurate result; if it's slightly high, you'll get 100 and if it is slightly low you'll get 99.
Moral: if you mean i*i, write i*i.