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 : : { 26 : 0 : return os << "(" << dq.getNoninfinitesimalPart() << "," 27 : 0 : << dq.getInfinitesimalPart() << ")"; 28 : : } 29 : : 30 : 0 : std::string DeltaRational::toString() const 31 : : { 32 : 0 : return "(" + getNoninfinitesimalPart().toString() + "," 33 : 0 : + getInfinitesimalPart().toString() + ")"; 34 : : } 35 : : 36 : 1034349 : void DeltaRational::seperatingDelta(Rational& res, 37 : : const DeltaRational& a, 38 : : const DeltaRational& b) 39 : : { 40 [ - + ][ - + ]: 1034349 : Assert(res.sgn() > 0); [ - - ] 41 : : 42 : 1034349 : int cmp = a.cmp(b); 43 [ + - ]: 1034349 : if (cmp != 0) 44 : : { 45 : 1034349 : bool aLeqB = cmp < 0; 46 : : 47 [ + - ]: 1034349 : const DeltaRational& min = aLeqB ? a : b; 48 [ + - ]: 1034349 : const DeltaRational& max = aLeqB ? b : a; 49 : : 50 : 1034349 : const Rational& pinf = min.getInfinitesimalPart(); 51 : 1034349 : const Rational& cinf = max.getInfinitesimalPart(); 52 : : 53 : 1034349 : const Rational& pmaj = min.getNoninfinitesimalPart(); 54 : 1034349 : const Rational& cmaj = max.getNoninfinitesimalPart(); 55 : : 56 [ + + ]: 1034349 : if (pmaj == cmaj) 57 : : { 58 [ - + ][ - + ]: 34977 : Assert(pinf < cinf); [ - - ] 59 : : // any value of delta preserves the order 60 : : } 61 [ + + ]: 999372 : else if (pinf == cinf) 62 : : { 63 [ - + ][ - + ]: 938890 : Assert(pmaj < cmaj); [ - - ] 64 : : // any value of delta preserves the order 65 : : } 66 : : else 67 : : { 68 [ + - ][ + - ]: 60482 : Assert(pinf != cinf && pmaj != cmaj); [ - + ][ - + ] [ - - ] 69 : 60482 : Rational denDiffAbs = (cinf - pinf).abs(); 70 : : 71 : 60482 : Rational numDiff = (cmaj - pmaj); 72 [ - + ][ - + ]: 60482 : Assert(numDiff.sgn() >= 0); [ - - ] 73 [ - + ][ - + ]: 60482 : Assert(denDiffAbs.sgn() > 0); [ - - ] 74 : 60482 : Rational ratio = numDiff / denDiffAbs; 75 [ - + ][ - + ]: 60482 : Assert(ratio.sgn() > 0); [ - - ] 76 : : 77 [ + + ]: 60482 : if (ratio < res) 78 : : { 79 : 12137 : res = ratio; 80 : : } 81 : 60482 : } 82 : : } 83 : 1034349 : } 84 : : 85 : 0 : DeltaRationalException::DeltaRationalException(const char* op, 86 : : const DeltaRational& a, 87 : 0 : const DeltaRational& b) 88 : : { 89 : 0 : std::stringstream ss; 90 : 0 : ss << "Operation [" << op << "] between DeltaRational values "; 91 : 0 : ss << a << " and " << b << " is not a DeltaRational."; 92 : 0 : setMessage(ss.str()); 93 : 0 : } 94 : : 95 : 0 : DeltaRationalException::~DeltaRationalException() {} 96 : 0 : Integer DeltaRational::euclidianDivideQuotient(const DeltaRational& y) const 97 : : { 98 [ - - ][ - - ]: 0 : if (isIntegral() && y.isIntegral()) [ - - ] 99 : : { 100 : 0 : Integer ti = floor(); 101 : 0 : Integer yi = y.floor(); 102 : 0 : return ti.euclidianDivideQuotient(yi); 103 : 0 : } 104 : : else 105 : : { 106 : 0 : throw DeltaRationalException("euclidianDivideQuotient", *this, y); 107 : : } 108 : : } 109 : : 110 : 0 : Integer DeltaRational::euclidianDivideRemainder(const DeltaRational& y) const 111 : : { 112 [ - - ][ - - ]: 0 : if (isIntegral() && y.isIntegral()) [ - - ] 113 : : { 114 : 0 : Integer ti = floor(); 115 : 0 : Integer yi = y.floor(); 116 : 0 : return ti.euclidianDivideRemainder(yi); 117 : 0 : } 118 : : else 119 : : { 120 : 0 : throw DeltaRationalException("euclidianDivideRemainder", *this, y); 121 : : } 122 : : } 123 : : 124 : : } // namespace cvc5::internal