Changeset 4057:1c4122596782 for sage/rings/rational.pyx
- Timestamp:
- 04/22/07 12:57:28 (6 years ago)
- Branch:
- default
- File:
-
- 1 edited
-
sage/rings/rational.pyx (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
-
sage/rings/rational.pyx
r4022 r4057 368 368 return self.numerator().valuation(p) - self.denominator().valuation(p) 369 369 370 def sqrt (self, bits=None):370 def sqrt_approx(self, bits=None): 371 371 r""" 372 372 Returns the positive square root of self as a real number to … … 385 385 EXAMPLES: 386 386 sage: x = 23/2 387 sage: x.sqrt ()387 sage: x.sqrt_approx() 388 388 3.39116499156263 389 389 sage: x = 32/5 390 sage: x.sqrt ()390 sage: x.sqrt_approx() 391 391 2.52982212813470 392 392 sage: x = 16/9 393 sage: x.sqrt() 394 4/3 395 sage: x.sqrt(53) 393 sage: x.sqrt_approx() 396 394 1.33333333333333 395 sage: x.sqrt_approx(100) 396 1.3333333333333333333333333333 397 397 sage: x = 9837/2 398 sage: x.sqrt ()398 sage: x.sqrt_approx() 399 399 70.1320183653658 400 400 sage: x = 645373/45 401 sage: x.sqrt ()401 sage: x.sqrt_approx() 402 402 119.756512233040 403 403 sage: x = -12/5 404 sage: x.sqrt ()404 sage: x.sqrt_approx() 405 405 1.54919333848297*I 406 406 … … 409 409 """ 410 410 if bits is None: 411 try:412 return self.square_root()413 except ValueError:414 pass415 411 bits = max(53, 2*(mpz_sizeinbase(self.value, 2)+2)) 416 412 … … 422 418 return R(self).sqrt() 423 419 424 def sq uare_root(self):420 def sqrt(self): 425 421 r""" 426 422 Return the positive rational square root of self, or raises a … … 429 425 EXAMPLES: 430 426 sage: x = 125/5 431 sage: x.sq uare_root()427 sage: x.sqrt() 432 428 5 433 429 sage: x = 64/4 434 sage: x.sq uare_root()430 sage: x.sqrt() 435 431 4 436 432 sage: x = 1000/10 437 sage: x.sq uare_root()433 sage: x.sqrt() 438 434 10 439 435 sage: x = 81/3 440 sage: x.square_root() 441 Traceback (most recent call last): 442 ... 443 ValueError: self (=27) is not a perfect square 436 sage: x.sqrt() 437 3*sqrt(3) 438 sage: x = -81/3 439 sage: x.sqrt() 440 3*sqrt(3)*I 444 441 445 442 AUTHOR: 446 -- Naqi Jaffery (2006-03-05): examples 447 """ 443 -- Naqi Jaffery (2006-03-05): some examples 444 """ 445 if self < 0: 446 from sage.calculus.calculus import sqrt 447 return sqrt(self) 448 448 # TODO -- this could be quicker, by using GMP directly. 449 return self.numerator().sq uare_root() / self.denominator().square_root()449 return self.numerator().sqrt() / self.denominator().sqrt() 450 450 451 451 def period(self): … … 694 694 32/243 695 695 sage: (-1/1)^(1/3) 696 Traceback (most recent call last): 697 ... 698 TypeError: exponent (=1/3) must be an integer. 699 Coerce your numbers to real or complex numbers first. 696 -1 700 697 701 698 We raise to some interesting powers: … … 1112 1109 def is_zero(self): 1113 1110 return bool(mpz_cmp_si(mpq_numref(self.value), 0) == 0) 1111 1112 def is_square(self): 1113 return self.numerator().is_square() and self.denominator().is_square() 1114 1114 1115 1115 cdef _lshift(self, long int exp):
Note: See TracChangeset
for help on using the changeset viewer.
