Ah, I missed all this. Did you ever open a ticket (with this or Nils' or
other code)? I guess it's too late for your book (well, the 1st print
edition, on the internet revision means something different, as a recent
article I read on lit crit was pointing out...)
> def nth_real_root( x, n ):
> """Note: n must be a positive integer. However, x is any real number.
> (Otherwise this explanation will make no sense.)
> For odd values of n, we return the unique real number y such
> that y^n = x.
> For even values of n, if x<0, there is no real number y such
> that y^n = x and so we throw an exception.
> For even values of n, if x=>0, then we return the unique real
> number y such that y^n = x."""
>
> if ((n in ZZ)==false):
> raise RuntimeError('nth_real_root(x,n) requires n to be a
> positive integer. Your n is not an integer.')
>
> if (n<=0):
> raise RuntimeError('nth_real_root(x,n) requires n to be a
> positive integer. Your n is not positive.')
>
> assert (n in ZZ)
> assert (n>0)
>
> if ((n%2) == 0):
> # n is even
> if (x<0):
> raise RuntimeError('There is no nth real root (of a
> negative number x) when n is even.')
> else:
> return x^(1/n)
>
> assert ((n%2)==1)
> # n is odd
> return sign(x)*(abs(x))^(1/n)
>
>
By the way, apparently it's better to use a try/except clause than an
assert, so that we have a meaningful error message, or so I have heard, I'm
not a PEP guru.
--
You received this message because you are subscribed to the Google Groups
"sage-edu" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sage-edu.
For more options, visit https://groups.google.com/d/optout.