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

Vector.distance method: math domain error #5238

Closed
taylorjdawson opened this issue Jun 25, 2017 · 6 comments
Closed

Vector.distance method: math domain error #5238

taylorjdawson opened this issue Jun 25, 2017 · 6 comments
Labels
awaiting-reply Waiting for reply from issue opener, will be closed if no reply

Comments

@taylorjdawson
Copy link

Versions

  • Python: 3.6.1
  • OS: Windows 10 Education; Version 1703
  • Kivy: 1.10.0
  • Kivy installation method: pip

Description

The app will crash when rapidly clicking around inside of the window. I tracked down the issue. It is with the Vector.distance() function when the distance is calculated. The issue is the math.sqrt() function.
I had the distance method print the values that were causing the error. They are different everytime.

These values will cause the math.sqrt() function to throw the ValueError: math domain error exception:

import math
x2 = 0.48125; x1 = 0.48; y2 = 0.4716666666666667; y1 = 0.475
print(math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2))

For some reason the math.sqrt() function is unable to handle these inputs.

Steps to Reproduce:

  1. Run the code shown below
  2. Rapidly click around inside of the window that is displayed
  3. The app will crash and you will get the error seen below in the Traceback

Code and Logs

Code:

from kivy.app import App
from kivy.uix.label import Label

class MyApp(App):

    def build(self):
        return Label(text='Hello world')

if __name__ == '__main__':
    MyApp().run()

Traceback:

 Traceback (most recent call last):
   File "C:/Users/Levi/PycharmProjects/TheOfficeQuoteDatabase/test_kizy.py", line 12, in <module>
     MyApp().run()
   File "C:\Users\Levi\AppData\Local\Programs\Python\Python36-32\lib\site-packages\kivy\app.py", line 828, in run
     runTouchApp()
   File "C:\Users\Levi\AppData\Local\Programs\Python\Python36-32\lib\site-packages\kivy\base.py", line 504, in runTouchApp
     EventLoop.window.mainloop()
   File "C:\Users\Levi\AppData\Local\Programs\Python\Python36-32\lib\site-packages\kivy\core\window\window_sdl2.py", line 663, in mainloop
     self._mainloop()
   File "C:\Users\Levi\AppData\Local\Programs\Python\Python36-32\lib\site-packages\kivy\core\window\window_sdl2.py", line 405, in _mainloop
     EventLoop.idle()
   File "C:\Users\Levi\AppData\Local\Programs\Python\Python36-32\lib\site-packages\kivy\base.py", line 342, in idle
     self.dispatch_input()
   File "C:\Users\Levi\AppData\Local\Programs\Python\Python36-32\lib\site-packages\kivy\base.py", line 320, in dispatch_input
     self.input_events = mod.process(events=self.input_events)
   File "C:\Users\Levi\AppData\Local\Programs\Python\Python36-32\lib\site-packages\kivy\input\postproc\doubletap.py", line 77, in process
     double_tap = self.find_double_tap(touch)
   File "C:\Users\Levi\AppData\Local\Programs\Python\Python36-32\lib\site-packages\kivy\input\postproc\doubletap.py", line 55, in find_double_tap
     Vector(touch.osx, touch.osy))
   File "C:\Users\Levi\AppData\Local\Programs\Python\Python36-32\lib\site-packages\kivy\vector.py", line 255, in distance
     return math.sqrt((self[0] - to[0]) ** 2 + (self[1] - to[1]) ** 2)
 ValueError: math domain error
taylorjdawson added a commit to taylorjdawson/kivy that referenced this issue Jun 25, 2017
Use ** .5 to calculate the square root instead of math.sqrt() to avoid ValueError: math domain error which causes the app to crash.
Issue source: kivy#5238
@KeyWeeUsr
Copy link
Contributor

KeyWeeUsr commented Jun 25, 2017

Doesn't it raise the error because of passing in a negative number? I mean, it all works for me with 3.5.2, 3.6.0, 2.7.13(a little bit shorter result though). As I can't reproduce the error, can you double check if the passed numbers are correct in all parts? If not, the bug is somewhere else and this PR will basically push a complex number into this line which will break anyway like this:

>>> (-2)**.5 > 20/1000.0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unorderable types: complex() > float()

Maybe we should just round the numbers correctly instead of allowing the possibility of having complex numbers passed into an IF statement?

Edit: Also, maybe try to reproduce the same thing on some other machine? In any case, this seems to me rather as a CPython issue instead of our side i.e. some broken(or perhaps even fixed recently?) float math?

@taylorjdawson
Copy link
Author

Yeah I agree rounding the numbers is a much better idea, however, I was having issues with even small rounded decimal numbers such as this:

>>> math.sqrt(.1)
Traceback (most recent call last):
  File "<input>", line 1, in <module>
ValueError: math domain error

Can you reproduce this on your machine?

@KeyWeeUsr
Copy link
Contributor

I installed the 3.6.1(32bit) interpreter from scratch:

>>> from math import sqrt
>>> sqrt(.1)
0.31622776601683794
>>> import math
>>> x2 = 0.48125; x1 = 0.48; y2 = 0.4716666666666667; y1 = 0.475
>>> print(math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2))
0.0035600015605489494

Win 8.1 64bit though

@taylorjdawson
Copy link
Author

Oh okay that is very strange... I am guessing it is just my machine then. I doubt it is because of Windows 10?
I just ran it on "Bash on Ubuntu on Windows" python version 3.5.2 and they both worked fine.

@KeyWeeUsr
Copy link
Contributor

Try maybe the other 3.6.1 version (64/32 alternative) if it's the same or try to reproduce it on some other machine (preferably with the same specs or at least some win10 with your updates). I believe 3.5.2 was compiled with GCC, so you'll need to try something with MSVC instead (official Python windows builds, not Win+ubuntu).

@dessant dessant added the awaiting-reply Waiting for reply from issue opener, will be closed if no reply label Jul 7, 2017
@kivybot
Copy link

kivybot commented Jul 22, 2017

Hey there!

We're automatically closing this issue since the original poster (or another commenter) hasn't yet responded to the question or request made to them 14 days ago. We therefore assume that the user has lost interest or resolved the problem on their own. Closed issues that remain inactive for a long period may get automatically locked.

Don't worry though; if this is in error, let us know with a comment and we'll be happy to reopen the issue.

Thanks!

(Please note that this is an automated comment.)

@kivybot kivybot closed this as completed Jul 22, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting-reply Waiting for reply from issue opener, will be closed if no reply
Projects
None yet
Development

No branches or pull requests

4 participants