Class: RDF::Literal::Numeric
- Inherits:
-
RDF::Literal
- Object
- RDF::Literal
- RDF::Literal::Numeric
- Defined in:
- lib/rdf/model/literal/numeric.rb
Overview
Shared methods and class ancestry for numeric literal classes.
Constant Summary
Constants inherited from RDF::Literal
Instance Attribute Summary
Attributes inherited from RDF::Literal
Instance Method Summary collapse
-
#*(other) ⇒ RDF::Literal::Numeric
Returns the product of ‘self` times `other`.
-
#+(other) ⇒ RDF::Literal::Numeric
Returns the sum of ‘self` plus `other`.
-
#+@ ⇒ RDF::Literal::Numeric
Returns ‘self`.
-
#-(other) ⇒ RDF::Literal::Numeric
Returns the difference of ‘self` minus `other`.
-
#-@ ⇒ RDF::Literal::Numeric
Returns ‘self` negated.
-
#/(other) ⇒ RDF::Literal::Numeric
Returns the quotient of ‘self` divided by `other`.
-
#<=>(other) ⇒ Integer
Compares this literal to ‘other` for sorting purposes.
-
#==(other) ⇒ Boolean
Returns ‘true` if this literal is equal to `other`.
-
#abs ⇒ RDF::Literal
Returns the absolute value of ‘self`.
-
#ceil ⇒ RDF::Literal
Returns the smallest integer greater than or equal to ‘self`.
-
#floor ⇒ RDF::Literal
Returns the largest integer less than or equal to ‘self`.
-
#round ⇒ RDF::Literal
Returns the number with no fractional part that is closest to the argument.
-
#to_d ⇒ BigDecimal
Returns the value as a decimal number.
-
#to_f ⇒ Float
Returns the value as a floating point number.
-
#to_i ⇒ Integer
(also: #to_int, #ord)
Returns the value as an integer.
-
#to_r ⇒ Rational
Returns the value as a rational number.
Methods inherited from RDF::Literal
#canonicalize!, #compatible?, #comperable_datatype?, datatype_map, datatyped_class, #eql?, #escape, #freeze, #has_datatype?, #has_language?, #hash, #humanize, #initialize, #inspect, #literal?, new, #object, #plain?, #simple?, #to_s, #valid?, #validate!, #value, #value_hash
Methods included from Term
#compatible?, #eql?, #term?, #to_base, #to_term
Methods included from Value
#anonymous?, #canonicalize, #canonicalize!, #constant?, #graph?, #inspect, #inspect!, #invalid?, #iri?, #list?, #literal?, #node?, #resource?, #statement?, #term?, #to_nquads, #to_ntriples, #to_rdf, #to_term, #type_error, #uri?, #valid?, #validate!, #variable?
Constructor Details
This class inherits a constructor from RDF::Literal
Instance Method Details
#*(other) ⇒ RDF::Literal::Numeric
Returns the product of ‘self` times `other`.
114 115 116 117 118 119 120 121 122 123 124 |
# 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 |
#+(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 '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`.
52 53 54 |
# File '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 '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.
61 62 63 |
# File '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.
137 138 139 140 141 142 143 144 145 |
# 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 |
#<=>(other) ⇒ Integer
Compares this literal to ‘other` for sorting purposes.
13 14 15 16 17 18 19 20 21 22 23 |
# 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 |
#==(other) ⇒ Boolean
Returns ‘true` if this literal is equal to `other`.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# 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 |
#abs ⇒ RDF::Literal
Returns the absolute value of ‘self`.
152 153 154 |
# File 'lib/rdf/model/literal/numeric.rb', line 152 def abs raise NotImplementedError end |
#ceil ⇒ RDF::Literal
Returns the smallest integer greater than or equal to ‘self`.
172 173 174 |
# File 'lib/rdf/model/literal/numeric.rb', line 172 def ceil self end |
#floor ⇒ RDF::Literal
Returns the largest integer less than or equal to ‘self`.
183 184 185 |
# File 'lib/rdf/model/literal/numeric.rb', line 183 def floor self end |
#round ⇒ RDF::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.
161 162 163 |
# File 'lib/rdf/model/literal/numeric.rb', line 161 def round raise NotImplementedError end |
#to_d ⇒ BigDecimal
Returns the value as a decimal number.
212 213 214 215 216 |
# 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 |
#to_f ⇒ Float
Returns the value as a floating point number.
The usual accuracy limits and errors of binary float arithmetic apply.
204 205 206 |
# File 'lib/rdf/model/literal/numeric.rb', line 204 def to_f @object.to_f end |
#to_i ⇒ Integer Also known as: to_int, ord
Returns the value as an integer.
191 192 193 |
# File 'lib/rdf/model/literal/numeric.rb', line 191 def to_i @object.to_i end |
#to_r ⇒ Rational
Returns the value as a rational number.
222 223 224 |
# File 'lib/rdf/model/literal/numeric.rb', line 222 def to_r @object.to_r end |