class RDF::Literal::Numeric
Shared methods and class ancestry for numeric literal classes.
@since 0.3.0
Public Instance Methods
Returns the product of `self` times `other`.
@param [Literal::Numeric, to_i, to_f, to_d] other @return [RDF::Literal::Numeric] @since 0.2.3 @see www.w3.org/TR/xpath-functions/#func-numeric-multiply
# File lib/rdf/model/literal/numeric.rb, line 114 def *(other) if self.class == Double || [Double, ::Float].include?(other.class) RDF::Literal::Double.new(to_f * other.to_f) elsif ((self.class == RDF::Literal::Float || other.class == RDF::Literal::Float) rescue false) RDF::Literal::Float.new(to_f * other.to_f) elsif self.class == Decimal || other.class == Decimal RDF::Literal::Decimal.new(to_d * (other.respond_to?(:to_d) ? other.to_d : BigDecimal(other.to_s))) else RDF::Literal::Integer.new(to_i * other.to_i) end end
Returns the sum of `self` plus `other`.
For xs:float or xs:double values, if one of the operands is a zero or a finite number and the other is INF or -INF, INF or -INF is returned. If both operands are INF, INF is returned. If both operands are -INF, -INF is returned. If one of the operands is INF and the other is -INF, NaN is returned. @param [Literal::Numeric, to_i, to_f, to_d] other @return [RDF::Literal::Numeric] @since 0.2.3 @see www.w3.org/TR/xpath-functions/#func-numeric-add
# File lib/rdf/model/literal/numeric.rb, line 76 def +(other) if self.class == Double || [Double, ::Float].include?(other.class) RDF::Literal::Double.new(to_f + other.to_f) elsif ((self.class == RDF::Literal::Float || other.class == RDF::Literal::Float) rescue false) RDF::Literal::Float.new(to_f + other.to_f) elsif self.class == Decimal || other.class == Decimal RDF::Literal::Decimal.new(to_d + (other.respond_to?(:to_d) ? other.to_d : BigDecimal(other.to_s))) else RDF::Literal::Integer.new(to_i + other.to_i) end end
Returns `self`.
@return [RDF::Literal::Numeric] @since 0.2.3
# File lib/rdf/model/literal/numeric.rb, line 52 def +@ self # unary plus end
Returns the difference of `self` minus `other`.
@param [Literal::Numeric, to_i, to_f, to_d] other @return [RDF::Literal::Numeric] @since 0.2.3 @see www.w3.org/TR/xpath-functions/#func-numeric-subtract
# File lib/rdf/model/literal/numeric.rb, line 95 def -(other) if self.class == Double || [Double, ::Float].include?(other.class) RDF::Literal::Double.new(to_f - other.to_f) elsif ((self.class == RDF::Literal::Float || other.class == RDF::Literal::Float) rescue false) RDF::Literal::Float.new(to_f - other.to_f) elsif self.class == Decimal || other.class == Decimal RDF::Literal::Decimal.new(to_d - (other.respond_to?(:to_d) ? other.to_d : BigDecimal(other.to_s))) else RDF::Literal::Integer.new(to_i - other.to_i) end end
Returns `self` negated.
@return [RDF::Literal::Numeric] @since 0.2.3
# File lib/rdf/model/literal/numeric.rb, line 61 def -@ self.class.new(-self.object) end
Returns the quotient of `self` divided by `other`.
As a special case, if the types of both $arg1 and $arg2 are xsd:integer, then the return type is xsd:decimal.
@param [Literal::Numeric, to_i, to_f, to_d] other @return [RDF::Literal::Numeric] @raise [ZeroDivisionError] if divided by zero @since 0.2.3 @see www.w3.org/TR/xpath-functions/#func-numeric-divide
# File lib/rdf/model/literal/numeric.rb, line 137 def /(other) if self.class == Double || [Double, ::Float].include?(other.class) RDF::Literal::Double.new(to_f / other.to_f) elsif ((self.class == RDF::Literal::Float || other.class == RDF::Literal::Float) rescue false) RDF::Literal::Float.new(to_f / other.to_f) else RDF::Literal::Decimal.new(to_d / (other.respond_to?(:to_d) ? other.to_d : BigDecimal(other.to_s))) end end
Compares this literal to `other` for sorting purposes.
@param [Object] other @return [Integer] `-1`, `0`, or `1` @since 0.3.0
# File lib/rdf/model/literal/numeric.rb, line 13 def <=>(other) case other when ::Numeric to_d <=> other when Double to_f <=> other.to_f when Numeric to_d <=> other.to_d else super end end
Returns `true` if this literal is equal to `other`.
@param [Object] other @return [Boolean] `true` or `false` @since 0.3.0
# File lib/rdf/model/literal/numeric.rb, line 31 def ==(other) # If lexically invalid, use regular literal testing return super unless self.valid? case other when Literal::Numeric return super unless other.valid? (cmp = (self <=> other)) ? cmp.zero? : false when RDF::URI, RDF::Node # Interpreting SPARQL data-r2/expr-equal/eq-2-2, numeric can't be compared with other types type_error("unable to determine whether #{self.inspect} and #{other.inspect} are equivalent") else super end end
Returns the absolute value of `self`.
@return [RDF::Literal] @raise [NotImplementedError] unless implemented in subclass
# File lib/rdf/model/literal/numeric.rb, line 152 def abs raise NotImplementedError end
Returns the smallest integer greater than or equal to `self`.
@example
RDF::Literal(1).ceil #=> RDF::Literal(1)
@return [RDF::Literal]
# File lib/rdf/model/literal/numeric.rb, line 172 def ceil self end
Returns the largest integer less than or equal to `self`.
@example
RDF::Literal(1).floor #=> RDF::Literal(1)
@return [RDF::Literal]
# File lib/rdf/model/literal/numeric.rb, line 183 def floor self end
Returns the number with no fractional part that is closest to the argument. If there are two such numbers, then the one that is closest to positive infinity is returned. An error is raised if arg is not a numeric value.
@return [RDF::Literal] @raise [NotImplementedError] unless implemented in subclass
# File lib/rdf/model/literal/numeric.rb, line 161 def round raise NotImplementedError end
Returns the value as a decimal number.
@return [BigDecimal]
# File lib/rdf/model/literal/numeric.rb, line 212 def to_d @object.respond_to?(:to_d) ? @object.to_d : BigDecimal(@object.to_s) rescue FloatDomainError ::Float::NAN end
Returns the value as a floating point number.
The usual accuracy limits and errors of binary float arithmetic apply.
@return [Float] @see BigDecimal#to_f
# File lib/rdf/model/literal/numeric.rb, line 204 def to_f @object.to_f end
Returns the value as an integer.
@return [Integer]
# File lib/rdf/model/literal/numeric.rb, line 191 def to_i @object.to_i end
Returns the value as a rational number.
@return [Rational]
# File lib/rdf/model/literal/numeric.rb, line 222 def to_r @object.to_r end