Class: RDF::Literal::Double
- Inherits:
-
Numeric
- Object
- RDF::Literal
- Numeric
- RDF::Literal::Double
- Defined in:
- lib/rdf/model/literal/double.rb
Overview
An floating point number literal.
Constant Summary collapse
- DATATYPE =
RDF::URI("http://www.w3.org/2001/XMLSchema#double")
- GRAMMAR =
/^(?:NaN|\-?INF|[+\-]?(?:\d+(:?\.\d*)?|\.\d+)(?:[eE][\+\-]?\d+)?)$/.freeze
- PI =
Approximation of the mathematical constant π
From the XQuery function [math:pi](www.w3.org/TR/xpath-functions/#func-math-pi).
Double.new(Math::PI)
Constants inherited from RDF::Literal
FALSE, TRUE, XSD_STRING, ZERO
Instance Attribute Summary
Attributes inherited from RDF::Literal
#datatype, #direction, #language
Instance Method Summary collapse
-
#<=>(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`.
-
#canonicalize! ⇒ RDF::Literal
Converts this literal into its canonical lexical representation.
-
#ceil ⇒ RDF::Literal::Integer
Returns the smallest integer greater than or equal to ‘self`.
-
#finite? ⇒ Boolean
Returns ‘true` if the value is a valid IEEE floating point number (it is not infinite, and `nan?` is `false`).
-
#floor ⇒ RDF::Literal::Integer
Returns the largest integer less than or equal to ‘self`.
-
#infinite? ⇒ Integer
Returns ‘nil`, `-1`, or `+1` depending on whether the value is finite, `-INF`, or `+INF`.
-
#initialize(value, datatype: nil, lexical: nil, **options) ⇒ Double
constructor
A new instance of Double.
-
#nan? ⇒ Boolean
Returns ‘true` if the value is an invalid IEEE floating point number.
-
#nonzero? ⇒ Boolean
Returns ‘self` if the value is not zero, `nil` otherwise.
-
#round ⇒ RDF::Literal::Double
Returns the number with no fractional part that is closest to the argument.
-
#to_s ⇒ String
Returns the value as a string.
-
#zero? ⇒ Boolean
Returns ‘true` if the value is zero.
Methods inherited from Numeric
#%, #*, #**, #+, #+@, #-, #-@, #/, #acos, #asin, #atan, #atan2, #cos, #exp, #exp10, #log, #log10, #sin, #sqrt, #tan, #to_d, #to_f, #to_i, #to_r
Methods inherited from RDF::Literal
#compatible?, #comperable_datatype2?, #comperable_datatype?, #datatype?, datatype_map, datatyped_class, #direction?, #english?, #eql?, #escape, #freeze, #hash, #humanize, #inspect, #language?, #literal?, new, #object, #plain?, #simple?, #squish, #squish!, #valid?, #validate!, #value, #value_hash
Methods included from Term
#compatible?, #eql?, #term?, #terms, #to_base, #to_term
Methods included from Value
#anonymous?, #canonicalize, #constant?, #graph?, #inspect, #inspect!, #invalid?, #iri?, #list?, #literal?, #node?, #resource?, #start_with?, #statement?, #term?, #to_nquads, #to_ntriples, #to_rdf, #to_term, #type_error, #uri?, #valid?, #validate!, #variable?
Constructor Details
#initialize(value, datatype: nil, lexical: nil, **options) ⇒ Double
Returns a new instance of Double.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/rdf/model/literal/double.rb', line 20 def initialize(value, datatype: nil, lexical: nil, **) @datatype = RDF::URI(datatype || self.class.const_get(:DATATYPE)) @string = lexical || (value if value.is_a?(String)) @object = case when value.is_a?(::String) then case value.upcase when '+INF' then 1/0.0 when 'INF' then 1/0.0 when '-INF' then -1/0.0 when 'NAN' then 0/0.0 else Float(value.sub(/\.[eE]/, '.0E')) rescue nil end when value.is_a?(::Float) then value when value.respond_to?(:to_f) then value.to_f else 0.0 # FIXME end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class RDF::Literal
Instance Method Details
#<=>(other) ⇒ Integer
Compares this literal to ‘other` for sorting purposes.
97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/rdf/model/literal/double.rb', line 97 def <=>(other) case other when ::Numeric to_f <=> other when RDF::Literal::Decimal to_f <=> other.to_d when RDF::Literal::Double to_f <=> other.to_f else super end end |
#==(other) ⇒ Boolean
Returns ‘true` if this literal is equal to `other`.
82 83 84 85 86 87 88 89 |
# File 'lib/rdf/model/literal/double.rb', line 82 def ==(other) if valid? && infinite? && other.respond_to?(:infinite?) && other.infinite? infinite? == other.infinite? # JRuby INF comparisons differ from MRI else super end end |
#abs ⇒ RDF::Literal
Returns the absolute value of ‘self`.
From the XQuery function [fn:abs](www.w3.org/TR/xpath-functions/#func-abs).
197 198 199 |
# File 'lib/rdf/model/literal/double.rb', line 197 def abs (f = to_f) && f > 0 ? self : self.class.new(f.abs) end |
#canonicalize! ⇒ RDF::Literal
Converts this literal into its canonical lexical representation.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/rdf/model/literal/double.rb', line 50 def canonicalize! # Can't use simple %f transformation due to special requirements from # N3 tests in representation @string = case when @object.nil? then 'NaN' when @object.nan? then 'NaN' when @object.infinite? then @object.to_s[0...-'inity'.length].upcase when @object.zero? then '0.0E0' else i, f, e = ('%.15E' % @object.to_f).split(/[\.E]/) f.sub!(/0*$/, '') # remove any trailing zeroes f = '0' if f.empty? # ...but there must be a digit to the right of the decimal point e.sub!(/^(?:\+|(\-))?0+(\d+)$/, '\1\2') # remove the optional leading '+' sign and any extra leading zeroes "#{i}.#{f}E#{e}" end @object = case @string when 'NaN' then 0/0.0 when 'INF' then 1/0.0 when '-INF' then -1/0.0 else Float(@string) end self end |
#ceil ⇒ RDF::Literal::Integer
Returns the smallest integer greater than or equal to ‘self`.
From the XQuery function [fn:ceil](www.w3.org/TR/xpath-functions/#func-ceil).
167 168 169 |
# File 'lib/rdf/model/literal/double.rb', line 167 def ceil RDF::Literal::Integer.new(to_f.ceil) end |
#finite? ⇒ Boolean
Returns ‘true` if the value is a valid IEEE floating point number (it is not infinite, and `nan?` is `false`).
134 135 136 |
# File 'lib/rdf/model/literal/double.rb', line 134 def finite? to_f.finite? end |
#floor ⇒ RDF::Literal::Integer
Returns the largest integer less than or equal to ‘self`.
From the XQuery function [fn:floor](www.w3.org/TR/xpath-functions/#func-floor).
185 186 187 |
# File 'lib/rdf/model/literal/double.rb', line 185 def floor RDF::Literal::Integer.new(to_f.floor) end |
#infinite? ⇒ Integer
Returns ‘nil`, `-1`, or `+1` depending on whether the value is finite, `-INF`, or `+INF`.
149 150 151 |
# File 'lib/rdf/model/literal/double.rb', line 149 def infinite? to_f.infinite? end |
#nan? ⇒ Boolean
Returns ‘true` if the value is an invalid IEEE floating point number.
119 120 121 |
# File 'lib/rdf/model/literal/double.rb', line 119 def nan? to_f.nan? end |
#nonzero? ⇒ Boolean
Returns ‘self` if the value is not zero, `nil` otherwise.
226 227 228 |
# File 'lib/rdf/model/literal/double.rb', line 226 def nonzero? to_f.nonzero? ? self : nil end |
#round ⇒ RDF::Literal::Double
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.
From the XQuery function [fn:round](www.w3.org/TR/xpath-functions/#func-round).
208 209 210 |
# File 'lib/rdf/model/literal/double.rb', line 208 def round self.class.new(to_d.round(half: (to_d < 0 ? :down : :up))) end |
#to_s ⇒ String
Returns the value as a string.
234 235 236 237 238 239 240 |
# File 'lib/rdf/model/literal/double.rb', line 234 def to_s @string || case when @object.nan? then 'NaN' when @object.infinite? then @object.to_s[0...-'inity'.length].upcase else @object.to_s end end |
#zero? ⇒ Boolean
Returns ‘true` if the value is zero.
217 218 219 |
# File 'lib/rdf/model/literal/double.rb', line 217 def zero? to_f.zero? end |