[sage-support] Nearest Integer

2011-07-14 Thread Santanu Sarkar
How to find the nearest integer (+ve or -ve) of a rational number (P/Q)
where P,Q are very large integers?

-- 
To post to this group, send email to sage-support@googlegroups.com
To unsubscribe from this group, send email to 
sage-support+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sage-support
URL: http://www.sagemath.org


Re: [sage-support] Nearest Integer

2011-07-14 Thread D. S. McNeil
 How to find the nearest integer (+ve or -ve) of a rational number (P/Q)
 where P,Q are very large integers?

You could use the .round method of rationals.

sage: q = 17+1/2+1/11**1000
sage: RR(q.numerator()), RR(q.denominator())
(2.48685403212345e10413928, 1.42105944692768e10413927)
sage: q.round()
18

sage: q = 13**(10**7)+1/2+1/11**(10**7)
sage: RR(q.numerator()), RR(q.denominator())
(4.73893349282680e21553360, 1.42105944692768e10413927)
sage: z = q.round()
sage: factor(z-1)
13^1000


sage: help(q.round)
round(...)
File: sage/rings/rational.pyx (starting at line 2858)

Returns the nearest integer to self, rounding away from 0 by
default, for consistency with the builtin Python round.

INPUT:


-  ``self`` - a rational number

-  ``mode`` - a rounding mode for half integers:

   - 'toward' rounds toward zero
   - 'away' (default) rounds away from zero
   - 'up' rounds up
   - 'down' rounds down
   - 'even' rounds toward the even integer
   - 'odd' rounds toward the odd integer


Doug

-- 
To post to this group, send email to sage-support@googlegroups.com
To unsubscribe from this group, send email to 
sage-support+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sage-support
URL: http://www.sagemath.org