Skip to content

Commit

Permalink
Merge pull request beeware#751 from student-06/fix_float
Browse files Browse the repository at this point in the history
This fixes all tests for float type except test_power_complex
  • Loading branch information
freakboy3742 authored Mar 6, 2018
2 parents 54680a6 + 50e63f5 commit b935c83
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 66 deletions.
4 changes: 2 additions & 2 deletions python/common/org/python/types/Complex.java
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ private org.python.Object calCPow(org.python.types.Complex b) {
if (b.imag.value != 0. || b.real.value < 0.) {
throw new org.python.exceptions.ZeroDivisionError("0.0 to a negative or complex power");
}
r.real.value = 1.0;
r.real.value = 0.0;
r.imag.value = 0.0;
} else {
vabs = Math.hypot(this.real.value, this.imag.value);
Expand All @@ -474,7 +474,7 @@ private org.python.Object calUPow(long n) {
r = (org.python.types.Complex) r.__mul__(p);
}
mask <<= 1;
p = (Complex) p.__mul__(p);
p = (org.python.types.Complex) p.__mul__(p);
}
return r;
}
Expand Down
83 changes: 59 additions & 24 deletions python/common/org/python/types/Float.java
Original file line number Diff line number Diff line change
Expand Up @@ -305,37 +305,45 @@ public org.python.Object __sub__(org.python.Object other) {
)
public org.python.Object __mul__(org.python.Object other) {

if (other instanceof org.python.types.Str) {
if (org.python.types.Object.isSequence(other)) {
throw new org.python.exceptions.TypeError("can't multiply sequence by non-int of type '" + "float" + "'");
} else if (other instanceof org.python.types.Int) {
return new org.python.types.Float(this.value * ((org.python.types.Int) other).value);
} else if (other instanceof org.python.types.Float) {
return new org.python.types.Float(((double) this.value) * ((org.python.types.Float) other).value);
} else if (other instanceof org.python.types.Bool) {
return new org.python.types.Float(this.value * (((org.python.types.Bool) other).value ? 1 : 0));
return new org.python.types.Float(this.value * (((org.python.types.Bool) other).__int__().value));
} else if (other instanceof org.python.types.Complex) {
return ((org.python.types.Complex) other).__mul__(this);
} else if (other instanceof org.python.types.Dict) {
throw new org.python.exceptions.TypeError("unsupported operand type(s) for *: 'float' and '" + other.typeName() + "'");
} else if (other instanceof org.python.types.NoneType) {
throw new org.python.exceptions.TypeError("unsupported operand type(s) for *: 'float' and '" + other.typeName() + "'");
} else if (other instanceof org.python.types.Set) {
throw new org.python.exceptions.TypeError("unsupported operand type(s) for *: 'float' and '" + other.typeName() + "'");
} else if (other instanceof org.python.types.FrozenSet) {
throw new org.python.exceptions.TypeError("unsupported operand type(s) for *: 'float' and '" + other.typeName() + "'");
} else if (other instanceof org.python.types.List) {
throw new org.python.exceptions.TypeError("can't multiply sequence by non-int of type 'float'");
} else if (other instanceof org.python.types.Tuple) {
throw new org.python.exceptions.TypeError("can't multiply sequence by non-int of type 'float'");
} else if (other instanceof org.python.types.Slice) {
throw new org.python.exceptions.TypeError("unsupported operand type(s) for *: 'float' and 'slice'");
} else if (other instanceof org.python.types.ByteArray){
throw new org.python.exceptions.TypeError("can't multiply sequence by non-int of type 'float'");
}

throw new org.python.exceptions.NotImplementedError("float.__mul__() has not been implemented.");
return super.__mul__(other);

}


@org.python.Method(
__doc__ = "Return self*value.",
args = {"other"}
)
public org.python.Object __imul__(org.python.Object other) {

if (org.python.types.Object.isSequence(other)) {
throw new org.python.exceptions.TypeError("can't multiply sequence by non-int of type '" + "float" + "'");
} else if (other instanceof org.python.types.Int) {
return new org.python.types.Float(this.value * ((org.python.types.Int) other).value);
} else if (other instanceof org.python.types.Float) {
return new org.python.types.Float(((double) this.value) * ((org.python.types.Float) other).value);
} else if (other instanceof org.python.types.Bool) {
return new org.python.types.Float(this.value * (((org.python.types.Bool) other).__int__().value));
} else if (other instanceof org.python.types.Complex) {
return ((org.python.types.Complex) other).__imul__(this);
}
return super.__imul__(other);
}



@org.python.Method(
__doc__ = "Return self/value.",
args = {"other"}
Expand All @@ -359,9 +367,9 @@ public org.python.Object __truediv__(org.python.Object other) {
} else {
throw new org.python.exceptions.ZeroDivisionError("float division by zero");
}
} else if (other instanceof org.python.types.Complex){
} else if (other instanceof org.python.types.Complex) {
org.python.types.Complex dummycomplex = new org.python.types.Complex(this.value, 0.0);
return dummycomplex.__truediv__((org.python.types.Complex) other);
return dummycomplex.__truediv__((org.python.types.Complex) other);
}
throw new org.python.exceptions.TypeError("unsupported operand type(s) for /: 'float' and '" + other.typeName() + "'");
}
Expand Down Expand Up @@ -429,7 +437,7 @@ public org.python.Object __mod__(org.python.Object other) {
double result = (((((double) this.value) % other_val) + other_val) % other_val);
return new org.python.types.Float(result);
}
} else if (other instanceof org.python.types.Complex){
} else if (other instanceof org.python.types.Complex) {
throw new org.python.exceptions.TypeError("can't mod complex numbers.");
}
throw new org.python.exceptions.TypeError("unsupported operand type(s) for %: 'float' and '" + other.typeName() + "'");
Expand Down Expand Up @@ -479,9 +487,9 @@ public org.python.Object __pow__(org.python.Object other, org.python.Object modu
double other_val = ((org.python.types.Float) other).value;
if (this.value == 0 && other_val < 0.0) {
throw new org.python.exceptions.ZeroDivisionError("0.0 cannot be raised to a negative power");
} else if (this.value < 0 && other_val != Math.round(other_val)) {
return (new org.python.types.Complex(this.value, 0)).__pow__(other, modulo);
}
// TODO: if this.value < 0 && other_val is not an integer, this will be a Complex result, so change this.value to Complex and delegate it out
// return (new org.python.types.Complex(this.value, 0)).__pow__(other, modulo);
return new org.python.types.Float(java.lang.Math.pow(this.value, other_val));
} else if (other instanceof org.python.types.Bool) {
if (((org.python.types.Bool) other).value) {
Expand Down Expand Up @@ -724,4 +732,31 @@ public org.python.types.Str hex() {
}
return new org.python.types.Str(result);
}

@org.python.Method(
__doc__ = "",
args = {"other"}
)
public org.python.Object __getitem__(org.python.Object other) {
throw new org.python.exceptions.TypeError("'float' object is not subscriptable");
}

@org.python.Method(
__doc__ = "",
args = {"other", "value"}
)
public void __setitem__(org.python.Object other, org.python.Object value) {
throw new org.python.exceptions.TypeError("'float' object does not support item assignment");
}

@org.python.Method(
__doc__ = "",
args = {"other"}
)
public void __delitem__(org.python.Object other) {
throw new org.python.exceptions.TypeError("'float' object does not support item deletion");
}



}
1 change: 0 additions & 1 deletion tests/builtins/test_pow.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ class BuiltinTwoargPowFunctionTests(BuiltinTwoargFunctionTestCase, TranspileTest
'test_complex_complex',

'test_float_complex',
'test_float_float',
]

