• 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: Luke the Hiesterman <email@hidden>
  • Date: Mon, 2 Nov 2009 12:17:33 -0800


On Nov 2, 2009, at 12:15 PM, Stephen J. Butler wrote:

On Mon, Nov 2, 2009 at 2:03 PM, Stephen J. Butler
<email@hidden> wrote:
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....

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.

Oops... I didn't look far enough! Down a ways is this code:

   //if y is an integer, less than 2**16, do iPow
   if( 0 == yFracBits && fabsy <= 0x1.0p16f )
   {
       int32_t iy = y; //should be exact
       int32_t yIsNeg = iy >> 31;
       iy = abs( iy );

       double dx = x;
       double result = iy & 1 ? dx : 1.0;

       while( iy >>= 1 )
       {
           dx *= dx;
           if( iy & 1 )
               result *= dx;
       }

       //We are using double precision here, so we don't need to
worry about range differences between tiny vs huge numbers for
negative Y
       if( yIsNeg )
           return (float) (1.0 / result);

       return (float) result;
   }

So it won't be much slower, but just a little slower (function call
overhead, plus all the work to get to this point).

.....which is pretty trivial in the modern world. Definitely recommend clarity as the default course of action as we've already discussed.


Luke
_______________________________________________

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


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>)
 >Re: why use pow(x, 2)? (From: "Stephen J. Butler" <email@hidden>)
 >Re: why use pow(x, 2)? (From: "Stephen J. Butler" <email@hidden>)

  • Prev by Date: Re: why use pow(x, 2)?
  • 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