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