Class: RDF::Literal::Numeric

Inherits:
RDF::Literal show all
Defined in:
vendor/bundler/ruby/3.0.0/bundler/gems/rdf-237066655558/lib/rdf/model/literal/numeric.rb

Overview

Shared methods and class ancestry for numeric literal classes.

Since:

  • 0.3.0

Direct Known Subclasses

Decimal, Double

Constant Summary

Constants inherited from RDF::Literal

FALSE, TRUE, ZERO

Constants included from N3::Terminals

N3::Terminals::ANON, N3::Terminals::BASE, N3::Terminals::BLANK_NODE_LABEL, N3::Terminals::DECIMAL, N3::Terminals::DOUBLE, N3::Terminals::ECHAR, N3::Terminals::ESCAPE_CHAR4, N3::Terminals::ESCAPE_CHAR8, N3::Terminals::EXPONENT, N3::Terminals::FORALL, N3::Terminals::INTEGER, N3::Terminals::IRIREF, N3::Terminals::IRI_RANGE, N3::Terminals::LANGTAG, N3::Terminals::PERCENT, N3::Terminals::PLX, N3::Terminals::PNAME_LN, N3::Terminals::PNAME_NS, N3::Terminals::PN_CHARS, N3::Terminals::PN_CHARS_BASE, N3::Terminals::PN_CHARS_BODY, N3::Terminals::PN_CHARS_U, N3::Terminals::PN_LOCAL, N3::Terminals::PN_LOCAL_BODY, N3::Terminals::PN_LOCAL_ESC, N3::Terminals::PN_PREFIX, N3::Terminals::PREFIX, N3::Terminals::QUICK_VAR_NAME, N3::Terminals::STRING_LITERAL_LONG_QUOTE, N3::Terminals::STRING_LITERAL_LONG_SINGLE_QUOTE, N3::Terminals::STRING_LITERAL_QUOTE, N3::Terminals::STRING_LITERAL_SINGLE_QUOTE, N3::Terminals::UCHAR, N3::Terminals::U_CHARS1, N3::Terminals::U_CHARS2, N3::Terminals::WS

Instance Attribute Summary

Attributes inherited from RDF::Literal

#datatype, #language

Instance Method Summary collapse

Methods inherited from RDF::Literal

#as_datetime, #as_number, #canonicalize!, #compatible?, #comperable_datatype?, #datatype?, #eql?, #escape, #hash, #humanize, #initialize, #inspect, #language?, #literal?, #method_missing, #object, #plain?, #respond_to_missing?, #simple?, #squish, #squish!, #to_s, #valid?, #valid_extended?, #validate!, #value, #value_hash

Methods included from Term

#aggregate?, #as_datetime, #as_number, #compatible?, #eql?, #escape, #evaluate, #ndvars, #optimize, #sameTerm?, #term?, #terms, #to_base, #to_term, #vars

Methods included from SPARQL::Algebra::Expression

cast, #constant?, #evaluate, extension, extensions, for, #invalid?, new, #node?, open, #optimize, #optimize!, parse, register_extension, #to_sxp_bin, #valid?, #validate!, #variable?

Methods included from Value

#anonymous?, #canonicalize, #canonicalize!, #constant?, #formula?, #graph?, #inspect, #inspect!, #invalid?, #iri?, #list?, #literal?, #node?, #resource?, #start_with?, #statement?, #term?, #to_ndvar, #to_nquads, #to_ntriples, #to_rdf, #to_term, #type_error, #uri?, #valid?, #validate!, #variable?

Constructor Details

This class inherits a constructor from RDF::Literal

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class RDF::Literal

Instance Method Details

#%(other) ⇒ RDF::Literal

Exponent − Performs remainder of self divided by other.

Parameters:

Returns:

See Also:

Since:

  • 0.2.3



148
149
150
151
152
153
154
155
156
157
158
# File 'vendor/bundler/ruby/3.0.0/bundler/gems/rdf-237066655558/lib/rdf/model/literal/numeric.rb', line 148

def %(other)
  if self.class == Double || [Double, ::Float].include?(other.class)
    self.class.new(to_f % other.to_f)
  elsif ((self.class == RDF::Literal::Float || other.class == RDF::Literal::Float) rescue false)
    self.class.new(to_f % other.to_f)
  elsif self.class == Decimal || other.class == Decimal
    self.class.new(to_d % (other.respond_to?(:to_d) ? other.to_d : BigDecimal(other.to_s)))
  else
    self.class.new(to_i % other.to_i)
  end
end

#*(other) ⇒ RDF::Literal::Numeric

Returns the product of self times other.



114
115
116
117
118
119
120
121
122
123
124
# File 'vendor/bundler/ruby/3.0.0/bundler/gems/rdf-237066655558/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

#**(other) ⇒ RDF::Literal::Numeric

Exponent − Performs exponential (power) calculation on operators.

Promotes values, as necessary, with the result type depending on the input values.



135
136
137
138
139
# File 'vendor/bundler/ruby/3.0.0/bundler/gems/rdf-237066655558/lib/rdf/model/literal/numeric.rb', line 135

def **(other)
  RDF::Literal(object ** (other.is_a?(RDF::Literal::Numeric) ? other.object : other))
