ValueError: math domain error in python2 [duplicate] - python

This question already has answers here:
Math domain error in python when using log
(2 answers)
Closed 5 years ago.
when I run this code:
max(MIN_LEARNING_RATE, min(0.5, 1.0 - math.log10((t+1)/25)))
with t = 0
I've this error:
ValueError: math domain error
but if i run the same code with python 3.6 the error disappeared

It's because in Python 2, the division returns a floor of the float, and in the case where it's 1/25, it returns 0.
And math.log(0) gives a domain error.
So in your Python 2 code, add this at the start of the script:
from __future__ import division
By default in Python 2:
$ python2
Python 2.7.13 (default, Dec 18 2016, 07:03:39)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import math
>>> math.log(1/25)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: math domain error
>>> 1/25
0
In Python 3:
$ python3
Python 3.6.1 (default, Apr 4 2017, 09:40:21)
[GCC 4.2.1 Compatible Apple LLVM 8.1.0 (clang-802.0.38)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import math
>>> math.log(1/25)
-3.2188758248682006
>>> 1/25
0.04
In Python 2 with __future__.division:
$ python2
Python 2.7.13 (default, Dec 18 2016, 07:03:39)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from __future__ import division
>>> import math
>>> math.log(1/25)
-3.2188758248682006
>>> 1/25
0.04

Due to rounding in python-2.x, (0+1)/25 evaluates to 1/25, which rounds to 0. Thus, math.log10(0), which is undefined, results in a ValueError.
Either add from __future__ import division to the first line in your program or change the line of code to max(MIN_LEARNING_RATE, min(0.5, 1.0 - math.log10((t+1)/25.0)))

Related

Malformatted decimal giving wrong error

I have this snippet:
Decimal('5,1')
What it should raise (verified on Python 3.6.5):
decimal.InvalidOperation: Invalid literal for Decimal: '5,1'
What I get in a certain Python 3.6.1 environment:
decimal.InvalidOperation: [<class 'decimal.ConversionSyntax'>]
Why does this happen?
It seems indeed a bug that was fixed in releases between 3.6.1 and 3.6.5.
My machine has 3.6.1 installed and I could reproduce it:
Python 3.6.1 (v3.6.1:69c0db5050, Mar 21 2017, 01:21:04)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from decimal import Decimal
>>> Decimal('5,1')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
decimal.InvalidOperation: [<class 'decimal.ConversionSyntax'>]
>>>
I couldn't spot a specific fix for it in their release notes, but upgrading should do it for you.

Python - print invalid syntax when using the 'end' argument [duplicate]

This question already has answers here:
Getting SyntaxError for print with keyword argument end=' '
(17 answers)
Closed 5 years ago.
In python 3, I'm tying to print a line without skipping a line as:
print('hello', end='')
And I'm getting this error message:
print('hello', end='')
^
SyntaxError: invalid syntax
What's going on? How do I correct this error?
Are you absolutely sure you're using Python 3, and not accidentally invoking Python 2?
~ $ python3
Python 3.6.2 (default, Jul 17 2017, 16:44:45)
[GCC 4.2.1 Compatible Apple LLVM 8.1.0 (clang-802.0.42)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> print('hello', end='')
hello>>>
~ $ python2
Python 2.7.13 (default, Jul 18 2017, 09:17:00)
[GCC 4.2.1 Compatible Apple LLVM 8.1.0 (clang-802.0.42)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> print('hello', end='')
File "<stdin>", line 1
print('hello', end='')
^
SyntaxError: invalid syntax
>>>

string.maketrans unicode handling: pypy vs CPython

Pypy doesn't seem to handle string.maketrans() when arguments are unicode, however CPython does:
$ python
Python 2.7.5 (default, Oct 11 2013, 14:51:32)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import string
>>> string.maketrans(ur"-/[] ", ur"_____")
'\x00\x01\x02\x03\x04\x05\x06...'
$ pypy
Python 2.7.13 (c925e73810367cd960a32592dd7f728f436c125c, Jun 08 2017, 19:14:08)
[PyPy 5.8.0 with GCC 6.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>> import string
>>>> string.maketrans(ur"-/[] ", ur"_____")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File ".../pypy-5.8-linux_x86_64-portable/lib-python/2.7/string.py", line 78, in maketrans
buf[ord(fromstr[i])] = tostr[i]
TypeError: 'unicode' object cannot be interpreted as an index
Didn't find anything relevant on http://pypy.readthedocs.io/en/latest/cpython_differences.html.
Is this a bug of CPython or PyPy?
That's a "bug", i.e. an unexpected difference. Fixed in 7fe0041fccaa (see line 78 of https://bitbucket.org/pypy/pypy/raw/default/lib-python/2.7/string.py).

how to address Python module inspect not having function getcallargs

I am trying to use the module inspect in two different environments. In one of the environments, everything is fine. In the other, inspect appears to be missing the function getcallargs. I am not sure what's going wrong. I'm also not sure how to check the version of inspect that is being used in each environment. How can I get inspect to work in the problematic environment?
The environment that works fine is as follows:
user1#computer1:~>python
Python 2.7.6 (default, Mar 22 2014, 22:59:56)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import inspect
>>> print inspect.getcallargs
<function getcallargs at 0x7ff122c0a578>
The environment that breaks is as follows:
(virtual_environment)-bash-4.1$ python
Python 2.6.6 (r266:84292, Jan 23 2014, 10:39:35)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import inspect
>>> print inspect.getcallargs
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'getcallargs'
In Python 2.6 the inspect module does not have the getcallargs function.
https://docs.python.org/release/2.6/library/inspect.html
Python 2.7 does have getcallargs
https://docs.python.org/2/library/inspect.html

Python 2.7 on Mac OSX. can not import random package Environment getting corrupted

Last login: Tue Nov 5 15:13:08 on ttys000
Preetis-MacBook-Pro:~ preetigupta25$ which python
/Library/Frameworks/Python.framework/Versions/2.7/bin/python
Preetis-MacBook-Pro:~ preetigupta25$ /Library/Frameworks/Python.framework/Versions/2.7/bin/python
Python 2.7.6rc1 (v2.7.6rc1:4913d0e9be30+, Oct 27 2013, 20:52:11)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import random
>>> ^D
Preetis-MacBook-Pro:~ preetigupta25$ /usr/bin/python
Python 2.7.6rc1 (v2.7.6rc1:4913d0e9be30+, Oct 27 2013, 20:52:11)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import random
>>> ^D
Preetis-MacBook-Pro:~ preetigupta25$ cd /Users/preetigupta25/Documents/lab\ work/Economic\ modelling/src
-bash: cd: /Users/preetigupta25/Documents/lab work/Economic modelling/src: No such file or directory
Preetis-MacBook-Pro:~ preetigupta25$ cd /Users/preetigupta25/Documents/lab\ work/Economic\ modelling/Preeti_model/src
Preetis-MacBook-Pro:src preetigupta25$ ./model.py
Traceback (most recent call last):
File "./model.py", line 5, in <module>
from random import generate_random_no
File "/Users/preetigupta25/Documents/lab work/Economic modelling/Preeti_model/src/random.py", line 4, in <module>
from random import random
ImportError: cannot import name random
Preetis-MacBook-Pro:src preetigupta25$ python
Python 2.7.6rc1 (v2.7.6rc1:4913d0e9be30+, Oct 27 2013, 20:52:11)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import random
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "random.py", line 4, in <module>
from random import random
ImportError: cannot import name random
>>>
I have Python 2.7.6 on my Mac OSX. I am trying to include random module in my python script which is not working.
If you see above, import random works fine on python prompt. But as soon as I run my script :( it can't find random module and after that I can not import random on python prompt as well.
My script is having #!/usr/bin/python as the first line.
import os
import sys
from random import random
class generate_random_no:
def __init__(self):
#rand = random()
pass
def get_random_number(self):
return randint()
def get_random_number(self, upperbound):
return randrange(upperbound)
Avoid filename like random.py. It will shadow standard library module random.

Categories

Resources