Mentions légales du service

Skip to content
Snippets Groups Projects
Commit 041409a1 authored by Fabien Vergnet's avatar Fabien Vergnet
Browse files

add exercice2

parent da57a5d9
No related branches found
No related tags found
No related merge requests found
#include <iostream>
#include <cmath> // for std::round
//! Returns `number` * (2 ^ `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;
}
//! Round to `x` the nearest integer.
int round_as_int(double x)
{
return static_cast<int>(std::round(x));
}
/************************************/
// Main function
/************************************/
int main(int argc, char** argv)
{
static_cast<void>(argc); // to silence warning about unused argc - don't bother
static_cast<void>(argv); // to silence warning about unused argv - don't bother
int numerator1 = round_as_int(0.65 * times_power_of_2(1, 1));
std::cout << "0.65 ~ " << numerator1 << " / 2^1" << std::endl ;
int numerator2 = round_as_int(0.65 * times_power_of_2(1, 2));
std::cout << "0.65 ~ " << numerator2 << " / 2^2" << std::endl ;
int numerator3 = round_as_int(0.65 * times_power_of_2(1, 3));
std::cout << "0.65 ~ " << numerator3 << " / 2^3" << std::endl ;
std::cout << std::endl ;
return EXIT_SUCCESS;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment