-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInt8.cpp
126 lines (108 loc) · 3.38 KB
/
Int8.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Int8.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ckatz <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/07/06 12:08:52 by ckatz #+# #+# */
/* Updated: 2018/07/17 10:34:05 by ckatz ### ########.fr */
/* */
/* ************************************************************************** */
#include "Int8.hpp"
#include "Error.hpp"
Int8::Int8(void)
{
}
Int8::Int8(const std::string numAsString, eOperandType type)
{
int t = stold(numAsString);
try
{
if (t > INT8_MAX)
{
throw Error::OverflowException();
}
}
catch (std::exception & e)
{
std::cout << e.what() << std::endl;
std::exit(EXIT_FAILURE);
}
try
{
if (t < INT8_MIN)
{
throw Error::UnderflowException();
}
}
catch (std::exception & e)
{
std::cout << e.what() << std::endl;
std::exit(EXIT_FAILURE);
}
int8_t temp = static_cast<int8_t>(t);
this->_value = std::to_string(temp);
this->_type = type;
}
Int8::~Int8(void)
{
}
int Int8::getPrecision(void) const
{
eOperandType precision = INT8;
return precision;
}
eOperandType Int8::getType(void) const
{
return this->_type;
}
std::string const & Int8::toString(void) const
{
return this->_value;
}
IOperand const * Int8::operator+(IOperand const & rhs) const
{
eOperandType newType;
OperandFactory fact;
long double a = std::stold(toString());
long double b = std::stold(rhs.toString());
newType = (getPrecision() > rhs.getPrecision()) ? getType() : rhs.getType();
return fact.createOperand(newType, std::to_string(a + b));
}
IOperand const * Int8::operator-(IOperand const & rhs) const
{
eOperandType newType;
OperandFactory fact;
long double a = std::stold(toString());
long double b = std::stold(rhs.toString());
newType = (getPrecision() > rhs.getPrecision()) ? getType() : rhs.getType();
return fact.createOperand(newType, std::to_string(a - b));
}
IOperand const * Int8::operator*(IOperand const & rhs) const
{
eOperandType newType;
OperandFactory fact;
long double a = std::stold(toString());
long double b = std::stold(rhs.toString());
newType = (getPrecision() > rhs.getPrecision()) ? getType() : rhs.getType();
return fact.createOperand(newType, std::to_string(a * b));
}
IOperand const * Int8::operator/(IOperand const & rhs) const
{
eOperandType newType;
OperandFactory fact;
long double a = std::stold(toString());
long double b = std::stold(rhs.toString());
newType = (getPrecision() > rhs.getPrecision()) ? getType() : rhs.getType();
return fact.createOperand(newType, std::to_string(a / b));
}
IOperand const * Int8::operator%(IOperand const & rhs) const
{
eOperandType newType;
OperandFactory fact;
long double a = std::stold(toString());
long double b = std::stold(rhs.toString());
newType = (getPrecision() > rhs.getPrecision()) ? getType() : rhs.getType();
return fact.createOperand(newType, std::to_string(std::fmod(a, b)));
}