Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #89834 > unrolled thread

Converting 5.223701009526849e-05 to 5e-05

Started byCecil Westerhof <Cecil@decebal.nl>
First post2015-05-03 10:11 +0200
Last post2015-05-07 10:00 +0200
Articles 9 — 5 participants

Back to article view | Back to comp.lang.python


Contents

  Converting 5.223701009526849e-05 to 5e-05 Cecil Westerhof <Cecil@decebal.nl> - 2015-05-03 10:11 +0200
    Re: Converting 5.223701009526849e-05 to 5e-05 Ben Finney <ben+python@benfinney.id.au> - 2015-05-03 18:40 +1000
      Re: Converting 5.223701009526849e-05 to 5e-05 Cecil Westerhof <Cecil@decebal.nl> - 2015-05-03 11:22 +0200
        Re: Converting 5.223701009526849e-05 to 5e-05 Chris Angelico <rosuav@gmail.com> - 2015-05-03 19:51 +1000
          Re: Converting 5.223701009526849e-05 to 5e-05 Cecil Westerhof <Cecil@decebal.nl> - 2015-05-03 12:02 +0200
            Re: Converting 5.223701009526849e-05 to 5e-05 Chris Angelico <rosuav@gmail.com> - 2015-05-03 20:57 +1000
        Re: Converting 5.223701009526849e-05 to 5e-05 Dave Angel <davea@davea.name> - 2015-05-03 07:34 -0400
    Re: Converting 5.223701009526849e-05 to 5e-05 Ben Finney <ben+python@benfinney.id.au> - 2015-05-03 18:48 +1000
      Re: Converting 5.223701009526849e-05 to 5e-05 Alexander Blinne <news@blinne.net> - 2015-05-07 10:00 +0200

#89834 — Converting 5.223701009526849e-05 to 5e-05

FromCecil Westerhof <Cecil@decebal.nl>
Date2015-05-03 10:11 +0200
SubjectConverting 5.223701009526849e-05 to 5e-05
Message-ID<87vbgakrlr.fsf@Equus.decebal.nl>
When I have a value like 5.223701009526849e-05 in most cases I am not
interested in all the digest after the dot. Is there a simple way to
convert it to a string like '5e-05'?
I could do something like:
    def format_small_number(n):
        abs_n = abs(n)
        assert (abs_n < 1) and (abs_n > 0)
        length = len(str(int(1 / abs_n)))
        if length <= 4:
            return '{0}'.format(int(n * 10 ** length) / 10.0 **  length)
        else:
            return '{0}E-{1}'.format(int(n * 10 ** length), length)

I use '5E-5' because I prefer that above '5e-5'.
I should take care of rounding: now 0.9999 becomes 0.9.

But I was wondering if there is a better way.

-- 
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof

[toc] | [next] | [standalone]


#89836

FromBen Finney <ben+python@benfinney.id.au>
Date2015-05-03 18:40 +1000
Message-ID<mailman.52.1430642455.12865.python-list@python.org>
In reply to#89834
Cecil Westerhof <Cecil@decebal.nl> writes:

> When I have a value like 5.223701009526849e-05 in most cases I am not
> interested in all the digest after the dot.

What type of value is it?

A ‘float’ value has many different textual representations, most of them
inaccurate. So talking about the digits of a ‘float’ value is only
partly meaningful; digits are a property of some chosen representation,
not intrinsic to the number.

A ‘str’ value can be converted in various ways, but is useless as a
number until you create a new number from the result.

Choosing a solution will rely on understanding that the textual
representation of a number is not itself a number; and vice versa, a
number value does not have a canonical text representation.

> Is there a simple way to convert it to a string like '5e-05'?

Assuming we're talking about a ‘float’ value::

    >>> foo = 5.223701009526849e-05
    >>> "{foo:5.1}".format(foo=foo)
    '5e-05'

See the ‘str.format’ documentation, especially the detailed
documentation for the “format specification mini-language”
<URL:https://docs.python.org/3/library/string.html#format-specification-mini-language>
for how to specify exactly how you want values to be formatted as
text.

