-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathopt2.cpp
More file actions
46 lines (39 loc) · 845 Bytes
/
opt2.cpp
File metadata and controls
46 lines (39 loc) · 845 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
41
42
43
44
45
46
#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);
}
optional<double> ias(double x)
{
auto y=sqr(x);
auto z=y?arcsin(y.get()):none;
auto w=z?inv(z.get()):none;
return w;
}
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";
}