-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumber.hpp
More file actions
66 lines (50 loc) · 2.17 KB
/
Number.hpp
File metadata and controls
66 lines (50 loc) · 2.17 KB
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
#pragma once
#include <cstdint>
#include <iostream>
#include <stdexcept>
#include <string>
class Number
{
friend Number abs(Number n);
friend Number operator-(const Number& left);
friend Number operator+(const Number& left, const Number& right);
friend Number operator-(const Number& left, const Number& right);
friend Number operator*(const Number& left, const Number& right);
friend Number operator/(const Number& left, const Number& right);
friend bool operator<(const Number& left, const Number& right);
friend bool operator==(const Number& left, const Number& right);
friend std::istream& operator>>(std::istream& left, Number& right);
friend std::ostream& operator<<(std::ostream& left, const Number& right);
public:
Number(int64_t value_, int8_t right_ = 0) : value(value_), right(right_)
{
uint8_t total_columns = get_total_columns();
if(get_total_columns() > MAX_COLUMNS)
throw std::range_error("Number constructor: Try rounding more.");
}
Number(std::string str_val);
Number() = default;
explicit operator int64_t();
void round(const Number& amount) { round_or_truncate(amount,0); }
void roundup(const Number& amount) { round_or_truncate(amount,1); }
void truncate(const Number& amount) { round_or_truncate(amount,-1); }
private:
const static uint8_t MAX_COLUMNS = 18;
uint8_t get_total_columns() const;
void round_or_truncate(Number amount, int bias);
int64_t value;
int8_t right;
};
Number abs(Number n);
Number operator-(const Number& left);
Number operator+(const Number& left, const Number& right);
Number operator-(const Number& left, const Number& right);
Number operator*(const Number& left, const Number& right);
Number operator/(const Number& left, const Number& right);
bool operator<(const Number& left, const Number& right);
bool operator==(const Number& left, const Number& right);
std::istream& operator>>(std::istream& left, Number& right);
std::ostream& operator<<(std::ostream& left, const Number& right);
#define type Number
#define RELOPS_DISABLE_ASSIGNMODULO
#include "relops.cxx"