• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: why use pow(x, 2)?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: why use pow(x, 2)?


  • Subject: Re: why use pow(x, 2)?
  • From: "Stephen J. Butler" <email@hidden>
  • Date: Mon, 2 Nov 2009 14:03:46 -0600

On Mon, Nov 2, 2009 at 1:37 PM, Luke the Hiesterman <email@hidden> wrote:
> Would it really be that much faster? I don't know exactly how pow() is
> implemented, but I assume it's basically just a loop of multiplications, in
> which case it would basically be the same as x*x in this case, since it
> would exit after the first iteration....

It isn't, because the second argument is a double/float. So you can do
pow( x, 2.4 ) if you wanted. There's no way to implement that with a
simple loop of multiplications.

If you think back to your basic math classes, you'll remember that:
x^y == exp(ln(x^y)) == exp(y * ln(x)). And this is one way to do
pow(). And you can do exp() and ln() as taylor series expansions. In
fact, here's the implementation. It uses log2 and exp2, but that's
theoretically the same:

http://www.opensource.apple.com/source/Libm/Libm-315/Source/Intel/expf_logf_powf.c

But Libm has two other implementations, neither simple. Not sure how
the system chooses one:

<http://www.opensource.apple.com/source/Libm/Libm-315/Source/Intel/powf.s>
<http://www.opensource.apple.com/source/Libm/Libm-315/Source/Intel/xmm_power.c>

I scanned through, and while there are some simple cases these
implementations check for, it doesn't appear x^2 is one of them. So
pow(x,2) is likely to be much slower than x*x.
_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: why use pow(x, 2)?
      • From: Greg Parker <email@hidden>
    • Re: why use pow(x, 2)?
      • From: "Stephen J. Butler" <email@hidden>
References: 
 >why use pow(x, 2)? (From: Chunk 1978 <email@hidden>)
 >Re: why use pow(x, 2)? (From: Jens Alfke <email@hidden>)
 >Re: why use pow(x, 2)? (From: Chunk 1978 <email@hidden>)
 >Re: why use pow(x, 2)? (From: Ed Wynne <email@hidden>)
 >Re: why use pow(x, 2)? (From: Luke the Hiesterman <email@hidden>)

  • Prev by Date: Ruby Grammar for TDParseKit
  • Next by Date: Re: why use pow(x, 2)?
  • Previous by thread: Re: why use pow(x, 2)?
  • Next by thread: Re: why use pow(x, 2)?
  • Index(es):
    • Date
    • Thread