Skip to content
This repository has been archived by the owner on May 31, 2020. It is now read-only.

Commit

Permalink
Merge pull request #644 from rkenmi/float_modulo_int
Browse files Browse the repository at this point in the history
test_float::test_modulo_int
  • Loading branch information
freakboy3742 authored Aug 22, 2017
2 parents 3ee28b2 + 7687913 commit c729dfe
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
20 changes: 18 additions & 2 deletions batavia/types/Float.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ function python_modulo(n, M) {
return ((n % M) + M) % M
}

var MAX_FLOAT = new Float('179769313486231580793728971405303415079934132710037826936173778980444968292764750946649017977587207096330286416692887910946555547851940402630657488671505820681908902000708383676273854845817711531764475730270069855571366959622842914819860834936475292719074168444365510704342711559699508093042880177904174497791')
var MIN_FLOAT = new Float('-179769313486231580793728971405303415079934132710037826936173778980444968292764750946649017977587207096330286416692887910946555547851940402630657488671505820681908902000708383676273854845817711531764475730270069855571366959622842914819860834936475292719074168444365510704342711559699508093042880177904174497791')

/**************************************************
* Javascript compatibility methods
**************************************************/
Expand Down Expand Up @@ -314,12 +317,25 @@ Float.prototype.__mul__ = function(other) {
Float.prototype.__mod__ = function(other) {
var types = require('../types')

/* TODO: Fix case for -0.0, which is coming out 0.0 */
if (types.isinstance(other, types.Int)) {
if (other.val.isZero()) {
throw new exceptions.ZeroDivisionError.$pyclass('float modulo')
} else {
return new Float(python_modulo(this.valueOf(), parseFloat(other.val)))
var thisNum = this.valueOf()
var otherNum = parseFloat(other.val)
var result = new Float(python_modulo(thisNum, otherNum))
if (otherNum > MAX_FLOAT || otherNum < MIN_FLOAT || result.toString() === 'nan' || result.toString() === 'inf' || result.toString() === '-inf') {
throw new exceptions.OverflowError.$pyclass(
'int too large to convert to float'
)
}
if ((otherNum > thisNum && thisNum > 0) || (thisNum > otherNum && thisNum < 0) || thisNum === 0) {
return new Float(thisNum)
}
if (result.valueOf() === 0 && (thisNum % otherNum) + otherNum === otherNum) {
return new Float(otherNum)
}
return result
}
} else if (types.isinstance(other, Float)) {
if (other.valueOf() === 0) {
Expand Down
18 changes: 8 additions & 10 deletions tests/datatypes/test_float.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,13 @@ class BinaryFloatOperationTests(BinaryOperationTestCase, TranspileTestCase):
not_implemented = [

# these work, but print incorrectly

'test_floor_divide_int',
'test_modulo_int',


'test_true_divide_int',




'test_floor_divide_complex',

Expand Down Expand Up @@ -78,7 +77,7 @@ class BinaryFloatOperationTests(BinaryOperationTestCase, TranspileTestCase):
'test_power_complex',
'test_power_float',



'test_true_divide_complex',
]
Expand All @@ -90,13 +89,12 @@ class InplaceFloatOperationTests(InplaceOperationTestCase, TranspileTestCase):
not_implemented = [

# these work, but print incorrectly

'test_floor_divide_int',
'test_modulo_int',


'test_true_divide_int',



'test_floor_divide_complex',

Expand All @@ -109,7 +107,7 @@ class InplaceFloatOperationTests(InplaceOperationTestCase, TranspileTestCase):
'test_power_complex',
'test_power_float',



'test_true_divide_complex',
]

0 comments on commit c729dfe

Please sign in to comment.