Mentions légales du service
Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
GettingStartedWithModernCpp
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package Registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Admin message
GitLab upgrade completed. Current version is 17.8.2.
Show more breadcrumbs
STEFF Laurent
GettingStartedWithModernCpp
Commits
ececfacb
Commit
ececfacb
authored
5 years ago
by
GILLES Sebastien
Browse files
Options
Downloads
Patches
Plain Diff
Exercice 37: write the solution which puts declarations and definitions together.
parent
0e4df27f
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
TP/4-Templates/Solution/exercice37.cpp
+29
-43
29 additions, 43 deletions
TP/4-Templates/Solution/exercice37.cpp
with
29 additions
and
43 deletions
TP/4-Templates/Solution/exercice37.cpp
+
29
−
43
View file @
ececfacb
...
...
@@ -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
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment