1 // Implementation for CVeryLong methods
2 // Copyright © 2009 The University of Chicago
6 #include "StringFunc.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
)
17 void IncrementDoubleByVeryLong(double& Double
, CVeryLong
& VeryLong
)
23 CVeryLong::CVeryLong()
29 CVeryLong::CVeryLong (const CVeryLong
& Rhs
):
30 m_Mantissa(Rhs
.m_Mantissa
), m_Exponent(Rhs
.m_Exponent
)
33 CVeryLong::CVeryLong(const double f
)
42 Log1
= (float) log10(f
);
44 m_Exponent
= int (Log1
);
45 m_Mantissa
= float (f
/ exp10 ( (float) m_Exponent
) );
50 m_Exponent
= -1 + -1 * int(Log1
);
51 m_Mantissa
= float (f
/ exp10 ( (float) m_Exponent
));
59 CVeryLong::~CVeryLong()
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
)
75 if (f
<0 ) {return 0;}
81 Log1
= (float) log10(f
);
83 m_Exponent
= int (Log1
);
84 m_Mantissa
= float (f
/ exp10 ( (float) m_Exponent
) );
89 m_Exponent
= ( -1 * int(Log1
) );
90 m_Mantissa
= float (f
/ exp10 ( float(m_Exponent
) ) );
99 CVeryLong
CVeryLong::operator+ (const CVeryLong
& Rhs
)
102 if (m_Mantissa
== (float) 0) {
106 if (Rhs
.GetMantissa () == 0) {
111 const int ExponentDifference
= Rhs
.GetExponent() - m_Exponent
;
112 if (ExponentDifference
== 0 ) {
113 Sum
.SetMantissa (m_Mantissa
+ Rhs
.GetMantissa() );
114 Sum
.SetExponent (m_Exponent
);
117 if (ExponentDifference
> 0) {
118 Sum
.SetExponent(Rhs
.GetExponent() );
119 Sum
.SetMantissa ( Rhs
.GetMantissa() +
120 m_Mantissa
/ exp10((float) ExponentDifference
) );
122 } else { //ExponentDifference < 0
123 Sum
.SetMantissa (m_Mantissa
+
124 Rhs
.GetMantissa() * exp10( (float) ExponentDifference
) );
125 Sum
.SetExponent(m_Exponent
);
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
;
144 CVeryLong
CVeryLong::operator* (const CVeryLong
& Rhs
)
149 Temp
= m_Mantissa
* Rhs
.GetMantissa();
154 Product
.SetMantissa ( Temp
/10 );
155 Product
.SetExponent (1);
158 Product
.SetMantissa(Temp
);
159 Product
.SetExponent (0);
162 Product
.SetExponent (Product
.GetExponent() +
164 Rhs
.GetExponent() ) ;
170 CVeryLong
CVeryLong::operator* (const double& Rhs
)
175 return *this * vlRhs
;
186 CVeryLong
CVeryLong::operator/ (const CVeryLong
& Rhs
)
189 m_Mantissa
/= Rhs
.GetMantissa();
190 Log1
= (float) log10(m_Mantissa
);
194 } else if (Log1
> 0) {
199 m_Exponent
-= Rhs
.GetExponent();
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)
212 Rhs
= m_Mantissa
* exp10 ( (float) m_Exponent
);
219 int CVeryLong::operator> (const CVeryLong
& Rhs
)
222 if (m_Exponent
== Rhs
.GetExponent())
224 return ( m_Mantissa
> Rhs
.GetMantissa() );
228 if (m_Exponent
> Rhs
.GetExponent() ) {
235 int CVeryLong::operator< (const CVeryLong
& Rhs
)
237 if (m_Exponent
== Rhs
.GetExponent())
239 return ( m_Mantissa
< Rhs
.GetMantissa() );
241 if (m_Exponent
< Rhs
.GetExponent() ) {
248 int CVeryLong::operator> (const double& Rhs
)
251 return ( *this > A
) ;
254 int CVeryLong::operator< (const double& Rhs
)
256 return ( *this > A
);
258 int CVeryLong::operator> (const float & Rhs
)
260 if (Rhs
== 0 && m_Mantissa
> 0 )
264 double a
= (double) Rhs
;
267 int CVeryLong::operator< (const float & Rhs
)
269 double a
= (double) Rhs
;
273 int CVeryLong::operator== (const double & Rhs
)
276 if (m_Mantissa
== A
.GetMantissa() &&
277 m_Exponent
== A
.GetExponent() ) {
283 int CVeryLong::operator! ()
284 { if (m_Mantissa
== 0) { return 1; }
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
);