beeware / batavia

A JavaScript implementation of the Python virtual machine.
http://pybee.org/batavia
Other
1.39k stars 425 forks source link

Failing macOS unittest - pow corner case(s) #827

Open pzrq opened 5 years ago

pzrq commented 5 years ago

Expected Behavior

# Expected - Python 3.7.3
>>> print(pow(0.7071067811865476+0.7071067811865475j, 18446744073709551616))
(0.5092909260061389+0.8605944182295221j)

Current Behavior

# Batavia (testserver demo - copy/paste and run)
print(pow(0.7071067811865476+0.7071067811865475j, 18446744073709551616))
(0.7531061966369503+0.6578989714135652j)

Steps to reproduce

  1. At a terminal, the test which inspired my issue:
    (venv) pzrq@mozzie:~/Projects/beeware/batavia$ python setup.py test -s tests.builtins.test_pow.BuiltinTwoargPowFunctionTests.test_complex_int

Your Environment

pzrq commented 5 years ago

I may have the expected/actual backwards (or at least needs more data), in an Ubuntu VM:

pzrq@pzrq-VirtualBox:~$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 18.04 LTS
Release:    18.04
Codename:   bionic

pzrq@pzrq-VirtualBox:~$ python3
Python 3.6.5 (default, Apr  1 2018, 05:46:30) 
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print(pow(0.7071067811865476+0.7071067811865475j, 18446744073709551616))
(0.7531061966369503+0.6578989714135652j)
swenson commented 5 years ago

The bug looks to be here:

https://github.com/beeware/batavia/blob/master/batavia/types/Complex.js#L364

If the exponent is larger than 100, we cast it to a JavaScript 52-bit Number, so we lose the lower-order bits.

Instead we should just do the exponentiation ourselves.