[issue4796] Decimal to receive from_float method

2009-01-10 Thread Raymond Hettinger
Raymond Hettinger added the comment: Go ahead with the update, but it isn't really necessary. The PEPs are for getting something into the language in the first place and for centralizing a major discussion. They typically get out of date quickly after they've been implemented. In this case, w

[issue4796] Decimal to receive from_float method

2009-01-10 Thread Mark Dickinson
Mark Dickinson added the comment: I agree the PEP should be updated. Your proposed update looks good to me. ___ Python tracker ___ ___ Python-

[issue4796] Decimal to receive from_float method

2009-01-10 Thread Facundo Batista
Facundo Batista added the comment: Raymond, Mark, thanks for this work! I'd include the following in the PEP (under the "from float" discussion), what do you think? """ Update: The .from_float() method was added to Python 2.7 and 3.1 versions, providing lossless and exact conversion from float

[issue4796] Decimal to receive from_float method

2009-01-04 Thread Raymond Hettinger
Raymond Hettinger added the comment: > Do you think it would be worth replacing the two uses of > conditional expressions in Decimal.from_float with if-else > statements? Yes, please. > Of course, from_float still won't work with earlier versions > of Python, but having one Decimal method unav

[issue4796] Decimal to receive from_float method

2009-01-04 Thread Mark Dickinson
Mark Dickinson added the comment: Raymond, Do you think it would be worth replacing the two uses of conditional expressions in Decimal.from_float with if-else statements? Alex Goretoy pointed out (on c.l.p) that the trunk version of decimal.py can no longer be imported into Python 2.4 (and 2.3

[issue4796] Decimal to receive from_float method

2009-01-03 Thread Raymond Hettinger
Raymond Hettinger added the comment: The direct method is *much* faster! Applied Mark's suggestions. Committed as r68208 and r68211 . -- resolution: -> fixed status: open -> closed versions: +Python 2.7, Python 3.1 ___ Python tracker

[issue4796] Decimal to receive from_float method

2009-01-03 Thread Mark Dickinson
Mark Dickinson added the comment: A couple more things: 1. There's a typo 'equilvalent' in the decimal.py part of the patch. 2. Can I suggest using return d._fix(self) instead of return self.plus(d) in create_decimal_from_float. The plus method does two things: rounds to the current con

[issue4796] Decimal to receive from_float method

2009-01-03 Thread Mark Dickinson
Mark Dickinson added the comment: Instead of the repeated divisions and Inexact tests, how about a direct approach: n/2**k = (n*5**k)/10**k, so something like: sign = 0 if copysign(1.0, self) == 1.0 else 1 n, d = abs(self).as_integer_ratio() k = d.bit_length() - 1 return _dec_

[issue4796] Decimal to receive from_float method

2009-01-03 Thread Raymond Hettinger
Changes by Raymond Hettinger : Removed file: http://bugs.python.org/file12558/decimal.diff ___ Python tracker ___ ___ Python-bugs-list mailing

[issue4796] Decimal to receive from_float method

2009-01-03 Thread Raymond Hettinger
Changes by Raymond Hettinger : -- assignee: rhettinger -> marketdickinson keywords: +needs review -patch Added file: http://bugs.python.org/file12562/decimal2.diff ___ Python tracker

[issue4796] Decimal to receive from_float method

2009-01-02 Thread Raymond Hettinger
Raymond Hettinger added the comment: See attached patch for Py27. -- keywords: +patch Added file: http://bugs.python.org/file12558/decimal.diff ___ Python tracker ___ ___

[issue4796] Decimal to receive from_float method

2009-01-02 Thread Raymond Hettinger
Raymond Hettinger added the comment: > once you've converted your float to Decimal it's trivial > to round it to whatever precision you feel like, so #2 > seems unnecessary to me. Agree with Mark on how to control rounding. The DecimalWay(tm) is that used by Context.create_decimal(). A seco

[issue4796] Decimal to receive from_float method

2009-01-02 Thread Mark Dickinson
Mark Dickinson added the comment: > It looks like you and Raymond have rejected #2 but are keeping #1 I'm not against #2, but I'm not particularly for it either. In any case, once you've converted your float to Decimal it's trivial to round it to whatever precision you feel like, so #2 seems

[issue4796] Decimal to receive from_float method

2009-01-02 Thread Steven D'Aprano
Steven D'Aprano added the comment: Mark wrote: >> Also, why not just extend the Decimal() constructor to accept >> a float as the argument? Why have a separate from_float() >> method at all? > > This was discussed extensively when the decimal module was > being proposed; see the Decimal PEP f

[issue4796] Decimal to receive from_float method

2009-01-02 Thread Mark Dickinson
Mark Dickinson added the comment: > Just for the avoidance of all doubt, do you mean the largest exponent > with the number normalised to one digit to the right of the decimal > place? No. I'm using 'exponent' in the sense described in the standard. See: http://speleotrove.com/decimal/dbov

[issue4796] Decimal to receive from_float method

2009-01-02 Thread Steven D'Aprano
Steven D'Aprano added the comment: Raymond: > Accordingly, I recommend Decimal.from_float(f) with no > qualifiers or optional arguments. -0 on this one. It's going to confuse an awful lot of newbies when they write Decimal.from_float(1.1) and get Decimal('110008881784197001252...

[issue4796] Decimal to receive from_float method

2009-01-02 Thread Steven D'Aprano
Steven D'Aprano added the comment: Mark suggested the following strategy for Decimal.from_float: "always use the largest exponent possible". Just for the avoidance of all doubt, do you mean the largest exponent with the number normalised to one digit to the right of the decimal place? Becaus

[issue4796] Decimal to receive from_float method

2009-01-02 Thread Mark Dickinson
Mark Dickinson added the comment: One also has to worry about the exponent of the converted result: e.g., should Decimal.from_float(10.0) produce Decimal('1E1') or Decimal('10')? The latter looks nicer, to me. IEEE 754 isn't much help here: as far as I can tell it says nothing about binary

[issue4796] Decimal to receive from_float method

2009-01-02 Thread Mark Dickinson
Mark Dickinson added the comment: > Accordingly, I recommend Decimal.from_float(f) with no qualifiers or > optional arguments. I agree with this. Since lossless conversion is feasible, this seems the obvious way to go. It's lucky that the exponent range for (binary) floats is relatively sma

[issue4796] Decimal to receive from_float method

2008-12-31 Thread Raymond Hettinger
Raymond Hettinger added the comment: FYI, there is already a lossless implementation in the docs: def float_to_decimal(f): "Convert a floating point number to a Decimal with no loss of information" n, d = f.as_integer_ratio() with localcontext() as ctx: ctx.traps[Inexact] =

[issue4796] Decimal to receive from_float method

2008-12-31 Thread Raymond Hettinger
Raymond Hettinger added the comment: The decimal constructor should be lossless. The underlying spec was designed with the notion that all numbers in decimal are exact; operations can be lossy but the numbers themselves are exact. Accordingly, I recommend Decimal.from_float(f) with no qualifie

[issue4796] Decimal to receive from_float method

2008-12-31 Thread Raymond Hettinger
Changes by Raymond Hettinger : -- assignee: -> rhettinger nosy: +rhettinger ___ Python tracker ___ ___ Python-bugs-list mailing list U

[issue4796] Decimal to receive from_float method

2008-12-31 Thread Steven D'Aprano
New submission from Steven D'Aprano : In the PEP for Decimal, it was discussed that the class should have a from_float() method for converting from floats, but to leave it out of the Python 2.4 version: http://www.python.org/dev/peps/pep-0327/#from-float Following discussions with Mark Dickins