| 1 |
/* |
| 2 |
* Copyright (C) 2013-2015, 2019 by the Konclude Developer Team. |
| 3 |
* |
| 4 |
* This file is part of the reasoning system Konclude. |
| 5 |
* For details and support, see <http://konclude.com/>. |
| 6 |
* |
| 7 |
* Konclude is free software: you can redistribute it and/or modify |
| 8 |
* it under the terms of version 3 of the GNU Lesser General Public |
| 9 |
* License (LGPLv3) as published by the Free Software Foundation. |
| 10 |
* |
| 11 |
* Konclude is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
* GNU (Lesser) General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU (Lesser) General Public |
| 17 |
* License along with Konclude. If not, see <http://www.gnu.org/licenses/>. |
| 18 |
* |
| 19 |
*/ |
| 20 |
|
| 21 |
#include "CDatatypeDoubleValueSpaceMap.h" |
| 22 |
|
| 23 |
|
| 24 |
namespace Konclude { |
| 25 |
|
| 26 |
namespace Reasoner { |
| 27 |
|
| 28 |
namespace Kernel { |
| 29 |
|
| 30 |
namespace Process { |
| 31 |
|
| 32 |
|
| 33 |
CDatatypeDoubleValueSpaceMap::CDatatypeDoubleValueSpaceMap(CProcessContext* processContext) : CDatatypeCompareValueSpaceMap(processContext) { |
| 34 |
} |
| 35 |
|
| 36 |
bool CDatatypeDoubleValueSpaceMap::representsInfinitelyManyValues(CDataLiteralCompareValue* leftValueExcluded, CDataLiteralCompareValue* rightValueExcluded) { |
| 37 |
return false; |
| 38 |
} |
| 39 |
|
| 40 |
bool CDatatypeDoubleValueSpaceMap::representsInfinitelyManyValues(CDataLiteralCompareValue* value) { |
| 41 |
return false; |
| 42 |
} |
| 43 |
|
| 44 |
bool CDatatypeDoubleValueSpaceMap::getValueCopy(CDataLiteralCompareValue* valueCopyInto, CDataLiteralCompareValue* valueCopyFrom) { |
| 45 |
CDataLiteralDoubleValue* doubleValueCopyInto = dynamic_cast<CDataLiteralDoubleValue*>(valueCopyInto); |
| 46 |
CDataLiteralDoubleValue* doubleValueCopyFrom = dynamic_cast<CDataLiteralDoubleValue*>(valueCopyFrom); |
| 47 |
doubleValueCopyInto->initValue(doubleValueCopyFrom); |
| 48 |
return true; |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
bool CDatatypeDoubleValueSpaceMap::isNaN(cint64 bits) { |
| 53 |
return ((bits & Q_UINT64_C(0x7ff0000000000000)) == Q_UINT64_C(0x7ff0000000000000)) && ((bits & Q_UINT64_C(0x000fffffffffffff)) != 0); |
| 54 |
} |
| 55 |
|
| 56 |
bool CDatatypeDoubleValueSpaceMap::getValueNext(CDataLiteralCompareValue* valueNext, CDataLiteralCompareValue* valueLast) { |
| 57 |
CDataLiteralDoubleValue* doubleValueNext = dynamic_cast<CDataLiteralDoubleValue*>(valueNext); |
| 58 |
CDataLiteralDoubleValue* doubleValueLast = dynamic_cast<CDataLiteralDoubleValue*>(valueLast); |
| 59 |
|
| 60 |
|
| 61 |
double nextDoubleValue = 0.; |
| 62 |
double lastDoubleValue = doubleValueLast->getDouble(); |
| 63 |
cint64 bits = *reinterpret_cast<cint64*>(&lastDoubleValue); |
| 64 |
cint64 magnitude = (bits & Q_UINT64_C(0x7fffffffffffffff)); |
| 65 |
bool positive = ((bits & Q_UINT64_C(0x8000000000000000))==0); |
| 66 |
// The successors of NaN and +INF are these numbers themselves. |
| 67 |
if (isNaN(bits) || (magnitude == Q_UINT64_C(0x7ff0000000000000) && positive)) { |
| 68 |
doubleValueNext->initValueFromDouble(lastDoubleValue); |
| 69 |
} else { |
| 70 |
bool newPositive; |
| 71 |
cint64 newMagnitude; |
| 72 |
if (positive) { |
| 73 |
newPositive = true; |
| 74 |
newMagnitude = magnitude+1; |
| 75 |
} else if (!positive && magnitude == 0) { |
| 76 |
// The successor of -0 is +0 |
| 77 |
newPositive = true; |
| 78 |
newMagnitude = 1; // skip +0 for now |
| 79 |
} else { // if (!positive && magnitude != 0) |
| 80 |
newPositive = false; |
| 81 |
newMagnitude = magnitude-1; |
| 82 |
} |
| 83 |
cint64 newBits = newMagnitude | (newPositive ? 0 : Q_UINT64_C(0x8000000000000000)); |
| 84 |
nextDoubleValue = *reinterpret_cast<double*>(&newBits); |
| 85 |
doubleValueNext->initValueFromDouble(nextDoubleValue); |
| 86 |
return true; |
| 87 |
} |
| 88 |
return false; |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
cuint64 CDatatypeDoubleValueSpaceMap::getIntervalValueCount(CDataLiteralCompareValue* leftValueExcluded, CDataLiteralCompareValue* rightValueExcluded) { |
| 93 |
CDataLiteralDoubleValue* doubleValueLeft = dynamic_cast<CDataLiteralDoubleValue*>(leftValueExcluded); |
| 94 |
CDataLiteralDoubleValue* doubleValueRight = dynamic_cast<CDataLiteralDoubleValue*>(rightValueExcluded); |
| 95 |
|
| 96 |
|
| 97 |
double leftDoubleValue = doubleValueLeft->getDouble(); |
| 98 |
double rightDoubleValue = doubleValueRight->getDouble(); |
| 99 |
|
| 100 |
cint64 bitsLowerBoundExclusive = *reinterpret_cast<cint64*>(&leftDoubleValue); |
| 101 |
cint64 bitsUpperBoundExclusive = *reinterpret_cast<cint64*>(&rightDoubleValue); |
| 102 |
if (isNaN(bitsLowerBoundExclusive) || isNaN(bitsUpperBoundExclusive)) |
| 103 |
return 0; |
| 104 |
|
| 105 |
cuint64 valueCount = 0; |
| 106 |
|
| 107 |
bool positiveLowerBoundExclusive = ((bitsLowerBoundExclusive & Q_UINT64_C(0x8000000000000000))==0); |
| 108 |
bool positiveUpperBoundExclusive = ((bitsUpperBoundExclusive & Q_UINT64_C(0x8000000000000000))==0); |
| 109 |
|
| 110 |
cint64 magnitudeLowerBoundExclusive = (bitsLowerBoundExclusive & Q_UINT64_C(0x7fffffffffffffff)); |
| 111 |
cint64 magnitudeUpperBoundExclusive = (bitsUpperBoundExclusive & Q_UINT64_C(0x7fffffffffffffff)); |
| 112 |
|
| 113 |
// Determine the number of elements. This works even if 'lowerBoundExclusive' or 'upperBoundExclusive' is +INF or -INF |
| 114 |
if (positiveLowerBoundExclusive && positiveUpperBoundExclusive) { |
| 115 |
// it must be that magnitudeLowerBoundExclusive < magnitudeUpperBoundExclusive |
| 116 |
valueCount = magnitudeUpperBoundExclusive-magnitudeLowerBoundExclusive-1; |
| 117 |
|
| 118 |
} else if (!positiveLowerBoundExclusive && !positiveUpperBoundExclusive) { |
| 119 |
// it must be that magnitudeUpperBoundExclusive < magnitudeLowerBoundExclusive |
| 120 |
valueCount = magnitudeLowerBoundExclusive-magnitudeUpperBoundExclusive-1; |
| 121 |
|
| 122 |
} else if (!positiveLowerBoundExclusive && positiveUpperBoundExclusive) { |
| 123 |
// the number of values from 'lowerBoundExclusive' to -0 |
| 124 |
cuint64 startToMinusZero = magnitudeLowerBoundExclusive; |
| 125 |
|
| 126 |
// the number of values from +0 to 'upperBoundExclusive' |
| 127 |
cint64 plusZeroToEnd = magnitudeUpperBoundExclusive; |
| 128 |
valueCount = startToMinusZero+plusZeroToEnd -1; // extra -1 to count +0/-0 only once for now |
| 129 |
} |
| 130 |
|
| 131 |
return valueCount; |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
}; // end namespace Process |
| 137 |
|
| 138 |
}; // end namespace Kernel |
| 139 |
|
| 140 |
}; // end namespace Reasoner |
| 141 |
|
| 142 |
}; // end namespace Konclude |