[Tutor] Need a better name for this function

Richard D. Moores rdmoores at gmail.com
Wed Dec 16 02:22:39 CET 2009

On Tue, Dec 15, 2009 at 15:05, Dave Angel <davea at ieee.org> wrote:
> Richard D. Moores wrote:
>> def float_to_exact_number_stored_in_computer(f):
>>   """
>>   Given a float, return the exact number stored in computer.
>>   See
>> http://docs.python.org/3.1/tutorial/floatingpoint.html#representation-error
>>   """
>>   from decimal import Decimal
>>   return Decimal.from_float(f)
>> I've just borrowed from the doc, but that name is too long, isn't it? Please
>> suggest something shorter but still meaningful.
>> Thanks,
>> Dick Moores
> As Hugo says, when your entire function consists of a single call to another one, you can usually simplify it.  In this case, something like:
> import decimal
> float_to_decimal_approximation = decimal.Decimal.from_float

> No need to define a new function, all you're doing is giving it a new name.  Actually, I think the existing name is clearer.

If I keep the function, renamed to Allan's suggested float2Decimal(),
then that's all I'll have to remember. But I see yours and Hugo's

> As for the name being too long, the real question is what's the purpose of the function.

I don't know if it will be useful or not, but it will satisfy my
curiosity, given a float, as to what the "exact number stored in my
computer is".

>  It certainly doesn't give you an exact representation, as that cannot be done in general, without changing the defaults in the decimal module.  For example, try 2.0 ** -50    The exact decimal version of that needs 50 digits, and Python 3.1 docs say it uses 28 by default..

For (2.0 ** -50) I get 8.8817841970012523233890533447265625E-16, or 35
digits. But for an ordinary 1.341 I get
1.3409999999999999698019337301957421004772186279296875 -- 53 digits.

Dave, I just realized that you used the term, "exact representation".
What does that mean? If it means "exact decimal version", what does
THAT mean? I'm not being facetious. I'm truly confused by this stuff
-- "the exact number stored in my computer" is the first phrase I've
understood. With 2.x I was totally in the dark about repr().


More information about the Tutor mailing list