<commit_before>package org.exist.xquery.value;

import java.text.Collator;

import org.exist.xquery.Constants;
import org.exist.xquery.XPathException;

public abstract class NumericValue extends ComputableValue {

	public abstract String getStringValue() throws XPathException;

	public abstract AtomicValue convertTo(int requiredType) throws XPathException;
	
	public double getDouble() throws XPathException {
		return ((DoubleValue)convertTo(Type.DOUBLE)).getValue();
	}
	
	public long getLong() throws XPathException {
		return ((IntegerValue)convertTo(Type.INTEGER)).getValue();
	}
	
	public int getInt() throws XPathException {
		return (int)((IntegerValue)convertTo(Type.INTEGER)).getValue();
	}
	
	public abstract boolean isNaN();

	public abstract boolean isInfinite();

	public abstract boolean isZero();
	
	/* (non-Javadoc)
	 * @see org.exist.xquery.value.AtomicValue#compareTo(int, org.exist.xquery.value.AtomicValue)
	 */
	public boolean compareTo(Collator collator, int operator, AtomicValue other)
		throws XPathException {
		if (other.isEmpty())
			return false;
		if(Type.subTypeOf(other.getType(), Type.NUMBER)) {
			double otherVal = ((NumericValue)other).getDouble();
			double val = getDouble();
			switch(operator) {
				case Constants.EQ:
					return val == otherVal;
				case Constants.NEQ:
					return val != otherVal;
				case Constants.GT:
					return val > otherVal;
				case Constants.GTEQ:
					return val >= otherVal;
				case Constants.LT:
					return val < otherVal;
				case Constants.LTEQ:
					return val <= otherVal;
				default:
					throw new XPathException("Type error: cannot apply operator to numeric value");
			}
		}
		throw new XPathException("Type error: cannot compare operands: " + 
			Type.getTypeName(getType()) + " and " + 
			Type.getTypeName(other.getType()));
	}
	
	/* (non-Javadoc)
	 * @see org.exist.xquery.value.AtomicValue#compareTo(org.exist.xquery.value.AtomicValue)
	 */
	public int compareTo(Collator collator, AtomicValue other) throws XPathException {
		if(Type.subTypeOf(other.getType(), Type.NUMBER)) {
			double otherVal = ((NumericValue)other).getDouble();
			double val = getDouble();
			if(val == otherVal)
				return Constants.EQUAL;
			else if(val > otherVal)
				return Constants.SUPERIOR;
			else
				return Constants.INFERIOR;<commit_msg>Just like the empty sequence, NaN is never equal to itself<commit_after>package org.exist.xquery.value;

import java.text.Collator;

import org.exist.xquery.Constants;
import org.exist.xquery.XPathException;

public abstract class NumericValue extends ComputableValue {

	public abstract String getStringValue() throws XPathException;

	public abstract AtomicValue convertTo(int requiredType) throws XPathException;
	
	public double getDouble() throws XPathException {
		return ((DoubleValue)convertTo(Type.DOUBLE)).getValue();
	}
	
	public long getLong() throws XPathException {
		return ((IntegerValue)convertTo(Type.INTEGER)).getValue();
	}
	
	public int getInt() throws XPathException {
		return (int)((IntegerValue)convertTo(Type.INTEGER)).getValue();
	}
	
	public abstract boolean isNaN();

	public abstract boolean isInfinite();

	public abstract boolean isZero();
	
	/* (non-Javadoc)
	 * @see org.exist.xquery.value.AtomicValue#compareTo(int, org.exist.xquery.value.AtomicValue)
	 */
	public boolean compareTo(Collator collator, int operator, AtomicValue other)
		throws XPathException {
		if (other.isEmpty())
			return false;
		if(Type.subTypeOf(other.getType(), Type.NUMBER)) {
			if (isNaN()) {
				//NaN does not equal itself.
				if (((NumericValue)other).isNaN())
				return false;
			}			
			double otherVal = ((NumericValue)other).getDouble();
			double val = getDouble();
			switch(operator) {
				case Constants.EQ:
					return val == otherVal;
				case Constants.NEQ:
					return val != otherVal;
				case Constants.GT:
					return val > otherVal;
				case Constants.GTEQ:
					return val >= otherVal;
				case Constants.LT:
					return val < otherVal;
				case Constants.LTEQ:
					return val <= otherVal;
				default:
					throw new XPathException("Type error: cannot apply operator to numeric value");
			}
		}
		throw new XPathException("Type error: cannot compare operands: " + 
			Type.getTypeName(getType()) + " and " + 
			Type.getTypeName(other.getType()));
	}
	
	/* (non-Javadoc)
	 * @see org.exist.xquery.value.AtomicValue#compareTo(org.exist.xquery.value.AtomicValue)
	 */
	public int compareTo(Collator collator, AtomicValue other) throws XPathException {
		if(Type.subTypeOf(other.getType(), Type.NUMBER)) {
			if (isNaN()) {
				//NaN does not equal itself.
				if (((NumericValue)other).isNaN())
				return Constants.INFERIOR;
			}
			double otherVal = ((NumericValue)other).getDouble();
			double val = getDouble();
			if(val == otherVal)
				return Constants.EQUAL;
			else if(val > otherVal)
				return Constants.SUPERIOR;
			else
				return Constants.INFERIOR;
{'fraction_non_alphanumeric': 0.11479747608385915, 'fraction_numerical': 0.0, 'mean_word_length': 4.081695966907962, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6531094', 'n_tokens_mistral': 1746, 'n_tokens_neox': 1492, 'n_words': 373}