// Copyright (c) 2006-2008 Fernando Luis Cacciola Carballal. All rights reserved. // // This file is part of CGAL (www.cgal.org); you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public License as // published by the Free Software Foundation; either version 3 of the License, // or (at your option) any later version. // // Licensees holding a valid commercial license may use this file in // accordance with the commercial license agreement provided with the software. // // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. // // $URL$ // $Id$ // SPDX-License-Identifier: LGPL-3.0+ // // Author(s) : Fernando Cacciola // #ifndef CGAL_CERTIFIED_QUOTIENT_PREDICATES_H #define CGAL_CERTIFIED_QUOTIENT_PREDICATES_H #include #include namespace CGAL { template inline Uncertain certified_quotient_is_positive(const Quotient& x) { Uncertain signum = CGAL_NTS certified_sign(x.num) ; Uncertain sigden = CGAL_NTS certified_sign(x.den) ; Uncertain zero(ZERO); return ( signum != zero ) & ( signum == sigden ); } template inline Uncertain certified_quotient_is_negative(const Quotient& x) { Uncertain signum = CGAL_NTS certified_sign(x.num) ; Uncertain sigden = CGAL_NTS certified_sign(x.den) ; Uncertain zero(ZERO); return ( signum != zero ) & ( signum != sigden ); } template inline Uncertain certified_quotient_is_zero(const Quotient& x) { return CGAL_NTS certified_is_zero(x.num) ; } template CGAL_MEDIUM_INLINE Uncertain certified_quotient_sign(const Quotient& x) { // No assumptions on the sign of den are made return CGAL_NTS certified_sign(x.num) * CGAL_NTS certified_sign(x.den); } template CGAL_MEDIUM_INLINE Uncertain certified_quotient_compare(const Quotient& x, const Quotient& y) { Uncertain r = Uncertain::indeterminate(); // No assumptions on the sign of den are made // code assumes that SMALLER == - 1; CGAL_precondition( SMALLER == static_cast(-1) ); Uncertain xnumsign = CGAL_NTS certified_sign(x.num) ; Uncertain xdensign = CGAL_NTS certified_sign(x.den) ; Uncertain ynumsign = CGAL_NTS certified_sign(y.num) ; Uncertain ydensign = CGAL_NTS certified_sign(y.den) ; if ( is_certain(xnumsign) && is_certain(xdensign) && is_certain(ynumsign) && is_certain(ydensign) ) { int xsign = xnumsign * xdensign ; int ysign = ynumsign * ydensign ; if (xsign == 0) return static_cast(-ysign); if (ysign == 0) return static_cast(xsign); // now (x != 0) && (y != 0) int diff = xsign - ysign; if (diff == 0) { int msign = xdensign * ydensign; NT1 leftop = x.num * y.den * msign; NT1 rightop = y.num * x.den * msign; r = certified_compare(leftop, rightop); } else { r = (xsign < ysign) ? SMALLER : LARGER; } } return r ; } template inline Uncertain certified_is_zero(const Quotient& n) { return certified_quotient_is_zero(n); } template inline Uncertain certified_is_positive(const Quotient& n) { return certified_quotient_is_positive(n); } template inline Uncertain certified_is_negative(const Quotient& n) { return certified_quotient_is_negative(n); } template inline Uncertain certified_sign(const Quotient& n) { return certified_quotient_sign(n); } template inline Uncertain certified_compare(const Quotient& n1, const Quotient& n2) { return certified_quotient_compare(n1,n2); } } // end namespace CGAL #endif // CGAL_CERTIFIED_QUOTIENT_PREDICATES_H