Fix some special cases. Cleanup whitespace.

This commit is contained in:
JoelKatz
2012-04-11 12:32:41 -07:00
parent dc8889668a
commit efd363538c

View File

@@ -21,13 +21,15 @@ void STAmount::canonicalize()
}
while(value < cMinValue)
{
if(offset<=cMinOffset) throw std::runtime_error("value overflow");
if (offset <= cMinOffset)
throw std::runtime_error("value overflow");
value *= 10;
--offset;
}
while(value > cMaxValue)
{
if(offset>=cMaxOffset) throw std::runtime_error("value underflow");
if (offset >= cMaxOffset)
throw std::runtime_error("value underflow");
value /= 10;
++offset;
}
@@ -70,8 +72,10 @@ std::string STAmount::getText() const
std::string post = val.substr(offset+43);
size_t s_pre = pre.find_first_not_of('0');
if(s_pre==std::string::npos) pre="0";
else pre=pre.substr(s_pre);
if (s_pre == std::string::npos)
pre="0";
else
pre = pre.substr(s_pre);
size_t s_post = post.find_last_not_of('0');
if (s_post == std::string::npos)
@@ -173,35 +177,43 @@ STAmount& STAmount::operator-=(uint64 v)
STAmount::operator double() const
{
if(!value) return 0.0;
return (static_cast<double>(value)) * pow(10.0, offset);
if (!value)
return 0.0;
return static_cast<double>(value) * pow(10.0, offset);
}
STAmount operator+(STAmount v1, STAmount v2)
{ // We can check for precision loss here (value%10)!=0
if (v1.value == 0) return v2;
if (v2.value == 0) return v1;
while(v1.offset < v2.offset)
{
v1.value /= 10;
v1.offset+=1;
++v1.offset;
}
while(v2.offset < v1.offset)
{
v2.value /= 10;
v2.offset+=1;
++v2.offset;
}
// this addition cannot overflow
return STAmount(v1.name, v1.value + v2.value, v1.offset);
}
STAmount operator-(STAmount v1, STAmount v2)
{ // We can check for precision loss here (value%10)!=0
if(v2.offset > v1.offset) throw std::runtime_error("value underflow");
{
if (v2.value == 0) return v1;
if ( (v1.value == 0) || (v2.offset > v1.offset) )
throw std::runtime_error("value underflow");
while(v1.offset > v2.offset)
{
v2.value /= 10;
++v2.offset;
}
if(v1.value < v2.value) throw std::runtime_error("value underflow");
if (v1.value < v2.value)
throw std::runtime_error("value underflow");
return STAmount(v1.name, v1.value - v2.value, v1.offset);
}
@@ -229,7 +241,8 @@ STAmount operator/(const STAmount& num, const STAmount& den)
STAmount operator*(const STAmount &v1, const STAmount &v2)
{
if( (v1.value == 0) || (v2.value == 0) ) return STAmount();
if ( (v1.value == 0) || (v2.value == 0) )
return STAmount();
// Compute (numerator*10 * denominator*10) / 10^18
CBigNum v;
@@ -242,6 +255,7 @@ STAmount operator*(const STAmount &v1, const STAmount &v2)
// 10^16 <= product <= 10^18
assert(BN_num_bytes(&v) <= 60);
return STAmount(v.getulong(), v1.offset + v2.offset + 16);
}