• 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: getting nth root of an integer
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: getting nth root of an integer


  • Subject: Re: getting nth root of an integer
  • From: Charlton Wilbur <email@hidden>
  • Date: Sun, 27 May 2007 10:44:36 -0400


On May 27, 2007, at 10:33 AM, Ken Tozier wrote:


On May 27, 2007, at 10:18 AM, Charlton Wilbur wrote:

If the variable p is an integer, however, by C's integer division rules 1/p will be 0 whenever p != 1. pow(x, 1.0/p) will probably do what you want.

Thanks Charlton

Next question regards floor(). if pow(x, 1.0/3) returns a value that can be represented as an integer (like 6.000000) executing floor(6.000000) returns 5.000000. Why isn't it returning 6? I looked at the various round functions but it doesn't look like you can tell them to round down.

Most likely because pow() returns something like 5.9999999999999998215921. When you pass that to printf() or NSLog, it's rounded to a certain number of decimal places of precision for display, which means you see 6.000000. But when you pass it to floor (), it doesn't round, and chops off the decimal part, leaving 5.000000. Floating-point math is imprecise and approximate by nature; if you need precise math, you need to use something like the GNU GMP library, and operate on rational numbers rather than floating- point numbers.


Charlton





--
Charlton Wilbur
email@hidden




_______________________________________________

Cocoa-dev mailing list (email@hidden)

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: getting nth root of an integer
      • From: Ricky Sharp <email@hidden>
References: 
 >getting nth root of an integer (From: Ken Tozier <email@hidden>)
 >Re: getting nth root of an integer (From: Charlton Wilbur <email@hidden>)
 >Re: getting nth root of an integer (From: Ken Tozier <email@hidden>)

  • Prev by Date: Re: getting nth root of an integer
  • Next by Date: Re: getting nth root of an integer
  • Previous by thread: Re: getting nth root of an integer
  • Next by thread: Re: getting nth root of an integer
  • Index(es):
    • Date
    • Thread