Commit ececfacb authored by GILLES Sebastien's avatar GILLES Sebastien
Browse files

Exercice 37: write the solution which puts declarations and definitions together.

parent 0e4df27f
......@@ -174,20 +174,41 @@ private:
};
//! Returns `number` * (2 ^ `exponent`)
int times_power_of_2(int number, int exponent);
int times_power_of_2(int number, int exponent)
{
while (exponent > 0)
{
number *= 2;
exponent -= 1;
}
while (exponent < 0)
{
number /= 2;
exponent += 1 ;
}
return number;
}
int round_as_int(double x)
{
return static_cast<int>(std::round(x));
}
//! Round to `x` the nearest integer.
int round_as_int(double x);
int max_int(int Nbits)
{
return (times_power_of_2(1, Nbits) - 1);
}
//! Maximum integer that might be represented with `Nbits` bits.
int max_int(int Nbits);
[[noreturn]] void error(std::string explanation)
{
std::cout << "ERROR: " << explanation << std::endl;
exit(EXIT_FAILURE);
}
//! Function to call when an error occurred; its aborts the program.
[[noreturn]] void error(std::string explanation);
//! Class which stores several `TestDisplay` as pointers.
......@@ -252,41 +273,6 @@ int main(int argc, char** argv)
/************************************/
int times_power_of_2(int number, int exponent)
{
while (exponent > 0)
{
number *= 2;
exponent -= 1;
}
while (exponent < 0)
{
number /= 2;
exponent += 1 ;
}
return number;
}
int round_as_int(double x)
{
return static_cast<int>(std::round(x));
}
int max_int(int Nbits)
{
return (times_power_of_2(1, Nbits) - 1);
}
[[noreturn]] void error(std::string explanation)
{
std::cout << "ERROR: " << explanation << std::endl;
exit(EXIT_FAILURE);
}
/*==================================*/
// PowerOfTwoApprox
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment