Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F85750361
ch-08-ex-02-solution.cpp
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Tue, Oct 1, 21:08
Size
877 B
Mime Type
text/x-c++
Expires
Thu, Oct 3, 21:08 (2 d)
Engine
blob
Format
Raw Data
Handle
21261441
Attached To
R9360 PCSC_git
ch-08-ex-02-solution.cpp
View Options
/*
* chapter-08-exercise-02.cpp
*
* Homebrew abs function
*
* Created on: Nov 1, 2012
* Author: Radu Popescu <radu.popescu@epfl.ch>
*/
#include <iostream>
template <typename T> T absolute(const T x) { return (x >= 0) ? x : -1 * x; }
// Same thing, but computation is done at compile time. Not possible for double
// type
template <int x> struct absolute2 {
const static int val = ((x >= 0) ? x : -1 * x);
};
int main(int argc, char *argv[]) {
const int x1 = -2;
const int x2 = -3;
const double x3 = -7.39;
std::cout << "The absolute value of " << x1 << " is " << absolute(x1)
<< std::endl;
std::cout << "The absolute value of " << x2 << " is " << absolute2<x2>::val
<< " (computed at compile-time)" << std::endl;
std::cout << "The absolute value of " << x3 << " is " << absolute(x3)
<< std::endl;
return 0;
}
Event Timeline
Log In to Comment