-
Notifications
You must be signed in to change notification settings - Fork 10
/
opt3.cpp
49 lines (41 loc) · 901 Bytes
/
opt3.cpp
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
41
42
43
44
45
46
47
48
49
#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);
}
template<typename F>
optional<double> call(const optional<double>& x, F f)
{
return x?f(x.get()):none;
}
optional<double> ias(double x)
{
return call(call(sqr(x),arcsin),inv);
}
int main()
{
std::cout<<"ias(1.0)="<<ias(1.0)<<"\n";
std::cout<<"ias(-1.0)="<<ias(-1.0)<<"\n";
std::cout<<"ias(2.0)="<<ias(2.0)<<"\n";
std::cout<<"ias(0.0)="<<ias(0.0)<<"\n";
}