float vs decimal

Hi, I just read the blog post of gvr : http://python-history.blogspot.com/2009/03/problem-with-integer-division.htm... And i wonder why
Why .33 could not be a Decimal directly ? bye -- William Dodé - http://flibuste.net Informaticien Indépendant

On Wed, Mar 11, 2009 at 12:25 PM, William Dode <wilk@flibuste.net> wrote:
I proposed something like this earlier, see: http://mail.python.org/pipermail/python-ideas/2008-December/002379.html Obviously, the proposal didn't go anywhere, the reason being that Decimal is currently implemented in Python and is thus much too inefficient to be the default (efficiency/practicality beating correctness/purity here apparently). There are non-Python implementations of the decimal standard in C, but no one could locate one with a Python-compatible license. The closest was the IBM implementation whose spec the decimal PEP was based off of, but unfortunately it uses the ICU License which has a classic-BSD-like attribution clause. Cheers, Chris -- I have a blog: http://blog.rebertia.com

On 11-03-2009, Chris Rebert wrote:
Thanks to resume the situation. I think of another question. Why it's so difficult to mix float and decimal ? For example we cannot do Decimal(float) or float * Decimal. And i'm afraid that with python 3 it will more often a pain because operation with two integers can sometimes return integer (and accept an operation with decimal) and sometimes not when the operation will return a float.
I mean, why if a Decimal is in the middle of an operation, float didn't silently become a Decimal ? -- William Dodé - http://flibuste.net Informaticien Indépendant

On Wed, Mar 11, 2009 at 2:32 PM, William Dode <wilk@flibuste.net> wrote:
It's in the FAQ section of the decimal module - http://docs.python.org/library/decimal.html : 17. Is there a way to convert a regular float to a Decimal? A. Yes, all binary floating point numbers can be exactly expressed as a Decimal. An exact conversion may take more precision than intuition would suggest, so we trap Inexact to signal a need for more precision: def float_to_decimal(f): [definition snipped] 17. Why isn’t the float_to_decimal() routine included in the module? A. There is some question about whether it is advisable to mix binary and decimal floating point. Also, its use requires some care to avoid the representation issues associated with binary floating point:
float_to_decimal(1.1) Decimal('1.100000000000000088817841970012523233890533447265625')
Cheers, Chris -- I have a blog: http://blog.rebertia.com

On 11-03-2009, Chris Rebert wrote: [...]
I understand. It means that explicit is better and that we should know what we do with decimal numbers... thanks -- William Dodé - http://flibuste.net Informaticien Indépendant

On Wed, Mar 11, 2009 at 12:25 PM, William Dode <wilk@flibuste.net> wrote:
I proposed something like this earlier, see: http://mail.python.org/pipermail/python-ideas/2008-December/002379.html Obviously, the proposal didn't go anywhere, the reason being that Decimal is currently implemented in Python and is thus much too inefficient to be the default (efficiency/practicality beating correctness/purity here apparently). There are non-Python implementations of the decimal standard in C, but no one could locate one with a Python-compatible license. The closest was the IBM implementation whose spec the decimal PEP was based off of, but unfortunately it uses the ICU License which has a classic-BSD-like attribution clause. Cheers, Chris -- I have a blog: http://blog.rebertia.com

On 11-03-2009, Chris Rebert wrote:
Thanks to resume the situation. I think of another question. Why it's so difficult to mix float and decimal ? For example we cannot do Decimal(float) or float * Decimal. And i'm afraid that with python 3 it will more often a pain because operation with two integers can sometimes return integer (and accept an operation with decimal) and sometimes not when the operation will return a float.
I mean, why if a Decimal is in the middle of an operation, float didn't silently become a Decimal ? -- William Dodé - http://flibuste.net Informaticien Indépendant

On Wed, Mar 11, 2009 at 2:32 PM, William Dode <wilk@flibuste.net> wrote:
It's in the FAQ section of the decimal module - http://docs.python.org/library/decimal.html : 17. Is there a way to convert a regular float to a Decimal? A. Yes, all binary floating point numbers can be exactly expressed as a Decimal. An exact conversion may take more precision than intuition would suggest, so we trap Inexact to signal a need for more precision: def float_to_decimal(f): [definition snipped] 17. Why isn’t the float_to_decimal() routine included in the module? A. There is some question about whether it is advisable to mix binary and decimal floating point. Also, its use requires some care to avoid the representation issues associated with binary floating point:
float_to_decimal(1.1) Decimal('1.100000000000000088817841970012523233890533447265625')
Cheers, Chris -- I have a blog: http://blog.rebertia.com

On 11-03-2009, Chris Rebert wrote: [...]
I understand. It means that explicit is better and that we should know what we do with decimal numbers... thanks -- William Dodé - http://flibuste.net Informaticien Indépendant
participants (2)
-
Chris Rebert
-
William Dode