-- 
 \         “The double standard that exempts religious activities from |
  `\       almost all standards of accountability should be dismantled |
_o__)                   once and for all.” —Daniel Dennett, 2010-01-12 |
Ben Finney

[toc] | [prev] | [next] | [standalone]


#89844

FromCecil Westerhof <Cecil@decebal.nl>
Date2015-05-03 11:22 +0200
Message-ID<87mw1mkoc5.fsf@Equus.decebal.nl>
In reply to#89836
Op Sunday 3 May 2015 10:40 CEST schreef Ben Finney:

> Cecil Westerhof <Cecil@decebal.nl> writes:
>
>> When I have a value like 5.223701009526849e-05 in most cases I am
>> not interested in all the digest after the dot.
>
> What type of value is it?

If the absolute value is bigger as 0 and smaller as 1, it should be a
float. ;-)


> A ‘float’ value has many different textual representations, most of
> them inaccurate. So talking about the digits of a ‘float’ value is
> only partly meaningful; digits are a property of some chosen
> representation, not intrinsic to the number.
>
> A ‘str’ value can be converted in various ways, but is useless as a
> number until you create a new number from the result.
>
> Choosing a solution will rely on understanding that the textual
> representation of a number is not itself a number; and vice versa, a
> number value does not have a canonical text representation.

It is because I display things like:
    02:47:18: Increase memoize -> iterative 19
              (0.0004942629893776029 / 2.475001383572817e-05)

And that is way to specific.


>> Is there a simple way to convert it to a string like '5e-05'?
>
> Assuming we're talking about a ‘float’ value::
>
>>>> foo = 5.223701009526849e-05
>>>> "{foo:5.1}".format(foo=foo)
> '5e-05'
>
> See the ‘str.format’ documentation, especially the detailed
> documentation for the “format specification mini-language”
> <URL:https://docs.python.org/3/library/string.html#format-specification-mini-language>
> for how to specify exactly how you want values to be formatted as
> text.

Very interesting documentation. I go for:
    '{foo:.3E}'.format(foo=foo)

Then it simplifies also big numbers and it works for int's also. (Not
needed now, but it never hurts to be prepared for the future.)

-- 
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof

[toc] | [prev] | [next] | [standalone]


#89845

FromChris Angelico <rosuav@gmail.com>
Date2015-05-03 19:51 +1000
Message-ID<mailman.59.1430646707.12865.python-list@python.org>
In reply to#89844
On Sun, May 3, 2015 at 7:22 PM, Cecil Westerhof <Cecil@decebal.nl> wrote:
> Op Sunday 3 May 2015 10:40 CEST schreef Ben Finney:
>
>> Cecil Westerhof <Cecil@decebal.nl> writes:
>>
>>> When I have a value like 5.223701009526849e-05 in most cases I am
>>> not interested in all the digest after the dot.
>>
>> What type of value is it?
>
> If the absolute value is bigger as 0 and smaller as 1, it should be a
> float. ;-)

Or maybe a fractions.Fraction, or a decimal.Decimal, or a complex, or
maybe a RXSTRING or a Gmp.mpf! There's more than one way to store a
number...

ChrisA

[toc] | [prev] | [next] | [standalone]


#89848

FromCecil Westerhof <Cecil@decebal.nl>
Date2015-05-03 12:02 +0200
Message-ID<87egmykmhe.fsf@Equus.decebal.nl>
In reply to#89845
Op Sunday 3 May 2015 11:51 CEST schreef Chris Angelico:

> On Sun, May 3, 2015 at 7:22 PM, Cecil Westerhof <Cecil@decebal.nl> wrote:
>> Op Sunday 3 May 2015 10:40 CEST schreef Ben Finney:
>>
>>> Cecil Westerhof <Cecil@decebal.nl> writes:
>>>
>>>> When I have a value like 5.223701009526849e-05 in most cases I am
>>>> not interested in all the digest after the dot.
>>>
>>> What type of value is it?
>>
>> If the absolute value is bigger as 0 and smaller as 1, it should be
>> a float. ;-)
>
> Or maybe a fractions.Fraction, or a decimal.Decimal, or a complex,
> or maybe a RXSTRING or a Gmp.mpf! There's more than one way to store
> a number...

Oops. :'-(
I still have to learn a lot.

-- 
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof

[toc] | [prev] | [next] | [standalone]


#89854

FromChris Angelico <rosuav@gmail.com>
Date2015-05-03 20:57 +1000
Message-ID<mailman.61.1430650648.12865.python-list@python.org>
In reply to#89848
On Sun, May 3, 2015 at 8:02 PM, Cecil Westerhof <Cecil@decebal.nl> wrote:
>>> If the absolute value is bigger as 0 and smaller as 1, it should be
>>> a float. ;-)
>>
>> Or maybe a fractions.Fraction, or a decimal.Decimal, or a complex,
>> or maybe a RXSTRING or a Gmp.mpf! There's more than one way to store
>> a number...
>
> Oops. :'-(
> I still have to learn a lot.

We all do :) Not all of those are Python types, incidentally. The
first three are, although 'complex' is a bit of a cheat (a complex
number is stored as two floats, so a complex with an imag of 0 is
virtually identical to a straight float); RXSTRING is the one and only
data type in REXX, and is (as the name suggests) stored as a string;
and Gmp.mpf is Pike's data type for a floating-point value stored
using the GNU Multiprecision library (gmp), and is thus capable of
arbitrary precision storage just like Python's own integer type. And
of course, the old fogeys among us know a bunch more ways to store
floating point values - not to mention all the ways of storing *fixed*
point values (the simplest being to just store an integer with the
number of hundredths of whatever it is you have - eg storing a dollar
amount as an integral number of cents). You'd be amazed how many
different ways there are of doing the same thing!

ChrisA

[toc] | [prev] | [next] | [standalone]


#89864

FromDave Angel <davea@davea.name>
Date2015-05-03 07:34 -0400
Message-ID<mailman.65.1430653659.12865.python-list@python.org>
In reply to#89844
On 05/03/2015 05:22 AM, Cecil Westerhof wrote:
> Op Sunday 3 May 2015 10:40 CEST schreef Ben Finney:
>
>> Cecil Westerhof <Cecil@decebal.nl> writes:
>>
>>> When I have a value like 5.223701009526849e-05 in most cases I am
>>> not interested in all the digest after the dot.
>>
>> What type of value is it?
>
> If the absolute value is bigger as 0 and smaller as 1, it should be a
> float. ;-)
>

or for many uses, more likely a decimal.Decimal(), where many of the 
problems Ben mentions are moot.


-- 
DaveA

[toc] | [prev] | [next] | [standalone]


#89838

FromBen Finney <ben+python@benfinney.id.au>
Date2015-05-03 18:48 +1000
Message-ID<mailman.54.1430642933.12865.python-list@python.org>
In reply to#89834
Ben Finney <ben+python@benfinney.id.au> writes:

> Assuming we're talking about a ‘float’ value::
>
>     >>> foo = 5.223701009526849e-05
>     >>> "{foo:5.1}".format(foo=foo)
>     '5e-05'

That's not as clear as it could be. Better is to be explicit about
choosing “exponential” format::

    >>> foo = 5.223701009526849e-05
    >>> "{foo:5.0e}".format(foo=foo)
    '5e-05'

-- 
 \      “Science embraces facts and debates opinion; religion embraces |
  `\    opinion and debates the facts.” —Tom Heehler, _The Well-Spoken |
_o__)                                                       Thesaurus_ |
Ben Finney

[toc] | [prev] | [next] | [standalone]


#90090

FromAlexander Blinne <news@blinne.net>
Date2015-05-07 10:00 +0200
Message-ID<mif63i$k9a$1@speranza.aioe.org>
In reply to#89838
Am 03.05.2015 um 10:48 schrieb Ben Finney:
> That's not as clear as it could be. Better is to be explicit about
> choosing “exponential” format::
> 
>     >>> foo = 5.223701009526849e-05
>     >>> "{foo:5.0e}".format(foo=foo)
>     '5e-05'
> 

Or even better the "general" format, which also works for 0.9999:

    >>> "{foo:.1g}".format(foo=5.223701009526849e-5)
    '5e-05'
    >>> "{foo:.1g}".format(foo=0.9999)
    '1'

I guess all roads lead to Rome...

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web