Branch data Line data Source code
1 : : /****************************************************************************** 2 : : * This file is part of the cvc5 project. 3 : : * 4 : : * Copyright (c) 2009-2026 by the authors listed in the file AUTHORS 5 : : * in the top-level source directory and their institutional affiliations. 6 : : * All rights reserved. See the file COPYING in the top-level source 7 : : * directory for licensing information. 8 : : * **************************************************************************** 9 : : * 10 : : * [[ Add one-line brief description here ]] 11 : : * 12 : : * [[ Add lengthier description here ]] 13 : : * \todo document this file 14 : : */ 15 : : 16 : : #include "theory/arith/delta_rational.h" 17 : : 18 : : #include <sstream> 19 : : 20 : : using namespace std; 21 : : 22 : : namespace cvc5::internal { 23 : : 24 : 0 : std::ostream& operator<<(std::ostream& os, const DeltaRational& dq){ 25 : 0 : return os << "(" << dq.getNoninfinitesimalPart() 26 : 0 : << "," << dq.getInfinitesimalPart() << ")"; 27 : : } 28 : : 29 : : 30 : 0 : std::string DeltaRational::toString() const { 31 : 0 : return "(" + getNoninfinitesimalPart().toString() + "," + 32 : 0 : getInfinitesimalPart().toString() + ")"; 33 : : } 34 : : 35 : 1016116 : void DeltaRational::seperatingDelta(Rational& res, const DeltaRational& a, const DeltaRational& b){ 36 [ - + ][ - + ]: 1016116 : Assert(res.sgn() > 0); [ - - ] 37 : : 38 : 1016116 : int cmp = a.cmp(b); 39 [ + - ]: 1016116 : if(cmp != 0){ 40 : 1016116 : bool aLeqB = cmp < 0; 41 : : 42 [ + - ]: 1016116 : const DeltaRational& min = aLeqB ? a : b; 43 [ + - ]: 1016116 : const DeltaRational& max = aLeqB ? b : a; 44 : : 45 : 1016116 : const Rational& pinf = min.getInfinitesimalPart(); 46 : 1016116 : const Rational& cinf = max.getInfinitesimalPart(); 47 : : 48 : 1016116 : const Rational& pmaj = min.getNoninfinitesimalPart(); 49 : 1016116 : const Rational& cmaj = max.getNoninfinitesimalPart(); 50 : : 51 [ + + ]: 1016116 : if(pmaj == cmaj){ 52 [ - + ][ - + ]: 33114 : Assert(pinf < cinf); [ - - ] 53 : : // any value of delta preserves the order 54 [ + + ]: 983002 : }else if(pinf == cinf){ 55 [ - + ][ - + ]: 925626 : Assert(pmaj < cmaj); [ - - ] 56 : : // any value of delta preserves the order 57 : : }else{ 58 [ + - ][ + - ]: 57376 : Assert(pinf != cinf && pmaj != cmaj); [ - + ][ - + ] [ - - ] 59 : 57376 : Rational denDiffAbs = (cinf - pinf).abs(); 60 : : 61 : 57376 : Rational numDiff = (cmaj - pmaj); 62 [ - + ][ - + ]: 57376 : Assert(numDiff.sgn() >= 0); [ - - ] 63 [ - + ][ - + ]: 57376 : Assert(denDiffAbs.sgn() > 0); [ - - ] 64 : 57376 : Rational ratio = numDiff / denDiffAbs; 65 [ - + ][ - + ]: 57376 : Assert(ratio.sgn() > 0); [ - - ] 66 : : 67 [ + + ]: 57376 : if(ratio < res){ 68 : 11401 : res = ratio; 69 : : } 70 : 57376 : } 71 : : } 72 : 1016116 : } 73 : : 74 : 0 : DeltaRationalException::DeltaRationalException(const char* op, 75 : : const DeltaRational& a, 76 : 0 : const DeltaRational& b) 77 : : { 78 : 0 : std::stringstream ss; 79 : 0 : ss << "Operation [" << op << "] between DeltaRational values "; 80 : 0 : ss << a << " and " << b << " is not a DeltaRational."; 81 : 0 : setMessage(ss.str()); 82 : 0 : } 83 : : 84 : 0 : DeltaRationalException::~DeltaRationalException() {} 85 : 0 : Integer DeltaRational::euclidianDivideQuotient(const DeltaRational& y) const 86 : : { 87 [ - - ][ - - ]: 0 : if(isIntegral() && y.isIntegral()){ [ - - ] 88 : 0 : Integer ti = floor(); 89 : 0 : Integer yi = y.floor(); 90 : 0 : return ti.euclidianDivideQuotient(yi); 91 : 0 : }else{ 92 : 0 : throw DeltaRationalException("euclidianDivideQuotient", *this, y); 93 : : } 94 : : } 95 : : 96 : 0 : Integer DeltaRational::euclidianDivideRemainder(const DeltaRational& y) const 97 : : { 98 [ - - ][ - - ]: 0 : if(isIntegral() && y.isIntegral()){ [ - - ] 99 : 0 : Integer ti = floor(); 100 : 0 : Integer yi = y.floor(); 101 : 0 : return ti.euclidianDivideRemainder(yi); 102 : 0 : }else{ 103 : 0 : throw DeltaRationalException("euclidianDivideRemainder", *this, y); 104 : : } 105 : : } 106 : : 107 : : } // namespace cvc5::internal