Skip to content
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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions python/common/org/python/stdlib/math/math.java
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;
Copy link
Collaborator

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?


@org.python.Attribute
public static org.python.Object __file__ = new org.python.types.Str("python/common/python/math.java");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this do? The file python/common/python/math.java does not exist. Perhaps it is misplaced?


@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));
Copy link
Collaborator

Choose a reason for hiding this comment

The 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));
Copy link
Collaborator

Choose a reason for hiding this comment

The 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) {
Copy link
Collaborator

Choose a reason for hiding this comment

The 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());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

floor() should be changed to sqrt()

}

}

}
51 changes: 51 additions & 0 deletions tests/stdlib/test_math.py
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"))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is testing a string. It should be math.sqrt().

""")

@expectedFailure
def test_sqrt_negatives(self):
self.assertCodeExecution("""
import math
print(math.sqrt(-1.0))
Copy link
Collaborator

Choose a reason for hiding this comment

The 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))
""")
2 changes: 1 addition & 1 deletion tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def remove_output_dir():
)

try:
out, err = proc.communicate(timeout=30)
out, err = proc.communicate(timeout=60)
Copy link
Collaborator

Choose a reason for hiding this comment

The 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()
Expand Down