-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathopt1.cpp
More file actions
40 lines (34 loc) · 726 Bytes
/
opt1.cpp
File metadata and controls
40 lines (34 loc) · 726 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <boost/optional.hpp>
#include <iostream>
#include <cmath>
template<typename T>
std::ostream& operator<<(std::ostream& os,const boost::optional<T>& x)
{
if(x)return os<<x.get();
else return os<<"none";
}
using namespace boost;
optional<double> inv(double x)
{
if(x==0.0)return none;
else return 1.0/x;
}
optional<double> sqr(double x)
{
if(x<0.0)return none;
else return std::sqrt(x);
}
optional<double> arcsin(double x)
{
if(x<-1.0||x>1.0)return none;
else return std::asin(x);
}
int main()
{
std::cout<<inv(5.0)<<"\n";
std::cout<<inv(0)<<"\n";
std::cout<<sqr(4.0)<<"\n";
std::cout<<sqr(-1.0)<<"\n";
std::cout<<arcsin(0.5)<<"\n";
std::cout<<arcsin(1.5)<<"\n";
}