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
Constant Summary
Constants inherited from RDF::Literal
Instance Attribute Summary
Attributes inherited from RDF::Literal
Instance Method Summary (collapse)
-
- (RDF::Literal::Numeric) *(other)
Returns the product of
selftimesother. -
- (RDF::Literal::Numeric) +(other)
Returns the sum of
selfplusother. -
- (RDF::Literal::Numeric) +@
Returns
self. -
- (RDF::Literal::Numeric) -(other)
Returns the difference of
selfminusother. -
- (RDF::Literal::Numeric) -@
Returns
selfnegated. -
- (RDF::Literal::Numeric) /(other)
Returns the quotient of
selfdivided byother. -
- (Integer) <=>(other)
Compares this literal to
otherfor sorting purposes. -
- (Boolean) ==(other)
Returns
trueif this literal is equal toother. -
- (BigDecimal) to_d
Returns the value as a decimal number.
-
- (Float) to_f
Returns the value as a floating point number.
-
- (Integer) to_i
(also: #to_int, #ord)
Returns the value as an integer.
-
- (Rational) to_r
Returns the value as a rational number.
Methods inherited from RDF::Literal
#anonymous?, #canonicalize, #canonicalize!, #comperable_datatype?, #eql?, #has_datatype?, #has_language?, #hash, #initialize, #inspect, #invalid?, #literal?, #object, #plain?, #to_s, #valid?, #validate!, #value
Methods included from Term
Methods included from Value
#graph?, #inspect, #inspect!, #iri?, #literal?, #node?, #resource?, #statement?, #to_ntriples, #to_quad, #to_rdf, #type_error, #uri?, #variable?
Constructor Details
This class inherits a constructor from RDF::Literal
Instance Method Details
- (RDF::Literal::Numeric) *(other)
Returns the product of self times other.
112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/rdf/model/literal/numeric.rb', line 112 def *(other) if self.class == Double || other.class == Double RDF::Literal::Double.new(to_f * other.to_f) elsif self.class == Float || other.class == Float 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) +(other)
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.
74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/rdf/model/literal/numeric.rb', line 74 def +(other) if self.class == Double || other.class == Double RDF::Literal::Double.new(to_f + other.to_f) elsif self.class == Float || other.class == Float 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.
50 51 52 |
# File 'lib/rdf/model/literal/numeric.rb', line 50 def +@ self # unary plus end |
- (RDF::Literal::Numeric) -(other)
Returns the difference of self minus other.
93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/rdf/model/literal/numeric.rb', line 93 def -(other) if self.class == Double || other.class == Double RDF::Literal::Double.new(to_f - other.to_f) elsif self.class == Float || other.class == Float 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.
59 60 61 |
# File 'lib/rdf/model/literal/numeric.rb', line 59 def -@ self.class.new(-self.object) end |
- (RDF::Literal::Numeric) /(other)
Returns the quotient of self divided by other.
As a special case, if the types of both $arg1 and $arg2 are xs:integer, then the return type is xs:decimal.
135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/rdf/model/literal/numeric.rb', line 135 def /(other) if self.class == Double || other.class == Double RDF::Literal::Double.new(to_f / other.to_f) elsif self.class == Float || other.class == Float 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 |
- (Integer) <=>(other)
Compares this literal to other for sorting purposes.
13 14 15 16 17 18 19 20 21 |
# File 'lib/rdf/model/literal/numeric.rb', line 13 def <=>(other) case other when ::Numeric to_d <=> other when Numeric to_d <=> other.to_d else super end end |
- (Boolean) ==(other)
Returns true if this literal is equal to other.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/rdf/model/literal/numeric.rb', line 29 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 |
- (BigDecimal) to_d
Returns the value as a decimal number.
172 173 174 |
# File 'lib/rdf/model/literal/numeric.rb', line 172 def to_d @object.respond_to?(:to_d) ? @object.to_d : BigDecimal(@object.to_s) end |
- (Float) to_f
Returns the value as a floating point number.
The usual accuracy limits and errors of binary float arithmetic apply.
164 165 166 |
# File 'lib/rdf/model/literal/numeric.rb', line 164 def to_f @object.to_f end |
- (Integer) to_i Also known as: to_int, ord
Returns the value as an integer.
151 152 153 |
# File 'lib/rdf/model/literal/numeric.rb', line 151 def to_i @object.to_i end |
- (Rational) to_r
Returns the value as a rational number.
180 181 182 |
# File 'lib/rdf/model/literal/numeric.rb', line 180 def to_r @object.to_r end |