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


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

Working with decimals

Started bySeymore4Head <Seymore4Head@Hotmail.invalid>
First post2014-08-23 13:47 -0400
Last post2014-08-23 18:03 -0400
Articles 11 on this page of 31 — 8 participants

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


Contents

  Working with decimals Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-08-23 13:47 -0400
    Re: Working with decimals Joel Goldstick <joel.goldstick@gmail.com> - 2014-08-23 14:21 -0400
      Re: Working with decimals Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-08-23 15:07 -0400
        Re: Working with decimals Joel Goldstick <joel.goldstick@gmail.com> - 2014-08-23 15:22 -0400
        Re: Working with decimals Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-08-23 20:24 +0100
          Re: Working with decimals Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-08-23 15:48 -0400
            Re: Working with decimals Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-08-23 21:31 +0100
    Re: Working with decimals Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-08-23 17:13 -0400
      Re: Working with decimals Joshua Landau <joshua@landau.ws> - 2014-08-23 22:47 +0100
      Re: Working with decimals Chris Angelico <rosuav@gmail.com> - 2014-08-24 08:31 +1000
      Re: Working with decimals Joshua Landau <joshua@landau.ws> - 2014-08-23 23:47 +0100
      Re: Working with decimals Chris Angelico <rosuav@gmail.com> - 2014-08-24 08:53 +1000
      Re: Working with decimals Larry Hudson <orgnut@yahoo.com> - 2014-08-24 00:04 -0700
        Re: Working with decimals Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-08-24 10:58 -0400
        Re: Working with decimals Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-08-24 11:12 -0400
          Re: Working with decimals Larry Hudson <orgnut@yahoo.com> - 2014-08-24 14:24 -0700
            Re: Working with decimals Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-08-24 19:07 -0400
      Re: Working with decimals Joshua Landau <joshua@landau.ws> - 2014-08-24 20:12 +0100
      Re: Working with decimals Ian Kelly <ian.g.kelly@gmail.com> - 2014-08-24 13:17 -0600
      Re: Working with decimals Ian Kelly <ian.g.kelly@gmail.com> - 2014-08-24 13:19 -0600
      Re: Working with decimals Joshua Landau <joshua@landau.ws> - 2014-08-24 20:25 +0100
      Re: Working with decimals Joshua Landau <joshua@landau.ws> - 2014-08-24 20:29 +0100
        Re: Working with decimals Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-08-25 12:16 +1000
          Re: Working with decimals Chris Angelico <rosuav@gmail.com> - 2014-08-25 12:27 +1000
            Re: Working with decimals Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-08-25 12:51 +1000
              Re: Working with decimals Chris Angelico <rosuav@gmail.com> - 2014-08-25 13:01 +1000
      Re: Working with decimals Ian Kelly <ian.g.kelly@gmail.com> - 2014-08-24 13:37 -0600
      Re: Working with decimals Ian Kelly <ian.g.kelly@gmail.com> - 2014-08-24 13:40 -0600
      Re: Working with decimals Joshua Landau <joshua@landau.ws> - 2014-08-24 20:49 +0100
    Re: Working with decimals Joshua Landau <joshua@landau.ws> - 2014-08-23 22:52 +0100
      Re: Working with decimals Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-08-23 18:03 -0400

Page 2 of 2 — ← Prev page 1 [2]


#76939

FromJoshua Landau <joshua@landau.ws>
Date2014-08-24 20:25 +0100
Message-ID<mailman.13383.1408908384.18130.python-list@python.org>
In reply to#76902
On 24 August 2014 20:19, Ian Kelly <ian.g.kelly@gmail.com> wrote:
> On Sun, Aug 24, 2014 at 1:17 PM, Ian Kelly <ian.g.kelly@gmail.com> wrote:
>>
>> On Sun, Aug 24, 2014 at 1:12 PM, Joshua Landau <joshua@landau.ws> wrote:
>> > Is math not already imported by start-up?
>>
>> Why would it be?
>
> It's easy to check, by the way:
>
> $ python -c "import sys; print(sys.modules['math'])"

I don't mean into the global namespace, but imported by other modules
(like the builtins) and thus cached, making instantiation trivial.

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


#76941

FromJoshua Landau <joshua@landau.ws>
Date2014-08-24 20:29 +0100
Message-ID<mailman.13385.1408908600.18130.python-list@python.org>
In reply to#76902
On 24 August 2014 20:25, Joshua Landau <joshua@landau.ws> wrote:
> On 24 August 2014 20:19, Ian Kelly <ian.g.kelly@gmail.com> wrote:
>> On Sun, Aug 24, 2014 at 1:17 PM, Ian Kelly <ian.g.kelly@gmail.com> wrote:
>>> On Sun, Aug 24, 2014 at 1:12 PM, Joshua Landau <joshua@landau.ws> wrote:
>>> > Is math not already imported by start-up?
>
> I don't mean into the global namespace, but imported by other modules
> (like the builtins) and thus cached, making instantiation trivial.