is_flakey = [
Expand Down
5 changes: 4 additions & 1 deletion tests/builtins/test_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ class BuiltinSetFunctionTests(BuiltinFunctionTestCase, TranspileTestCase):
not_implemented = [
'test_bytes',
'test_str',
'test_tuple',
]

not_implemented_versions = {
'test_tuple': (3.4, 3.5, 3.6)
}

is_flakey = [
'test_dict',
]
48 changes: 10 additions & 38 deletions tests/datatypes/test_float.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,15 @@ def test_hex(self):
code = '\n'.join(template.format(number) for number in numbers)
self.assertCodeExecution(code)

def test_mul_TypeError(self):
self.assertCodeExecution("""
a = 5.6
try:
print(a*None);
except TypeError as e:
print(e)
""")


class UnaryFloatOperationTests(UnaryOperationTestCase, TranspileTestCase):
data_type = 'float'
Expand All @@ -121,32 +130,6 @@ class BinaryFloatOperationTests(BinaryOperationTestCase, TranspileTestCase):
substitutions.update(SAMPLE_SUBSTITUTIONS)

not_implemented = [

'test_multiply_bytes',
'test_multiply_class',
'test_multiply_NotImplemented',
'test_multiply_range',

'test_power_float',

'test_subscr_bool',
'test_subscr_bytearray',
'test_subscr_bytes',
'test_subscr_class',
'test_subscr_complex',
'test_subscr_dict',
'test_subscr_float',
'test_subscr_frozenset',
'test_subscr_int',
'test_subscr_list',
'test_subscr_None',
'test_subscr_NotImplemented',
'test_subscr_range',
'test_subscr_set',
'test_subscr_slice',
'test_subscr_str',
'test_subscr_tuple',

]


Expand All @@ -162,15 +145,4 @@ class InplaceFloatOperationTests(InplaceOperationTestCase, TranspileTestCase):
substitutions.update(SAMPLE_SUBSTITUTIONS)

not_implemented = [
'test_multiply_bytearray',
'test_multiply_bytes',
'test_multiply_class',
'test_multiply_list',
'test_multiply_NotImplemented',
'test_multiply_range',
'test_multiply_str',
'test_multiply_tuple',

'test_power_float',

]
]
1 change: 1 addition & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,7 @@ def _string_substitutions(string):
"{'one', 'two', 'six'}": _string_substitutions("{'one', 'two', 'six'}"),
"{'a', 'b', 'c'}": _string_substitutions("{'a', 'b', 'c'}"),


# Normalize list ordering
"[1, 2.3456, 7]": _string_substitutions("[1, 2.3456, 7]"),
"[1, 2.3456, 'another']": _string_substitutions("[1, 2.3456, 'another']"),
Expand Down

0 comments on commit b935c83

Please sign in to comment.