CMiniLexicon::FindMajorSignatures(): use log file routines
[linguistica.git] / VeryLong.cpp
blobc39cf3209556ea99b8af3f60ba591d5ffd955e9b
1 // Implementation for CVeryLong methods
2 // Copyright © 2009 The University of Chicago
3 #include "VeryLong.h"
5 #include <cmath>
6 #include "StringFunc.h"
7 #include "log2.h"
9 // NB: Only for positive numbers! But with an exponent up to 32K or
10 // down to -32K (since exponent is based on an int);
12 int AssignFromVeryLongToDouble(CVeryLong& A, double& b)
14 return (A >> b);
17 void IncrementDoubleByVeryLong(double& Double, CVeryLong& VeryLong )
18 { double Temp;
19 VeryLong >> Temp;
20 Double += Temp;
23 CVeryLong::CVeryLong()
25 m_Mantissa = 0;
26 m_Exponent = 1;
29 CVeryLong::CVeryLong (const CVeryLong& Rhs):
30 m_Mantissa(Rhs.m_Mantissa), m_Exponent(Rhs.m_Exponent)
31 {};
33 CVeryLong::CVeryLong(const double f)
34 { float Log1;
35 // Q_ASSERT (f >= 0);
36 if (f <0 ) {return;}
37 if (f==0) {
38 m_Mantissa = 0;
39 m_Exponent = 1;
40 return;
42 Log1 = (float) log10(f);
43 if (Log1 > 0) {
44 m_Exponent = int (Log1);
45 m_Mantissa = float (f / exp10 ( (float) m_Exponent) );
46 return;
48 if (Log1 < 0) {
49 Log1 *= -1.0;
50 m_Exponent = -1 + -1 * int(Log1);
51 m_Mantissa = float (f / exp10 ( (float) m_Exponent));
52 return;
54 m_Exponent = 0;
55 m_Mantissa = 1.0;
56 return;
59 CVeryLong::~CVeryLong()
60 {};
62 void CVeryLong::SetExponent(int i) {m_Exponent = i;}
64 void CVeryLong::SetMantissa (float f) {m_Mantissa = f;}
66 void CVeryLong::operator= (const CVeryLong& Rhs)
67 { m_Mantissa = Rhs.GetMantissa();
68 m_Exponent = Rhs.GetExponent();
72 int CVeryLong::operator= (const double& f)
73 { float Log1;
74 // Q_ASSERT (f >= 0);
75 if (f <0 ) {return 0;}
76 if (f==0) {
77 m_Mantissa = 0;
78 m_Exponent = 1;
79 return 1;
81 Log1 = (float) log10(f);
82 if (f > 1) {
83 m_Exponent = int (Log1);
84 m_Mantissa = float (f / exp10 ( (float) m_Exponent) );
85 return 1;
87 if (f < 1) {
88 Log1 *= -1.0;
89 m_Exponent = ( -1 * int(Log1) );
90 m_Mantissa = float (f / exp10 ( float(m_Exponent ) ) );
91 return 1;
93 m_Exponent = 0;
94 m_Mantissa = 1.0;
95 return 1;
99 CVeryLong CVeryLong::operator+ (const CVeryLong& Rhs)
100 { CVeryLong Sum;
102 if (m_Mantissa == (float) 0) {
103 Sum = Rhs;
104 return Sum;
106 if (Rhs.GetMantissa () == 0) {
107 Sum = *this;
108 return Sum;
111 const int ExponentDifference = Rhs.GetExponent() - m_Exponent;
112 if (ExponentDifference == 0 ) {
113 Sum.SetMantissa (m_Mantissa + Rhs.GetMantissa() );
114 Sum.SetExponent (m_Exponent);
115 return Sum;
117 if (ExponentDifference > 0) {
118 Sum.SetExponent(Rhs.GetExponent() );
119 Sum.SetMantissa ( Rhs.GetMantissa() +
120 m_Mantissa / exp10((float) ExponentDifference) );
121 return Sum;
122 } else { //ExponentDifference < 0
123 Sum.SetMantissa (m_Mantissa +
124 Rhs.GetMantissa() * exp10( (float) ExponentDifference) );
125 Sum.SetExponent(m_Exponent);
126 return Sum;
130 double operator+= ( double& first, CVeryLong& second)
132 CVeryLong vlFirst(first);
133 vlFirst = vlFirst + second;
134 return vlFirst.ToDouble();
137 void CVeryLong::operator+= (const CVeryLong& Rhs)
138 { *this = *this + Rhs;
139 return;
144 CVeryLong CVeryLong::operator* (const CVeryLong& Rhs)
146 CVeryLong Product;
147 float Temp;
149 Temp = m_Mantissa * Rhs.GetMantissa();
152 if (Temp >= 10)
154 Product.SetMantissa ( Temp/10 );
155 Product.SetExponent (1);
156 } else
158 Product.SetMantissa(Temp);
159 Product.SetExponent (0);
162 Product.SetExponent (Product.GetExponent() +
163 m_Exponent +
164 Rhs.GetExponent() ) ;
165 return Product;
170 CVeryLong CVeryLong::operator* (const double& Rhs)
172 CVeryLong Product,
173 vlRhs (Rhs);
175 return *this * vlRhs;
186 CVeryLong CVeryLong::operator/ (const CVeryLong& Rhs)
187 { float Log1=0;
189 m_Mantissa /= Rhs.GetMantissa();
190 Log1 = (float) log10(m_Mantissa);
191 if (Log1 < 0) {
192 m_Mantissa *= 10;
193 m_Exponent--;
194 } else if (Log1 > 0) {
195 m_Mantissa /= 10;
196 m_Exponent++;
199 m_Exponent -= Rhs.GetExponent();
200 return *this;
203 float CVeryLong::GetMantissa() const {return m_Mantissa;};
204 int CVeryLong::GetExponent() const {return m_Exponent;};
206 int CVeryLong::operator>> (double & Rhs)
208 if (m_Exponent < -308 || m_Exponent > 308)
209 {Rhs = 0;
210 return 0;}
211 else {
212 Rhs = m_Mantissa * exp10 ( (float) m_Exponent);
213 return 1;
219 int CVeryLong::operator> (const CVeryLong& Rhs)
222 if (m_Exponent == Rhs.GetExponent())
224 return ( m_Mantissa > Rhs.GetMantissa() );
226 else
228 if (m_Exponent > Rhs.GetExponent() ) {
229 return 1;
230 } else {
231 return 0;
235 int CVeryLong::operator< (const CVeryLong& Rhs)
237 if (m_Exponent == Rhs.GetExponent())
239 return ( m_Mantissa < Rhs.GetMantissa() );
240 } else {
241 if (m_Exponent < Rhs.GetExponent() ) {
242 return 1;
243 } else {
244 return 0;
248 int CVeryLong::operator> (const double& Rhs)
250 CVeryLong A(Rhs);
251 return ( *this > A ) ;
254 int CVeryLong::operator< (const double& Rhs)
255 { CVeryLong A(Rhs);
256 return ( *this > A );
258 int CVeryLong::operator> (const float & Rhs)
260 if (Rhs == 0 && m_Mantissa > 0 )
262 return 1;
264 double a = (double) Rhs;
265 return *this > a;
267 int CVeryLong::operator< (const float & Rhs)
269 double a = (double) Rhs;
270 return *this < a;
273 int CVeryLong::operator== (const double & Rhs)
275 CVeryLong A(Rhs);
276 if (m_Mantissa == A.GetMantissa() &&
277 m_Exponent == A.GetExponent() ) {
278 return 1;
279 } else {
280 return 0;
283 int CVeryLong::operator! ()
284 { if (m_Mantissa == 0) { return 1; }
285 else { return 0; }
288 double CVeryLong::ToDouble()
290 return m_Mantissa * exp10 ((float) m_Exponent);
294 QString CVeryLong::Display()
296 QString Return = DisplayFloat( m_Mantissa ) + " * 10^^ " + DisplayInt( m_Exponent );
298 return Return;