Skip to content

Modulo(3rd argument) added to pow #876

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

Merged
merged 2 commits into from
Aug 4, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
9 changes: 9 additions & 0 deletions integration_tests/test_builtin_pow.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,15 @@ def test_pow():
assert pow(4, 2) == 16
assert abs(pow(-4235.0, 52) - 3.948003805985264e+188) < eps

i: i64
i = 7
j: i64
j = 2
k: i64
k = 5
assert pow(i, j, k) == 4
assert pow(102, 3, 121) == 38

c1: c32
c1 = complex(4, 5)
c1 = pow(c1, 4)
Expand Down
2 changes: 1 addition & 1 deletion integration_tests/test_math.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def test_pow():
a = 2
b: i64
b = 4
assert abs(pow(a, b) - 16) < eps
assert pow(a, b) == 16

def test_ldexp():
i: f64
Expand Down
23 changes: 20 additions & 3 deletions src/lpython/semantics/python_comptime_eval.h
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,16 @@ struct PythonIntrinsicProcedures {
ASR::expr_t* arg2 = args[1];
ASR::ttype_t* arg1_type = ASRUtils::expr_type(arg1);
ASR::ttype_t* arg2_type = ASRUtils::expr_type(arg2);
int64_t mod_by = -1;
if (args.size() == 3) {
ASR::expr_t* arg3 = args[2];
ASR::ttype_t* arg3_type = ASRUtils::expr_type(arg3);
if (!ASRUtils::is_integer(*arg3_type) ) { // Zero Division
throw SemanticError("Third argument must be an integer. Found: " + \
ASRUtils::type_to_str_python(arg3_type), loc);
}
mod_by = ASR::down_cast<ASR::IntegerConstant_t>(arg3)->m_n;
}
ASR::ttype_t *int_type = ASRUtils::TYPE(ASR::make_Integer_t(al, loc, 4, nullptr, 0));
ASR::ttype_t *real_type = ASRUtils::TYPE(ASR::make_Real_t(al, loc, 8, nullptr, 0));
ASR::ttype_t *complex_type = ASRUtils::TYPE(ASR::make_Complex_t(al, loc, 8, nullptr, 0));
Expand All @@ -219,9 +229,16 @@ struct PythonIntrinsicProcedures {
if (b < 0) // Negative power
return ASR::down_cast<ASR::expr_t>(make_RealConstant_t(al, loc,
pow(a, b), real_type));
else // Positive power
return ASR::down_cast<ASR::expr_t>(make_IntegerConstant_t(al, loc,
(int64_t)pow(a, b), int_type));
else {// Positive power
if (mod_by == -1)
return ASR::down_cast<ASR::expr_t>(make_IntegerConstant_t(al, loc,
(int64_t)pow(a, b), int_type));
else {
int64_t res = (int64_t)pow(a, b);
return ASR::down_cast<ASR::expr_t>(make_IntegerConstant_t(al, loc,
res % mod_by, int_type));
}
}

} else if (ASRUtils::is_real(*arg1_type) && ASRUtils::is_real(*arg2_type)) {
double a = ASR::down_cast<ASR::RealConstant_t>(arg1)->m_r;
Expand Down
61 changes: 61 additions & 0 deletions src/runtime/lpython_builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -541,3 +541,64 @@ def min(a: f64, b: f64) -> f64:
return a
else:
return b


@overload
def _floor(x: f64) -> i64:
r: i64
r = int(x)
if x >= 0 or x == r:
return r
return r - 1

@overload
def _floor(x: f32) -> i32:
r: i32
r = int(x)
if x >= 0 or x == r:
return r
return r - 1


@overload
def _mod(a: i32, b: i32) -> i32:
"""
Returns a%b
"""
r: i32
r = _floor(a/b)
return a - r*b


@overload
def _mod(a: i64, b: i64) -> i64:
"""
Returns a%b
"""
r: i64
r = _floor(a/b)
return a - r*b


@overload
def pow(x: i32, y: i32, z: i32) -> i32:
"""
Return `x` raised to the power `y`.
"""
if y < 0:
raise ValueError('y should be nonnegative')
result: i32
result = _mod(x**y, z)
return result


@overload
def pow(x: i64, y: i64, z: i64) -> i64:
"""
Return `x` raised to the power `y`.
"""
if y < 0:
raise ValueError('y should be nonnegative')
result: i64
result = _mod(x**y, z)
return result
2 changes: 1 addition & 1 deletion tests/reference/asr-complex1-f26c460.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"outfile": null,
"outfile_hash": null,
"stdout": "asr-complex1-f26c460.stdout",
"stdout_hash": "0962e62f1546143e61a7f2b8dd8f5a15f3f769accb991a8a382a1390",
"stdout_hash": "c9367e1509aa99dfeede3c7379e41ca9ca30083e9f7cb4d4a44aaa04",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/asr-complex1-f26c460.stdout

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/reference/asr-constants1-5828e8a.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"outfile": null,
"outfile_hash": null,
"stdout": "asr-constants1-5828e8a.stdout",
"stdout_hash": "a501ee33a6c5ceb7e94b68933e664826aee6e97ec90bdb17078be9ef",
"stdout_hash": "e0ab7f321cf9154c14127feb2ff3843d43456a28163ed2b117468711",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/asr-constants1-5828e8a.stdout

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/reference/asr-elemental_01-b58df26.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"outfile": null,
"outfile_hash": null,
"stdout": "asr-elemental_01-b58df26.stdout",
"stdout_hash": "15427f36dad87379bfdd0cd738b5f633d5b15fe517db961ca218d5db",
"stdout_hash": "04c9c971491d2fcbad688ca2a88ac38cb5ae071416eb8229d2c70cde",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/asr-elemental_01-b58df26.stdout

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/reference/asr-expr10-efcbb1b.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"outfile": null,
"outfile_hash": null,
"stdout": "asr-expr10-efcbb1b.stdout",
"stdout_hash": "f7a29a22601c5455d7900da733b156ebd02b8cc3b74d8c3e112ec8c2",
"stdout_hash": "9fff0e599599a527bebbb1bb6c4a1e8465db464704e5b03f1d8450fb",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/asr-expr10-efcbb1b.stdout
Original file line number Diff line number Diff line change
@@ -1 +1 @@
(TranslationUnit (SymbolTable 1 {complex@__lpython_overloaded_13__complex: (ExternalSymbol 1 complex@__lpython_overloaded_13__complex 4 __lpython_overloaded_13__complex lpython_builtin [] __lpython_overloaded_13__complex Public), complex@__lpython_overloaded_9__complex: (ExternalSymbol 1 complex@__lpython_overloaded_9__complex 4 __lpython_overloaded_9__complex lpython_builtin [] __lpython_overloaded_9__complex Public), lpython_builtin: (IntrinsicModule lpython_builtin), main_program: (Program (SymbolTable 75 {}) main_program [] []), test_UnaryOp: (Subroutine (SymbolTable 2 {a: (Variable 2 a Local () () Default (Integer 4 []) Source Public Required .false.), b: (Variable 2 b Local () () Default (Logical 4 []) Source Public Required .false.), b1: (Variable 2 b1 Local () () Default (Logical 4 []) Source Public Required .false.), b2: (Variable 2 b2 Local () () Default (Logical 4 []) Source Public Required .false.), b3: (Variable 2 b3 Local () () Default (Logical 4 []) Source Public Required .false.), c: (Variable 2 c Local () () Default (Complex 4 []) Source Public Required .false.), complex: (ExternalSymbol 2 complex 4 complex lpython_builtin [] complex Private), f: (Variable 2 f Local () () Default (Real 4 []) Source Public Required .false.)}) test_UnaryOp [] [(= (Var 2 a) (IntegerConstant 4 (Integer 4 [])) ()) (= (Var 2 a) (IntegerUnaryMinus (IntegerConstant 500 (Integer 4 [])) (Integer 4 []) (IntegerConstant -500 (Integer 4 []))) ()) (= (Var 2 a) (IntegerBitNot (IntegerConstant 5 (Integer 4 [])) (Integer 4 []) (IntegerConstant -6 (Integer 4 []))) ()) (= (Var 2 b) (LogicalNot (Cast (IntegerConstant 5 (Integer 4 [])) IntegerToLogical (Logical 4 []) (LogicalConstant .false. (Logical 4 []))) (Logical 4 []) (LogicalConstant .false. (Logical 4 []))) ()) (= (Var 2 b) (LogicalNot (Cast (IntegerUnaryMinus (IntegerConstant 1 (Integer 4 [])) (Integer 4 []) (IntegerConstant -1 (Integer 4 []))) IntegerToLogical (Logical 4 []) (LogicalConstant .false. (Logical 4 []))) (Logical 4 []) (LogicalConstant .false. (Logical 4 []))) ()) (= (Var 2 b) (LogicalNot (Cast (IntegerConstant 0 (Integer 4 [])) IntegerToLogical (Logical 4 []) (LogicalConstant .true. (Logical 4 []))) (Logical 4 []) (LogicalConstant .true. (Logical 4 []))) ()) (= (Var 2 f) (Cast (RealConstant 1.00000000000000000e+00 (Real 8 [])) RealToReal (Real 4 []) (RealConstant 1.00000000000000000e+00 (Real 4 []))) ()) (= (Var 2 f) (Cast (RealUnaryMinus (RealConstant 1.83745534000000014e+05 (Real 8 [])) (Real 8 []) (RealConstant -1.83745534000000014e+05 (Real 8 []))) RealToReal (Real 4 []) (RealConstant -1.83745534000000014e+05 (Real 4 []))) ()) (= (Var 2 b1) (LogicalConstant .true. (Logical 4 [])) ()) (= (Var 2 b2) (LogicalNot (LogicalConstant .false. (Logical 4 [])) (Logical 4 []) (LogicalConstant .true. (Logical 4 []))) ()) (= (Var 2 b3) (LogicalNot (Var 2 b2) (Logical 4 []) ()) ()) (= (Var 2 a) (IntegerConstant 1 (Integer 4 [])) ()) (= (Var 2 a) (IntegerUnaryMinus (Cast (LogicalConstant .false. (Logical 4 [])) LogicalToInteger (Integer 4 []) (IntegerConstant 0 (Integer 4 []))) (Integer 4 []) (IntegerConstant 0 (Integer 4 []))) ()) (= (Var 2 a) (IntegerBitNot (Cast (LogicalConstant .true. (Logical 4 [])) LogicalToInteger (Integer 4 []) (IntegerConstant -2 (Integer 4 []))) (Integer 4 []) (IntegerConstant -2 (Integer 4 []))) ()) (= (Var 2 c) (Cast (ComplexConstant 1.00000000000000000e+00 2.00000000000000000e+00 (Complex 8 [])) ComplexToComplex (Complex 4 []) (ComplexConstant 1.00000000000000000e+00 2.00000000000000000e+00 (Complex 4 []))) ()) (= (Var 2 c) (Cast (ComplexUnaryMinus (FunctionCall 1 complex@__lpython_overloaded_13__complex 2 complex [((IntegerConstant 3 (Integer 4 []))) ((RealConstant 6.50000000000000000e+01 (Real 8 [])))] (Complex 8 []) (ComplexConstant 3.00000000000000000e+00 6.50000000000000000e+01 (Complex 8 [])) ()) (Complex 8 []) (ComplexConstant -3.00000000000000000e+00 -6.50000000000000000e+01 (Complex 8 []))) ComplexToComplex (Complex 4 []) (ComplexConstant -3.00000000000000000e+00 -6.50000000000000000e+01 (Complex 4 []))) ()) (= (Var 2 b1) (LogicalConstant .false. (Logical 4 [])) ()) (= (Var 2 b2) (LogicalConstant .true. (Logical 4 [])) ())] Source Public Implementation () .false. .false.)}) [])
(TranslationUnit (SymbolTable 1 {complex@__lpython_overloaded_13__complex: (ExternalSymbol 1 complex@__lpython_overloaded_13__complex 4 __lpython_overloaded_13__complex lpython_builtin [] __lpython_overloaded_13__complex Public), complex@__lpython_overloaded_9__complex: (ExternalSymbol 1 complex@__lpython_overloaded_9__complex 4 __lpython_overloaded_9__complex lpython_builtin [] __lpython_overloaded_9__complex Public), lpython_builtin: (IntrinsicModule lpython_builtin), main_program: (Program (SymbolTable 81 {}) main_program [] []), test_UnaryOp: (Subroutine (SymbolTable 2 {a: (Variable 2 a Local () () Default (Integer 4 []) Source Public Required .false.), b: (Variable 2 b Local () () Default (Logical 4 []) Source Public Required .false.), b1: (Variable 2 b1 Local () () Default (Logical 4 []) Source Public Required .false.), b2: (Variable 2 b2 Local () () Default (Logical 4 []) Source Public Required .false.), b3: (Variable 2 b3 Local () () Default (Logical 4 []) Source Public Required .false.), c: (Variable 2 c Local () () Default (Complex 4 []) Source Public Required .false.), complex: (ExternalSymbol 2 complex 4 complex lpython_builtin [] complex Private), f: (Variable 2 f Local () () Default (Real 4 []) Source Public Required .false.)}) test_UnaryOp [] [(= (Var 2 a) (IntegerConstant 4 (Integer 4 [])) ()) (= (Var 2 a) (IntegerUnaryMinus (IntegerConstant 500 (Integer 4 [])) (Integer 4 []) (IntegerConstant -500 (Integer 4 []))) ()) (= (Var 2 a) (IntegerBitNot (IntegerConstant 5 (Integer 4 [])) (Integer 4 []) (IntegerConstant -6 (Integer 4 []))) ()) (= (Var 2 b) (LogicalNot (Cast (IntegerConstant 5 (Integer 4 [])) IntegerToLogical (Logical 4 []) (LogicalConstant .false. (Logical 4 []))) (Logical 4 []) (LogicalConstant .false. (Logical 4 []))) ()) (= (Var 2 b) (LogicalNot (Cast (IntegerUnaryMinus (IntegerConstant 1 (Integer 4 [])) (Integer 4 []) (IntegerConstant -1 (Integer 4 []))) IntegerToLogical (Logical 4 []) (LogicalConstant .false. (Logical 4 []))) (Logical 4 []) (LogicalConstant .false. (Logical 4 []))) ()) (= (Var 2 b) (LogicalNot (Cast (IntegerConstant 0 (Integer 4 [])) IntegerToLogical (Logical 4 []) (LogicalConstant .true. (Logical 4 []))) (Logical 4 []) (LogicalConstant .true. (Logical 4 []))) ()) (= (Var 2 f) (Cast (RealConstant 1.00000000000000000e+00 (Real 8 [])) RealToReal (Real 4 []) (RealConstant 1.00000000000000000e+00 (Real 4 []))) ()) (= (Var 2 f) (Cast (RealUnaryMinus (RealConstant 1.83745534000000014e+05 (Real 8 [])) (Real 8 []) (RealConstant -1.83745534000000014e+05 (Real 8 []))) RealToReal (Real 4 []) (RealConstant -1.83745534000000014e+05 (Real 4 []))) ()) (= (Var 2 b1) (LogicalConstant .true. (Logical 4 [])) ()) (= (Var 2 b2) (LogicalNot (LogicalConstant .false. (Logical 4 [])) (Logical 4 []) (LogicalConstant .true. (Logical 4 []))) ()) (= (Var 2 b3) (LogicalNot (Var 2 b2) (Logical 4 []) ()) ()) (= (Var 2 a) (IntegerConstant 1 (Integer 4 [])) ()) (= (Var 2 a) (IntegerUnaryMinus (Cast (LogicalConstant .false. (Logical 4 [])) LogicalToInteger (Integer 4 []) (IntegerConstant 0 (Integer 4 []))) (Integer 4 []) (IntegerConstant 0 (Integer 4 []))) ()) (= (Var 2 a) (IntegerBitNot (Cast (LogicalConstant .true. (Logical 4 [])) LogicalToInteger (Integer 4 []) (IntegerConstant -2 (Integer 4 []))) (Integer 4 []) (IntegerConstant -2 (Integer 4 []))) ()) (= (Var 2 c) (Cast (ComplexConstant 1.00000000000000000e+00 2.00000000000000000e+00 (Complex 8 [])) ComplexToComplex (Complex 4 []) (ComplexConstant 1.00000000000000000e+00 2.00000000000000000e+00 (Complex 4 []))) ()) (= (Var 2 c) (Cast (ComplexUnaryMinus (FunctionCall 1 complex@__lpython_overloaded_13__complex 2 complex [((IntegerConstant 3 (Integer 4 []))) ((RealConstant 6.50000000000000000e+01 (Real 8 [])))] (Complex 8 []) (ComplexConstant 3.00000000000000000e+00 6.50000000000000000e+01 (Complex 8 [])) ()) (Complex 8 []) (ComplexConstant -3.00000000000000000e+00 -6.50000000000000000e+01 (Complex 8 []))) ComplexToComplex (Complex 4 []) (ComplexConstant -3.00000000000000000e+00 -6.50000000000000000e+01 (Complex 4 []))) ()) (= (Var 2 b1) (LogicalConstant .false. (Logical 4 [])) ()) (= (Var 2 b2) (LogicalConstant .true. (Logical 4 [])) ())] Source Public Implementation () .false. .false.)}) [])
2 changes: 1 addition & 1 deletion tests/reference/asr-expr13-81bdb5a.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"outfile": null,
"outfile_hash": null,
"stdout": "asr-expr13-81bdb5a.stdout",
"stdout_hash": "8404ff452194d6b7f12ddd016ebf75253391cc020712ddee2624e001",
"stdout_hash": "577c708474fbfeda9c09f83756b7d22929c0644397b578eff508ad32",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
Loading