Sha256: 9c51a76be01eaef1357c2a25a1b61d5b706e336a913c0bab6c14cce0b83cf3b3

Contents?: true

Size: 1.51 KB

Versions: 42

Compression:

Stored size: 1.51 KB

Contents

module SPARQL; module Algebra
  class Operator
    ##
    # The SPARQL numeric `divide` operator.
    #
    # @example
    #   (/ 4 2)
    #
    # @see http://www.w3.org/TR/xpath-functions/#func-numeric-divide
    class Divide < Operator::Binary
      include Evaluatable

      NAME = [:'/', :divide]

      ##
      # Returns the arithmetic quotient of the operands.
      #
      # @param  [RDF::Literal::Numeric] left
      #   a numeric literal
      # @param  [RDF::Literal::Numeric] right
      #   a numeric literal
      # @return [RDF::Literal::Numeric]
      # @raise  [TypeError] if either operand is not a numeric literal
      def apply(left, right)
        case
          when left.is_a?(RDF::Literal::Numeric) && right.is_a?(RDF::Literal::Numeric)
            # For xsd:decimal and xsd:integer operands, if the divisor is
            # (positive or negative) zero, an error is raised.
            raise ZeroDivisionError, "divided by #{right}" if left.is_a?(RDF::Literal::Decimal) && right.zero?

            # As a special case, if the types of both operands are
            # xsd:integer, then the return type is xsd:decimal.
            if left.is_a?(RDF::Literal::Integer) && right.is_a?(RDF::Literal::Integer)
              RDF::Literal(left.to_d / right.to_d)
            else
              left / right
            end
          else raise TypeError, "expected two RDF::Literal::Numeric operands, but got #{left.inspect} and #{right.inspect}"
        end
      end
    end # Divide
  end # Operator
end; end # SPARQL::Algebra

Version data entries

42 entries across 42 versions & 1 rubygems

Version Path
sparql-3.1.0 lib/sparql/algebra/operator/divide.rb
sparql-3.0.2 lib/sparql/algebra/operator/divide.rb
sparql-3.0.1 lib/sparql/algebra/operator/divide.rb
sparql-3.0.0 lib/sparql/algebra/operator/divide.rb
sparql-2.2.2 lib/sparql/algebra/operator/divide.rb
sparql-2.2.1 lib/sparql/algebra/operator/divide.rb
sparql-2.2.0 lib/sparql/algebra/operator/divide.rb
sparql-2.1.0 lib/sparql/algebra/operator/divide.rb
sparql-2.0.0 lib/sparql/algebra/operator/divide.rb
sparql-2.0.0.beta2 lib/sparql/algebra/operator/divide.rb
sparql-1.99.1 lib/sparql/algebra/operator/divide.rb
sparql-2.0.0.beta1 lib/sparql/algebra/operator/divide.rb
sparql-1.1.9.1 lib/sparql/algebra/operator/divide.rb
sparql-1.99.0 lib/sparql/algebra/operator/divide.rb
sparql-1.1.9 lib/sparql/algebra/operator/divide.rb
sparql-1.1.8 lib/sparql/algebra/operator/divide.rb
sparql-1.1.7 lib/sparql/algebra/operator/divide.rb
sparql-1.1.6 lib/sparql/algebra/operator/divide.rb
sparql-1.1.5 lib/sparql/algebra/operator/divide.rb
sparql-1.1.4 lib/sparql/algebra/operator/divide.rb