This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: decimal module generates AttributeError: on call to as_integer_ratio
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.1, Python 3.2, Python 3.3, Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: daniel.urban, daveabailey, mark.dickinson
Priority: normal Keywords:

Created on 2011-03-26 00:45 by daveabailey, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (3)
msg132194 - (view) Author: David Bailey (daveabailey) Date: 2011-03-26 00:45
File "C:\test3.py", line 166, in m
    self.from_float(value * decimal.Decimal(1.0))/decimal.Decimal(1.0)
  File "c:\Python27\lib\decimal.py", line 691, in from_float
    n, d = abs(f).as_integer_ratio()
AttributeError: 'Decimal' object has no attribute 'as_integer_ratio'

line 691 in decimal.py 
      
n, d = abs(f).as_integer_ratio()

should be

n, d = _math.fabs(f).as_integer_ratio()
msg132218 - (view) Author: Daniel Urban (daniel.urban) * (Python triager) Date: 2011-03-26 08:44
The problem seems to be that you're calling Decimal.from_float with a Decimal instance, not a float.  I'm not sure that should even work.  The Decimal constructor can create a decimal from an other decimal.

Your suggested solution probably would solve this exception, but I'm not sure it would be the good solution.  This way when creating a decimal from another decimal with from_float (which doesn't makes much sense, I think) could result in a loss of precision:

>>> Decimal(Decimal('0.999999999999999999999'))
Decimal('0.999999999999999999999')
>>> 
>>> math.fabs(Decimal('0.999999999999999999999'))
1.0
>>>
msg132219 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2011-03-26 09:06
As Daniel says, from_float expects a float object, not a Decimal instance.

What did you want to achieve in the following line:

   self.from_float(value * decimal.Decimal(1.0))/decimal.Decimal(1.0)

?

By the way: in all current versions of Python, from_float is redundant:  you can create a Decimal directly from a float:


Python 2.7.1+ (2.7:d52b1faa7b11+, Mar 25 2011, 21:48:24) 
[GCC 4.2.1 (Apple Inc. build 5664)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from decimal import Decimal
[64140 refs]
>>> Decimal(2.3)
Decimal('2.29999999999999982236431605997495353221893310546875')
[64149 refs]
History
Date User Action Args
2022-04-11 14:57:15adminsetgithub: 55889
2011-03-26 09:06:41mark.dickinsonsetstatus: open -> closed

nosy: + mark.dickinson
messages: + msg132219

resolution: not a bug
2011-03-26 08:44:33daniel.urbansetversions: + Python 3.1, Python 3.2, Python 3.3
nosy: + daniel.urban

messages: + msg132218

type: crash -> behavior
2011-03-26 00:45:37daveabaileycreate