amath  1.8.5
Simple command line calculator
numb.h
Go to the documentation of this file.
1 /*-
2  * Copyright (c) 2014-2018 Carsten Sonne Larsen <cs@innolan.net>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  * Project homepage:
26  * https://amath.innolan.net
27  *
28  */
29 
30 #ifndef AMATH_NUMBER_H
31 #define AMATH_NUMBER_H
32 
33 #if __GNUC__ > 2
34 #pragma GCC diagnostic ignored "-Wshadow"
35 #endif
36 
37 /**
38  * @file numb.h
39  * @brief Class base handling of numbers
40  */
41 
42 #include "amath.h"
43 
44 union FloatUnion64 {
45  bool IsNegative() const { return (integer >> 63) != 0; }
46  bool IsZero() const { return integer == 0; }
47  bool IsInf() const { return GetExponent() == 0x7FF && GetMantissa() == 0; }
48  bool IsNaN() const { return GetExponent() == 0x7FF && GetMantissa() != 0; }
49  bool IsMaxPositive() const { return integer == 0x7EFFFFFFFFFFFFFFull; }
50  bool IsMaxNegative() const { return integer == 0x7FFFFFFFFFFFFFFFull; }
51  int32_t GetExponent() const { return (integer >> 52) & 0x7FF; }
52  uint64_t GetMantissa() const { return integer & 0xFFFFFFFFFFFFFull; }
53  double floatingPoint;
54  uint64_t integer;
55 };
56 
57 typedef enum
58 {
64 } NumberSystem;
65 
66 struct Number
67 {
68 public:
69  explicit Number(NumberSystem system) :
70  system(system)
71  {
72  }
73 
74  virtual ~Number()
75  {
76  }
77 
78  virtual Number* Clone() = 0;
79  virtual int GetIntegerValue() = 0;
80  virtual double GetRealValue() = 0;
81  virtual bool PureComplexValue() = 0;
82  virtual int GetPrecedence() = 0;
83  virtual int GetDefaultPrecedence() = 0;
84  virtual bool IsNegative() = 0;
85  virtual bool IsZero() = 0;
86  virtual bool IsNaN() = 0;
87  virtual bool IsInfinite() = 0;
88  virtual bool IsNotImplemented() = 0;
89 
90  virtual Number* Unary() = 0;
91  virtual Number* Add(Number* other) = 0;
92  virtual Number* Sub(Number* other) = 0;
93  virtual Number* Mul(Number* other) = 0;
94  virtual Number* Div(Number* other) = 0;
95  virtual Number* Raise(Number* exponent) = 0;
96 
97  virtual Number* Signum() = 0;
98  virtual Number* Trunc() = 0;
99  virtual Number* Round() = 0;
100  virtual Number* Floor() = 0;
101  virtual Number* Ceiling() = 0;
102  virtual Number* Absolute() = 0;
103  virtual Number* SquareRoot() = 0;
104  virtual Number* CubeRoot() = 0;
105  virtual Number* Reciprocal() = 0;
106  virtual Number* Factorial() = 0;
107 
108  virtual Number* Log() = 0;
109  virtual Number* Log2() = 0;
110  virtual Number* Log10() = 0;
111 
112  virtual Number* Sine() = 0;
113  virtual Number* Cosine() = 0;
114  virtual Number* Tangent() = 0;
115  virtual Number* Cosecant() = 0;
116  virtual Number* Secant() = 0;
117  virtual Number* Cotangent() = 0;
118  virtual Number* Chord() = 0;
119  virtual Number* ExSecant() = 0;
120  virtual Number* ExCosecant() = 0;
121 
122  virtual Number* ArcSine() = 0;
123  virtual Number* ArcCosine() = 0;
124  virtual Number* ArcTangent() = 0;
125  virtual Number* ArcCosecant() = 0;
126  virtual Number* ArcSecant() = 0;
127  virtual Number* ArcCotangent() = 0;
128  virtual Number* ArcChord() = 0;
129  virtual Number* ArcExSecant() = 0;
130  virtual Number* ArcExCosecant() = 0;
131 
132  virtual Number* HypSine() = 0;
133  virtual Number* HypCosine() = 0;
134  virtual Number* HypTangent() = 0;
135  virtual Number* HypCosecant() = 0;
136  virtual Number* HypSecant() = 0;
137  virtual Number* HypCotangent() = 0;
138 
139  virtual Number* HypArcSine() = 0;
140  virtual Number* HypArcCosine() = 0;
141  virtual Number* HypArcTangent() = 0;
142  virtual Number* HypArcCosecant() = 0;
143  virtual Number* HypArcSecant() = 0;
144  virtual Number* HypArcCotangent() = 0;
145 
146  virtual Number* VerSine() = 0;
147  virtual Number* VerCosine() = 0;
148  virtual Number* CoVerSine() = 0;
149  virtual Number* CoVerCosine() = 0;
150  virtual Number* HaVerSine() = 0;
151  virtual Number* HaVerCosine() = 0;
152  virtual Number* HaCoVerSine() = 0;
153  virtual Number* HaCoVerCosine() = 0;
154 
155  virtual Number* ArcVerSine() = 0;
156  virtual Number* ArcVerCosine() = 0;
157  virtual Number* ArcCoVerSine() = 0;
158  virtual Number* ArcCoVerCosine() = 0;
159  virtual Number* ArcHaVerSine() = 0;
160  virtual Number* ArcHaVerCosine() = 0;
161  virtual Number* ArcHaCoVerSine() = 0;
162  virtual Number* ArcHaCoVerCosine() = 0;
163 
165  friend class DecimalSystem;
166  friend struct IntegerNumber;
167  friend struct RealNumber;
168  friend struct ComplexNumber;
169 
170 protected:
171  NumberSystem system;
172 };
173 
174 #endif
virtual bool IsNegative()=0
virtual ~Number()
Definition: numb.h:74
virtual int GetPrecedence()=0
virtual int GetIntegerValue()=0
virtual Number * Log()=0
virtual Number * ArcTangent()=0
virtual Number * ArcCotangent()=0
virtual Number * Cotangent()=0
bool IsNaN() const
Definition: numb.h:48
virtual Number * Absolute()=0
virtual Number * Round()=0
virtual Number * Factorial()=0
virtual Number * Mul(Number *other)=0
Definition: numb.h:63
Number(NumberSystem system)
Definition: numb.h:69
virtual bool IsInfinite()=0
virtual Number * ArcExCosecant()=0
virtual Number * VerSine()=0
virtual Number * HypArcTangent()=0
virtual Number * ExCosecant()=0
NumberSystem system
Definition: numb.h:171
virtual Number * Secant()=0
virtual Number * ArcCosine()=0
virtual Number * ArcHaVerSine()=0
virtual bool IsZero()=0
virtual Number * CoVerCosine()=0
virtual Number * Raise(Number *exponent)=0
virtual Number * HypTangent()=0
int32_t GetExponent() const
Definition: numb.h:51
virtual Number * ArcSecant()=0
virtual Number * HypArcSine()=0
Definition: numb.h:66
virtual Number * Clone()=0
virtual Number * Unary()=0
bool IsInf() const
Definition: numb.h:47
bool IsMaxNegative() const
Definition: numb.h:50
virtual Number * HypArcCotangent()=0
virtual Number * HypCotangent()=0
bool IsNegative() const
Definition: numb.h:45
virtual Number * ArcExSecant()=0
virtual bool IsNotImplemented()=0
virtual Number * Floor()=0
virtual Number * CubeRoot()=0
virtual Number * Sine()=0
double floatingPoint
Definition: numb.h:53
virtual Number * ArcHaCoVerCosine()=0
uint64_t GetMantissa() const
Definition: numb.h:52
virtual bool IsNaN()=0
virtual Number * ExSecant()=0
virtual Number * ArcHaVerCosine()=0
virtual Number * VerCosine()=0
Base class for all numeral systems with a positional notation.
Definition: ntextp.h:41
Represent a real number with 15 significant digits.
Definition: real.h:45
virtual Number * Tangent()=0
virtual double GetRealValue()=0
virtual Number * CoVerSine()=0
virtual Number * Log2()=0
virtual Number * ArcVerSine()=0
virtual Number * ArcSine()=0
virtual Number * HypArcCosine()=0
virtual Number * SquareRoot()=0
virtual Number * HypCosine()=0
uint64_t integer
Definition: numb.h:54
virtual Number * ArcChord()=0
virtual Number * HypArcSecant()=0
Represent a complex number with 2 components of 15 significant digits.
Definition: cplex.h:46
virtual Number * ArcCoVerCosine()=0
virtual Number * HaVerSine()=0
virtual Number * Cosecant()=0
virtual Number * Sub(Number *other)=0
virtual Number * HypArcCosecant()=0
bool IsMaxPositive() const
Definition: numb.h:49
virtual Number * Signum()=0
bool IsZero() const
Definition: numb.h:46
virtual Number * ArcCosecant()=0
virtual Number * Cosine()=0
virtual Number * Add(Number *other)=0
virtual Number * HaVerCosine()=0
virtual Number * Reciprocal()=0
virtual Number * Div(Number *other)=0
virtual int GetDefaultPrecedence()=0
virtual Number * ArcCoVerSine()=0
virtual Number * Trunc()=0
virtual Number * HypSecant()=0
virtual Number * HypCosecant()=0
virtual Number * Chord()=0
virtual bool PureComplexValue()=0
virtual Number * HaCoVerCosine()=0
virtual Number * Log10()=0
virtual Number * ArcHaCoVerSine()=0
virtual Number * HaCoVerSine()=0
virtual Number * ArcVerCosine()=0
virtual Number * Ceiling()=0
virtual Number * HypSine()=0
Definition: numb.h:61