Here's something better.  Now unless I made a mistake, the floor and
ceiling functions
return a minimal interval that contains the *image* of the interval
under the floor and ceiling maps.  This is sensible, given how most
functions on intervals work.  It's a reasonable compromise until there
are integer intervals, which should definitely be implemented soon.

William

On 5/11/07, Michel <[EMAIL PROTECTED]> wrote:
>
> Thanks!
>
> I am sorry however but I think this implementation is morally wrong. I
> think floor and ceil should
> throw a ValueError
> when their value is not uniquely determined, i.e. when the interval
> contains an integer.
> After all the point of an IntervalField is to have results which are
> guaranteed to be correct.
>
> Michel
>
> On May 11, 5:14 pm, "William Stein" <[EMAIL PROTECTED]> wrote:
> > On 5/11/07, Michel <[EMAIL PROTECTED]> wrote:
> >
> >
> >
> > > I get the following strange behaviour on sage-2.5:
> >
> > > sage: R=RealIntervalField(100)
> > > sage: floor(R(10))
> >
> > > Sage just hangs. ^C twice gets the prompt back.
> >
> > > Michel
> >
> > > PS. It would seem more logical to me if floor and ceil were actually
> > > methods. An element
> > > of a RealIntervalField could have a _floor_ method which the global
> > > floor function
> > > could call if present.
> >
> > floor just tried to call the floor method if it is there, then tries other
> > methods if there is no floor method.  It fails in this case since it has
> > no way of computing the floor, since nobody implemented it yet for 
> > intervals,
> > for some reason.   Anyway, I've implemented it just now.
> >
> > [4408.txt]# HG changeset patch
> > # User William Stein <[EMAIL PROTECTED]>
> > # Date 1178896400 25200
> > # Node ID 04c23de44a4eb595683d52f864df42d24ee0b43d
> > # Parent  e90e3396991d07bf491cf671066dd46787606049
> > added floor and ceil for mpfi intervals
> >
> > diff -r e90e3396991d -r 04c23de44a4e sage/rings/real_mpfi.pyx
> > --- a/sage/rings/real_mpfi.pyx  Thu May 10 23:19:15 2007 -0700
> > +++ b/sage/rings/real_mpfi.pyx  Fri May 11 08:13:20 2007 -0700
> > @@ -1390,46 +1390,58 @@ cdef class RealIntervalFieldElement(sage
> >  #         mpfr_round(x.value, self.value)
> >  #         return x
> >
> > -#     def floor(self):
> > -#         """
> > -#         Returns the floor of this number
> > -
> > -#         EXAMPLES:
> > -#             sage: R = RealIntervalField()
> > -#             sage: (2.99).floor()
> > -#             2
> > -#             sage: (2.00).floor()
> > -#             2
> > -#             sage: floor(RR(-5/2))
> > -#             -3
> > -#         """
> > -#         cdef RealIntervalFieldElement x
> > -#         x = self._new()
> > -#         mpfr_floor(x.value, self.value)
> > -#         return x.integer_part()
> > -
> > -#     def ceil(self):
> > -#         """
> > -#         Returns the ceiling of this number
> > -
> > -#         OUTPUT:
> > -#             integer
> > -
> > -#         EXAMPLES:
> > -#             sage: (2.99).ceil()
> > -#             3
> > -#             sage: (2.00).ceil()
> > -#             2
> > -#             sage: (2.01).ceil()
> > -#             3
> > -#         """
> > -#         cdef RealIntervalFieldElement x
> > -#         x = self._new()
> > -#         mpfr_ceil(x.value, self.value)
> > -#         return x.integer_part()
> > -
> > -#     def ceiling(self):
> > -#         return self.ceil()
> > +    def floor(self):
> > +        """
> > +        Returns the floor of this number
> > +
> > +        EXAMPLES:
> > +            sage: R = RealIntervalField()
> > +            sage: (2.99).floor()
> > +            2
> > +            sage: (2.00).floor()
> > +            2
> > +            sage: floor(RR(-5/2))
> > +            -3
> > +            sage: R = RealIntervalField(100)
> > +            sage: a = R(9.5, 11.3); a
> > +            [9.5000000000000000000000000000000 .. 
> > 11.300000000000000710542735760101]
> > +            sage: floor(a)
> > +            9
> > +            sage: a.floor()
> > +            9
> > +            sage: ceil(a)
> > +            12
> > +            sage: a.ceil()
> > +            12
> > +        """
> > +        return self.lower().floor()
> > +
> > +    def ceil(self):
> > +        """
> > +        Returns the ceiling of this number
> > +
> > +        OUTPUT:
> > +            integer
> > +
> > +        EXAMPLES:
> > +            sage: (2.99).ceil()
> > +            3
> > +            sage: (2.00).ceil()
> > +            2
> > +            sage: (2.01).ceil()
> > +            3
> > +            sage: R = RealIntervalField(30)
> > +            sage: a = R(-9.5, -11.3); a
> > +            [-11.300000012 .. -9.5000000000]
> > +            sage: a.floor()
> > +            -12
> > +            sage: a.ceil()
> > +            -9
> > +        """
> > +        return self.upper().ceil()
> > +
> > +    def ceiling(self):
> > +         return self.ceil()
> >
> >  #     def trunc(self):
> >  #         """
>
>
> >
>


-- 
William Stein
Associate Professor of Mathematics
University of Washington
http://www.williamstein.org

--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/sage-devel
URLs: http://sage.scipy.org/sage/ and http://modular.math.washington.edu/sage/
-~----------~----~----~----~------~----~------~--~---

# HG changeset patch
# User William Stein <[EMAIL PROTECTED]>
# Date 1178898666 25200
# Node ID c6f86ef0019f1d8c26a21c08442c5ad130710201
# Parent  95b4bd205a125109be441683a7099087887103aa
make a "moral" improvement to real mpfi floor and ceiling.

diff -r 95b4bd205a12 -r c6f86ef0019f sage/rings/real_mpfi.pyx
--- a/sage/rings/real_mpfi.pyx  Fri May 11 08:16:38 2007 -0700
+++ b/sage/rings/real_mpfi.pyx  Fri May 11 08:51:06 2007 -0700
@@ -1406,15 +1406,15 @@ cdef class RealIntervalFieldElement(sage
             sage: a = R(9.5, 11.3); a
             [9.5000000000000000000000000000000 .. 
11.300000000000000710542735760101]
             sage: floor(a)
-            9
+            [9.0000000000000000000000000000000 .. 
11.000000000000000000000000000000]
             sage: a.floor()
-            9
+            [9.0000000000000000000000000000000 .. 
11.000000000000000000000000000000]
             sage: ceil(a)
-            12
+            [10.000000000000000000000000000000 .. 
12.000000000000000000000000000000]
             sage: a.ceil()
-            12            
-        """
-        return self.lower().floor()
+            [10.000000000000000000000000000000 .. 
12.000000000000000000000000000000]
+        """
+        return self.parent()(self.lower().floor(), self.upper().floor())
 
     def ceil(self):
         """
@@ -1434,11 +1434,13 @@ cdef class RealIntervalFieldElement(sage
             sage: a = R(-9.5, -11.3); a
             [-11.300000012 .. -9.5000000000]
             sage: a.floor()
-            -12
+            [-12.000000000 .. -10.000000000]
             sage: a.ceil()
-            -9            
-        """
-        return self.upper().ceil()
+            [-11.000000000 .. -9.0000000000]
+            sage: ceil(a)
+            [-11.000000000 .. -9.0000000000]
+        """
+        return self.parent()(self.lower().ceil(), self.upper().ceil())
     
     def ceiling(self):
          return self.ceil()

Reply via email to