-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add math.sqrt() #1
base: master
Are you sure you want to change the base?
Changes from 5 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package python; | ||
|
||
@org.python.Module( | ||
__doc__ = | ||
"TODO" | ||
) | ||
public class math extends org.python.types.Module { | ||
public math() { | ||
super(); | ||
} | ||
|
||
public static org.python.Object _STRUCT_TM_ITEMS; | ||
|
||
@org.python.Attribute | ||
public static org.python.Object __file__ = new org.python.types.Str("python/common/python/math.java"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What does this do? The file |
||
|
||
@org.python.Method( | ||
__doc__ = "Return the square root of number", | ||
args ={"number"} | ||
) | ||
public static org.python.Object sqrt(org.python.Object number) { | ||
if (number instanceof org.python.types.Float){ | ||
if(((org.python.types.Float) number).value < 0.0){ | ||
throw new org.python.exceptions.ValueError("math domain error"); | ||
} | ||
return new org.python.types.Float(Math.sqrt(((org.python.types.Float) number.__float__()).value)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hard to read. This could be changed into a variable set outside the if statement so it can be used on line 32. |
||
} | ||
else if(number instanceof org.python.types.Int){ | ||
if(((org.python.types.Int) number).value < 0){ | ||
throw new org.python.exceptions.ValueError("math domain error"); | ||
} | ||
return new org.python.types.Float(Math.sqrt(((org.python.types.Float) number.__float__()).value)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See comment for line 26. |
||
} | ||
else if(number instanceof org.python.types.Bool) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In python booleans are instances of Int: therefore we will never enter this case since we already check for integers in an earlier if statement on row 28. |
||
boolean val = ((org.python.types.Bool) number).value; | ||
return new org.python.types.Float(val ? 1 : 0); | ||
} | ||
else{ | ||
throw new org.python.exceptions.TypeError("floor() argument must be real number, not "+ number.typeName()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. floor() should be changed to sqrt() |
||
} | ||
|
||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import unittest | ||
|
||
from unittest import expectedFailure | ||
from ..utils import TranspileTestCase | ||
|
||
|
||
class MathModuleTests(TranspileTestCase): | ||
|
||
@expectedFailure | ||
def test_sqrt_no_number(self): | ||
self.assertCodeExecution(""" | ||
import math | ||
print(math.sqrt("no number")) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is testing a string. It should be |
||
""") | ||
|
||
@expectedFailure | ||
def test_sqrt_negatives(self): | ||
self.assertCodeExecution(""" | ||
import math | ||
print(math.sqrt(-1.0)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add a test case for a negative integer |
||
""") | ||
|
||
def test_sqrt_int(self): | ||
self.assertCodeExecution(""" | ||
import math | ||
print(math.sqrt(4)) | ||
""") | ||
|
||
def test_sqrt_floats(self): | ||
self.assertCodeExecution(""" | ||
import math | ||
print(math.sqrt(4.2)) | ||
""") | ||
|
||
def test_sqrt_bool_true(self): | ||
self.assertCodeExecution(""" | ||
import math | ||
print(math.sqrt(True)) | ||
""") | ||
|
||
def test_sqrt_bool_false(self): | ||
self.assertCodeExecution(""" | ||
import math | ||
print(math.sqrt(False)) | ||
""") | ||
|
||
def test_sqrt_zero(self): | ||
self.assertCodeExecution(""" | ||
import math | ||
print(math.sqrt(0)) | ||
""") |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -76,7 +76,7 @@ def remove_output_dir(): | |
) | ||
|
||
try: | ||
out, err = proc.communicate(timeout=30) | ||
out, err = proc.communicate(timeout=60) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the reason behind this change? |
||
except subprocess.TimeoutExpired: | ||
proc.kill() | ||
out, err = proc.communicate() | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What does this struct do?