Rational implemented...trying to figure out exponential

This commit is contained in:
RJ Catalano 2016-03-03 12:48:42 -06:00 committed by VoR0220
parent 9a075458ad
commit 6fa5e0fac9
2 changed files with 45 additions and 54 deletions

View File

@ -521,7 +521,6 @@ ConstantNumberType::ConstantNumberType(Literal const& _literal)
} }
else else
m_value = bigint(_literal.value()); m_value = bigint(_literal.value());
switch (_literal.subDenomination()) switch (_literal.subDenomination())
{ {
case Literal::SubDenomination::None: case Literal::SubDenomination::None:
@ -564,28 +563,14 @@ bool ConstantNumberType::isImplicitlyConvertibleTo(Type const& _convertTo) const
if (m_value == 0) if (m_value == 0)
return true; return true;
int forSignBit = (targetType->isSigned() ? 1 : 0); int forSignBit = (targetType->isSigned() ? 1 : 0);
if (m_scalingFactor == 0) //if current type is integer if (m_value > 0)
{ {
if (m_value > 0) if (m_value <= (u256(-1) >> (256 - targetType->numBits() + forSignBit)))
{
if (m_value <= (u256(-1) >> (256 - targetType->numBits() + forSignBit)))
return true;
}
else if (targetType->isSigned() && -m_value <= (u256(1) << (targetType->numBits() - forSignBit)))
return true; return true;
return false;
}
else if (m_scalingFactor != 0) //if current type is fixed point
{
if (m_value > 0)
{
if (leftOfRadix() <= (u256(-1) >> (256 - targetType->numBits() + forSignBit)))
return true;
}
else if (targetType->isSigned() && -leftOfRadix() <= (u256(1) << (targetType->numBits() - forSignBit)))
return true;
return false;
} }
else if (targetType->isSigned() && -m_value <= (u256(1) << (targetType->numBits() - forSignBit)))
return true;
return false;
} }
else if (_convertTo.category() == Category::FixedPoint) else if (_convertTo.category() == Category::FixedPoint)
{ {
@ -593,35 +578,22 @@ bool ConstantNumberType::isImplicitlyConvertibleTo(Type const& _convertTo) const
if (m_value == 0) if (m_value == 0)
return true; return true;
int forSignBit = (targetType->isSigned() ? 1 : 0); int forSignBit = (targetType->isSigned() ? 1 : 0);
if (m_scalingFactor == 0) //if the current type is an integer, focus on the integer bits if (m_value > 0)
{ {
if (m_value > 0) if (
{ m_value <= (u256(-1) >> (256 - targetType->numBits() + forSignBit)) &&
if (m_value <= (u256(-1) >> (256 - targetType->integerBits() + forSignBit))) m_scalingFactor <= targetType->fractionalBits()
return true; )
}
else if (targetType->isSigned() && -m_value <= (u256(1) << (targetType->integerBits() - forSignBit)))
return true; return true;
return false;
}
else if (m_scalingFactor != 0) //if the current type is fixed point, focus on both the
{ //integer bits and fractional bits and ensure they fit
if (m_value > 0)
{
if (
leftOfRadix() <= (u256(-1) >> (256 - targetType->integerBits() + forSignBit)) &&
rightOfRadix() <= (u256(-1) >> (256 - targetType->fractionalBits() + forSignBit))
)
return true;
}
else if ( else if (
targetType->isSigned() && targetType->isSigned() &&
-leftOfRadix() <= (u256(1) >> (256 - targetType->integerBits() + forSignBit)) && -m_value <= (u256(1) >> (256 - targetType->numBits() + forSignBit)) &&
-rightOfRadix() <= (u256(1) >> (256 - targetType->fractionalBits() + forSignBit)) m_scalingFactor <= targetType->fractionalBits()
) )
return true; return true;
return false; return false;
} }
} }
else if (_convertTo.category() == Category::FixedPoint) else if (_convertTo.category() == Category::FixedPoint)
{ {
@ -738,18 +710,11 @@ TypePointer ConstantNumberType::binaryOperatorResult(Token::Value _operator, Typ
value = m_value.numerator() & other.m_value.numerator(); value = m_value.numerator() & other.m_value.numerator();
break; break;
case Token::Add: case Token::Add:
value = leftOfRadix() + other.leftOfRadix(); value = m_value + other.m_value;
value *= boost::multiprecision::pow(bigint(10), scale);
value += rightOfRadix() + other.rightOfRadix();;
break; break;
case Token::Sub: case Token::Sub:
value = leftOfRadix() - other.leftOfRadix(); value = m_value - other.m_value;
if (rightOfRadix() < other.rightOfRadix())
scale = other.m_scalingFactor;
value *= boost::multiprecision::pow(bigint(10), scale);
value += (rightOfRadix() - other.rightOfRadix());
break; break;
//these next 4 need to be scaled accordingly if it's a fixed type
case Token::Mul: case Token::Mul:
scale = m_scalingFactor - other.m_scalingFactor; scale = m_scalingFactor - other.m_scalingFactor;
value = m_value * other.m_value; value = m_value * other.m_value;
@ -761,7 +726,6 @@ TypePointer ConstantNumberType::binaryOperatorResult(Token::Value _operator, Typ
value = m_value / other.m_value; value = m_value / other.m_value;
break; break;
case Token::Mod: case Token::Mod:
{
if (other.m_value == 0) if (other.m_value == 0)
return TypePointer(); return TypePointer();
else if (fixedPointType) else if (fixedPointType)
@ -815,7 +779,6 @@ string ConstantNumberType::toString(bool) const
{ {
if (m_value.denominator() == 1) if (m_value.denominator() == 1)
return "int_const " + m_value.numerator().str(); return "int_const " + m_value.numerator().str();
return "rational_const " + m_value.numerator().str() + '/' + m_value.denominator().str(); return "rational_const " + m_value.numerator().str() + '/' + m_value.denominator().str();
} }

View File

@ -3598,8 +3598,9 @@ BOOST_AUTO_TEST_CASE(fixed_type_literal_expression)
fixed c = 1.0 / 3.0; fixed c = 1.0 / 3.0;
ufixed d = 599 + .5367; ufixed d = 599 + .5367;
ufixed e = 35.245 % 12.9; ufixed e = 35.245 % 12.9;
ufixed g = 1.2 % 2.00000; ufixed f = 1.2 % 2.00000;
//ufixed f = 2.222 ** 3.333; //fixed g = 2 ** -1.5;
//fixed h = -3 ** -5.8;
} }
} }
)"; )";
@ -3631,6 +3632,19 @@ BOOST_AUTO_TEST_CASE(array_declaration_with_fixed_literal)
BOOST_CHECK(!success(text)); BOOST_CHECK(!success(text));
} }
BOOST_AUTO_TEST_CASE(mapping_with_fixed_literal)
{
char const* text = R"(
contract test {
mapping(fixed => string) fixedString;
function f() {
fixedString[3.14] = "Pi";
}
}
)";
BOOST_CHECK(success(text));
}
BOOST_AUTO_TEST_CASE(inline_array_fixed_literals) BOOST_AUTO_TEST_CASE(inline_array_fixed_literals)
{ {
char const* text = R"( char const* text = R"(
@ -3657,6 +3671,20 @@ BOOST_AUTO_TEST_CASE(size_capabilities_of_fixed_point_types)
BOOST_CHECK(success(text)); BOOST_CHECK(success(text));
} }
BOOST_AUTO_TEST_CASE(var_capable_of_holding_fixed_constants)
{
char const* text = R"(
contract test {
function f() {
var a = 0.12345678;
var b = 12345678.0;
var c = 0.00000009;
}
}
)";
BOOST_CHECK(success(text));
}
BOOST_AUTO_TEST_SUITE_END() BOOST_AUTO_TEST_SUITE_END()
} }