rescue ZeroDivisionError
  RDF::Literal::Double.new('INF')
end

#+(other) ⇒ RDF::Literal::Numeric

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.



76
77
78
79
80
81
82
83
84
85
86
# File 'vendor/bundler/ruby/3.0.0/bundler/gems/rdf-237066655558/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

#+@RDF::Literal::Numeric

Returns self.

Returns:

Since:

  • 0.2.3



52
53
54
# File 'vendor/bundler/ruby/3.0.0/bundler/gems/rdf-237066655558/lib/rdf/model/literal/numeric.rb', line 52

def +@
  self # unary plus
end

#-(other) ⇒ RDF::Literal::Numeric

Returns the difference of self minus other.



95
96
97
98
99
100
101
102
103
104
105
# File 'vendor/bundler/ruby/3.0.0/bundler/gems/rdf-237066655558/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

#-@RDF::Literal::Numeric

Returns self negated.

Returns:

Since:

  • 0.2.3



61
62
63
# File 'vendor/bundler/ruby/3.0.0/bundler/gems/rdf-237066655558/lib/rdf/model/literal/numeric.rb', line 61

def -@
  self.class.new(-self.object)
end

#/(other) ⇒ RDF::Literal::Numeric

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.

Parameters:

Returns:

Raises:

  • (ZeroDivisionError)

    if divided by zero

See Also:

Since:

  • 0.2.3



171
172
173
174
175
176
177
178
179
# File 'vendor/bundler/ruby/3.0.0/bundler/gems/rdf-237066655558/lib/rdf/model/literal/numeric.rb', line 171

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

#<=>(other) ⇒ Integer

Compares this literal to other for sorting purposes.

Parameters:

Returns:

Since:

  • 0.3.0



13
14
15
16
17
18
19
20
21
22
23
# File 'vendor/bundler/ruby/3.0.0/bundler/gems/rdf-237066655558/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

#==(other) ⇒ Boolean

Returns true if this literal is equal to other.

Parameters:

Returns:

Since:

  • 0.3.0



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'vendor/bundler/ruby/3.0.0/bundler/gems/rdf-237066655558/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

#absRDF::Literal

Returns the absolute value of self.

Returns:

Raises:

  • (NotImplementedError)

    unless implemented in subclass

Since:

  • 0.3.0



186
187
188
# File 'vendor/bundler/ruby/3.0.0/bundler/gems/rdf-237066655558/lib/rdf/model/literal/numeric.rb', line 186

def abs
  raise NotImplementedError
end

#ceilRDF::Literal

Returns the smallest integer greater than or equal to self.

Examples:

RDF::Literal(1).ceil            #=> RDF::Literal(1)

Returns:

Since:

  • 0.3.0



206
207
208
# File 'vendor/bundler/ruby/3.0.0/bundler/gems/rdf-237066655558/lib/rdf/model/literal/numeric.rb', line 206

def ceil
  self
end

#floorRDF::Literal

Returns the largest integer less than or equal to self.

Examples:

RDF::Literal(1).floor            #=> RDF::Literal(1)

Returns:

Since:

  • 0.3.0



217
218
219
# File 'vendor/bundler/ruby/3.0.0/bundler/gems/rdf-237066655558/lib/rdf/model/literal/numeric.rb', line 217

def floor
  self
end

#roundRDF::Literal

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.

Returns:

Raises:

  • (NotImplementedError)

    unless implemented in subclass

Since:

  • 0.3.0



195
196
197
# File 'vendor/bundler/ruby/3.0.0/bundler/gems/rdf-237066655558/lib/rdf/model/literal/numeric.rb', line 195

def round
  raise NotImplementedError
end

#to_dBigDecimal

Returns the value as a decimal number.

Returns:

Since:

  • 0.3.0



246
247
248
249
250
# File 'vendor/bundler/ruby/3.0.0/bundler/gems/rdf-237066655558/lib/rdf/model/literal/numeric.rb', line 246

def to_d
  @object.respond_to?(:to_d) ? @object.to_d : BigDecimal(@object.to_s)
rescue FloatDomainError
  ::Float::NAN
end

#to_fFloat

Returns the value as a floating point number.

The usual accuracy limits and errors of binary float arithmetic apply.

Returns:

See Also:

  • BigDecimal#to_f

Since:

  • 0.3.0



238
239
240
# File 'vendor/bundler/ruby/3.0.0/bundler/gems/rdf-237066655558/lib/rdf/model/literal/numeric.rb', line 238

def to_f
  @object.to_f
end

#to_iInteger Also known as: to_int, ord

Returns the value as an integer.

Returns:

Since:

  • 0.3.0



225
226
227
# File 'vendor/bundler/ruby/3.0.0/bundler/gems/rdf-237066655558/lib/rdf/model/literal/numeric.rb', line 225

def to_i
  @object.to_i
end

#to_rRational

Returns the value as a rational number.

Returns:

  • (Rational)

Since:

  • 0.3.0



256
257
258
# File 'vendor/bundler/ruby/3.0.0/bundler/gems/rdf-237066655558/lib/rdf/model/literal/numeric.rb', line 256

def to_r
  @object.to_r
end