Although it doesn't seem to be:

>>> python -c "import sys; print('math' in sys.modules)"
False

An even easier check:

>>> python -c "import time; a = time.time(); import math; b = time.time(); print(b-a)"
0.0006012916564941406

>>> python -c "import math, time; a = time.time(); import math; b = time.time(); print(b-a)"
9.5367431640625e-06

I guess I'm just pessimistic. Even so, that's not much reason to hide
the import inside a function.

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


#76951

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2014-08-25 12:16 +1000
Message-ID<53fa9c7b$0$29967$c3e8da3$5496439d@news.astraweb.com>
In reply to#76941
Joshua Landau wrote:


>>>> python -c "import sys; print('math' in sys.modules)"
> False
> 
> An even easier check:
> 
>>>> python -c "import time; a = time.time(); import math; b = time.time();
>>>> print(b-a)"
> 0.0006012916564941406
> 
>>>> python -c "import math, time; a = time.time(); import math; b =
>>>> time.time(); print(b-a)"
> 9.5367431640625e-06


I wouldn't exactly say that *two* calls to Python, each containing *five*
statements, followed by a visual comparison of two decimal numbers,
is "even easier" than a single call to Python, containing just two
statements :-)

Also, your timing test is open to interpretation and subject to interference
from the rest of your system, possibly even leading to completely the wrong
conclusion. If your system is sufficiently busy working on other tasks, the
two timings may be close together, or even reversed.


-- 
Steven

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


#76952

FromChris Angelico <rosuav@gmail.com>
Date2014-08-25 12:27 +1000
Message-ID<mailman.13392.1408933645.18130.python-list@python.org>
In reply to#76951
On Mon, Aug 25, 2014 at 12:16 PM, Steven D'Aprano
<steve+comp.lang.python@pearwood.info> wrote:
> Joshua Landau wrote:
>
>
>>>>> python -c "import sys; print('math' in sys.modules)"
>> False
>>
>> An even easier check:
>>
>>>>> python -c "import time; a = time.time(); import math; b = time.time();
>>>>> print(b-a)"
>> 0.0006012916564941406
>>
>>>>> python -c "import math, time; a = time.time(); import math; b =
>>>>> time.time(); print(b-a)"
>> 9.5367431640625e-06
>
>
> I wouldn't exactly say that *two* calls to Python, each containing *five*
> statements, followed by a visual comparison of two decimal numbers,
> is "even easier" than a single call to Python, containing just two
> statements :-)

I love this list. We can go off on a ridiculously long tangent, simply
because I said that it's only *usually* best to put imports at the top
of the file. We all agree that it normally is indeed best to hoist
them, and here we are, arguing over measurement methods on whether or
not there's ever any benefit to importing inside a function.

Yep, the Cheshire Cat was right. We're all mad here!

ChrisA

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


#76956

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2014-08-25 12:51 +1000
Message-ID<53faa4c7$0$29995$c3e8da3$5496439d@news.astraweb.com>
In reply to#76952
Chris Angelico wrote:

> I love this list. We can go off on a ridiculously long tangent, simply
> because I said that it's only *usually* best to put imports at the top
> of the file. We all agree that it normally is indeed best to hoist
> them, and here we are, arguing over measurement methods on whether or
> not there's ever any benefit to importing inside a function.
> 
> Yep, the Cheshire Cat was right. We're all mad here!

Speak for yourself! I'm not here, I'm over there.


-- 
Steven

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


#76958

FromChris Angelico <rosuav@gmail.com>
Date2014-08-25 13:01 +1000
Message-ID<mailman.13397.1408935669.18130.python-list@python.org>
In reply to#76956
On Mon, Aug 25, 2014 at 12:51 PM, Steven D'Aprano
<steve+comp.lang.python@pearwood.info> wrote:
> Chris Angelico wrote:
>
>> I love this list. We can go off on a ridiculously long tangent, simply
>> because I said that it's only *usually* best to put imports at the top
>> of the file. We all agree that it normally is indeed best to hoist
>> them, and here we are, arguing over measurement methods on whether or
>> not there's ever any benefit to importing inside a function.
>>
>> Yep, the Cheshire Cat was right. We're all mad here!
>
> Speak for yourself! I'm not here, I'm over there.

Of course you are. But thanks to the wonders of quantum entanglement,
your madness and my madness are intrinsically linked. It's like
Unicode String Theory, in that nobody understands it, but it's really
really important.

ChrisA

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


#76942

FromIan Kelly <ian.g.kelly@gmail.com>
Date2014-08-24 13:37 -0600
Message-ID<mailman.13386.1408909088.18130.python-list@python.org>
In reply to#76902

[Multipart message — attachments visible in raw view] — view raw

