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


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

Confusing math problem

Started by"Schizoid Man" <schiz_man@21stcentury.com>
First post2013-02-21 19:33 +0000
Last post2013-02-22 09:27 +0000
Articles 2 on this page of 22 — 8 participants

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


Contents

  Confusing math problem "Schizoid Man" <schiz_man@21stcentury.com> - 2013-02-21 19:33 +0000
    Re: Confusing math problem Dave Angel <davea@davea.name> - 2013-02-21 15:25 -0500
      Re: Confusing math problem "Schizoid Man" <schiz_man@21stcentury.com> - 2013-02-21 22:39 +0000
        Re: Confusing math problem Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-02-21 22:53 +0000
          Re: Confusing math problem "Schizoid Man" <schiz_man@21stcentury.com> - 2013-02-21 23:41 +0000
            Re: Confusing math problem Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-02-22 00:04 +0000
            Re: Confusing math problem Ian Kelly <ian.g.kelly@gmail.com> - 2013-02-21 17:19 -0700
      Re: Confusing math problem "Schizoid Man" <schiz_man@21stcentury.com> - 2013-02-21 23:39 +0000
    Re: Confusing math problem Ian Kelly <ian.g.kelly@gmail.com> - 2013-02-21 13:42 -0700
    Re: Confusing math problem Chris Angelico <rosuav@gmail.com> - 2013-02-22 07:46 +1100
      Re: Confusing math problem "Schizoid Man" <schiz_man@21stcentury.com> - 2013-02-21 22:44 +0000
        Re: Confusing math problem Chris Angelico <rosuav@gmail.com> - 2013-02-22 11:29 +1100
        Re: Confusing math problem Dave Angel <davea@davea.name> - 2013-02-21 21:19 -0500
    Re: Confusing math problem Dave Angel <davea@davea.name> - 2013-02-21 15:49 -0500
    Re: Confusing math problem Chris Angelico <rosuav@gmail.com> - 2013-02-22 08:23 +1100
      Re: Confusing math problem Peter Pearson <ppearson@nowhere.invalid> - 2013-02-21 21:59 +0000
        Re: Confusing math problem Chris Angelico <rosuav@gmail.com> - 2013-02-22 09:11 +1100
        Re: Confusing math problem Dave Angel <davea@davea.name> - 2013-02-21 17:33 -0500
        Re: Confusing math problem Chris Angelico <rosuav@gmail.com> - 2013-02-22 10:15 +1100
      Re: Confusing math problem Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-02-22 09:16 +0000
        Re: Confusing math problem Serhiy Storchaka <storchaka@gmail.com> - 2013-02-22 13:48 +0200
    Re: Confusing math problem Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-02-22 09:27 +0000

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


#39552

FromSerhiy Storchaka <storchaka@gmail.com>
Date2013-02-22 13:48 +0200
Message-ID<mailman.2256.1361533725.2939.python-list@python.org>
In reply to#39528
On 22.02.13 11:16, Steven D'Aprano wrote:
> On Fri, 22 Feb 2013 08:23:27 +1100, Chris Angelico wrote:
>> and you can cast out 1's in binary to find out if it's a
>> multiple of 1, too.
>
> O_o
>
> I wanna see the numbers that aren't a multiple of 1.

What "to be a multiple of" means? If A is a multiple of B means A % B == 
0, then 0.5 isn't a multiple of 1.

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


#39529

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2013-02-22 09:27 +0000
Message-ID<512739f8$0$29988$c3e8da3$5496439d@news.astraweb.com>
In reply to#39450
On Thu, 21 Feb 2013 19:33:32 +0000, Schizoid Man wrote:

> Hi there,
> 
> I run the following code in Python 3.3.0 (on a Windows 7 machine) and
> Python 2.7.3 on a Mac and I get two different results:

Others have already explained that math.pow and the ** exponentiation 
operator are subtly different. However I wish to discuss your code:


> result1 = []
> result2 = []
> for a in range(2,101):
>     for b in range(2,101):
>         result1.append(math.pow(a,b))
>         result2.append(a**b)
> result1 = list(set(result1))
> result2 = list(set(result2))
> print (len(result1))
> print (len(result2))

This is more simply written as:

result1 = set()
result2 = set()
for a in range(2, 101):
    for b in range(2, 101):
        result1.add(a**b)
        result2.add(math.pow(a, b))

print(len(result1))
print(len(result2))

No need for the pointless conversion from list to set to list again, if 
all you want is the number of unique values.

More interesting is to gather the pairs of values that differ:

results = []
for a in range(2, 101):
    for b in range(2, 101):
        if a**b != math.pow(a, b): results.append((a, b))

You will see that integer exponentiation and floating point 
exponentiation are more frequently different than not.

(8105 of the calculations differ, 1696 of them are the same.)



-- 
Steven

[toc] | [prev] | [standalone]


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

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


csiph-web