On Sun, Aug 24, 2014 at 1:25 PM, Joshua Landau <joshua@landau.ws> wrote:

> On 24 August 2014 20:19, Ian Kelly <ian.g.kelly@gmail.com> wrote:
> > On Sun, Aug 24, 2014 at 1:17 PM, Ian Kelly <ian.g.kelly@gmail.com>
> wrote:
> >>
> >> On Sun, Aug 24, 2014 at 1:12 PM, Joshua Landau <joshua@landau.ws>
> wrote:
> >> > Is math not already imported by start-up?
> >>
> >> Why would it be?
> >
> > It's easy to check, by the way:
> >
> > $ python -c "import sys; print(sys.modules['math'])"
>
> I don't mean into the global namespace, but imported by other modules
> (like the builtins) and thus cached, making instantiation trivial.
>

sys.modules is the module cache. If it's not in there, then it hasn't been
imported at all.

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


#76943

FromIan Kelly <ian.g.kelly@gmail.com>
Date2014-08-24 13:40 -0600
Message-ID<mailman.13387.1408909297.18130.python-list@python.org>
In reply to#76902

[Multipart message — attachments visible in raw view] — view raw

On Sun, Aug 24, 2014 at 1:29 PM, Joshua Landau <joshua@landau.ws> wrote:
>
> On 24 August 2014 20:25, Joshua Landau <joshua@landau.ws> wrote:
> > On 24 August 2014 20:19, Ian Kelly <ian.g.kelly@gmail.com> wrote:
> >> On Sun, Aug 24, 2014 at 1:17 PM, Ian Kelly <ian.g.kelly@gmail.com>
wrote:
> >>> On Sun, Aug 24, 2014 at 1:12 PM, Joshua Landau <joshua@landau.ws>
wrote:
> >>> > Is math not already imported by start-up?
> >
> > I don't mean into the global namespace, but imported by other modules
> > (like the builtins) and thus cached, making instantiation trivial.
>
> Although it doesn't seem to be:
>
> >>> python -c "import sys; print('math' in sys.modules)"
> False

That's the same check I posted, just using the in operator instead of a
straight lookup and raising an error.

> An even easier check:
>
> >>> python -c "import time; a = time.time(); import math; b =
time.time(); print(b-a)"
> 0.0006012916564941406

Too dependent on system timings. I get:

$ python -c "import time; a = time.time(); import math; b = time.time
(); print(b-a)"
0.0

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


#76945

FromJoshua Landau <joshua@landau.ws>
Date2014-08-24 20:49 +0100
Message-ID<mailman.13389.1408909820.18130.python-list@python.org>
In reply to#76902
On 24 August 2014 20:40, Ian Kelly <ian.g.kelly@gmail.com> wrote:
> That's the same check I posted, just using the in operator instead of a
> straight lookup and raising an error.

I think I need to take a break from the internet. This is the second
time in as many threads that I've responded with what I'm commenting
on.

*sigh*

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


#76903

FromJoshua Landau <joshua@landau.ws>
Date2014-08-23 22:52 +0100
Message-ID<mailman.13357.1408830777.18130.python-list@python.org>
In reply to#76884
On 23 August 2014 18:47, Seymore4Head <Seymore4Head@hotmail.invalid> wrote:
> Anyone care to suggest what method to use to fix the decimal format?

It sounds like you want a primer on floating point. The documentation
of the decimal module is actually a good read, although I don't doubt
there are even better resources somewhere:

https://docs.python.org/3/library/decimal.html

Note that you probably also want to use the decimal module, seeing as
it's good at storing decimals.

Finally, look at "moneyfmt" in the decimal docs:

https://docs.python.org/3/library/decimal.html#recipes

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


#76907

FromSeymore4Head <Seymore4Head@Hotmail.invalid>
Date2014-08-23 18:03 -0400
Message-ID<ts3iv9lsplt3uv77i9fcj0k6rcg1vaal3r@4ax.com>
In reply to#76903
On Sat, 23 Aug 2014 22:52:09 +0100, Joshua Landau <joshua@landau.ws>
wrote:

>On 23 August 2014 18:47, Seymore4Head <Seymore4Head@hotmail.invalid> wrote:
>> Anyone care to suggest what method to use to fix the decimal format?
>
>It sounds like you want a primer on floating point. The documentation
>of the decimal module is actually a good read, although I don't doubt
>there are even better resources somewhere:
>
>https://docs.python.org/3/library/decimal.html
>
>Note that you probably also want to use the decimal module, seeing as
>it's good at storing decimals.
>
>Finally, look at "moneyfmt" in the decimal docs:
>
>https://docs.python.org/3/library/decimal.html#recipes

OK.   Thanks

[toc] | [prev] | [standalone]


Page 2 of 2 — ← Prev page 1 [2]

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


csiph-web