Class: RDF::Literal::Double

Inherits:
Numeric show all
Defined in:
vendor/bundler/ruby/2.5.0/bundler/gems/rdf-c7356a6367f8/lib/rdf/model/literal/double.rb

Overview

An floating point number literal.

Examples:

Arithmetic with floating point literals

RDF::Literal(1.0) + 0.5                 #=> RDF::Literal(1.5)
RDF::Literal(3.0) - 6                   #=> RDF::Literal(-3.0)
RDF::Literal(Math::PI) * 2              #=> RDF::Literal(Math::PI * 2)
RDF::Literal(Math::PI) / 2              #=> RDF::Literal(Math::PI / 2)

See Also:

Since:

  • 0.2.1

Direct Known Subclasses

Float

Constant Summary collapse

DATATYPE =

Since:

  • 0.2.1

RDF::XSD.double
GRAMMAR =

Since:

  • 0.2.1

/^(?:NaN|\-?INF|[+\-]?(?:\d+(:?\.\d*)?|\.\d+)(?:[eE][\+\-]?\d+)?)$/.freeze

Constants inherited from RDF::Literal

FALSE, TRUE, ZERO

Instance Attribute Summary

Attributes inherited from RDF::Literal

#datatype, #language

Instance Method Summary collapse

Methods inherited from Numeric

#*, #+, #+@, #-, #-@, #/, #to_d, #to_f, #to_i, #to_r

Methods inherited from RDF::Literal

#compatible?, #comperable_datatype?, #eql?, #escape, #has_datatype?, #has_language?, #hash, #humanize, #inspect, #literal?, #method_missing, #object, #plain?, #respond_to_missing?, #simple?, #squish, #squish!, #valid?, #validate!, #value, #value_hash

Methods included from Term

#aggregate?, #compatible?, #eql?, #escape, #evaluate, #ndvars, #term?, #to_base, #to_term, #vars

Methods included from SPARQL::Algebra::Expression

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

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

Parameters:

  • value (String, Float, #to_f)
  • value (Object)
  • language (Symbol)

    (nil) Language is downcased to ensure proper matching

  • lexical (String)

    (nil) Supplied lexical representation of this literal, otherwise it comes from transforming value to a string form..

  • datatype (URI)

    (nil)

  • validate (Boolean)

    (false)

  • canonicalize (Boolean)

    (false)

Since:

  • 0.2.1



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-c7356a6367f8/lib/rdf/model/literal/double.rb', line 20

def initialize(value, datatype: nil, lexical: nil, **options)
  @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.

Parameters:

Returns:

Since:

  • 0.3.0



88
89
90
91
92
93
94
95
96
97
98
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-c7356a6367f8/lib/rdf/model/literal/double.rb', line 88

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.

Parameters:

Returns:

Since:

  • 0.3.0



73
74
75
76
77
78
79
80
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-c7356a6367f8/lib/rdf/model/literal/double.rb', line 73

def ==(other)
  if valid? && infinite? && other.respond_to?(:infinite?) && other.infinite?
    infinite? == other.infinite?
    # JRuby INF comparisons differ from MRI
  else
    super
  end
end

#absRDF::Literal

Returns the absolute value of self.

Returns:

Since:

  • 0.2.3



179
180
181
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-c7356a6367f8/lib/rdf/model/literal/double.rb', line 179

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.

Returns:

See Also:

Since:

  • 0.2.1



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-c7356a6367f8/lib/rdf/model/literal/double.rb', line 42

def canonicalize!
  # Can't use simple %f transformation due to special requirements from
  # N3 tests in representation
  @string = case
    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

#ceilRDF::Literal

Returns the smallest number greater than or equal to self.

Examples:

RDF::Literal(1.2).ceil            #=> RDF::Literal(2)
RDF::Literal(-1.2).ceil           #=> RDF::Literal(-1)
RDF::Literal(2.0).ceil            #=> RDF::Literal(2)
RDF::Literal(-2.0).ceil           #=> RDF::Literal(-2)

Returns:

Since:

  • 0.2.3



155
156
157
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-c7356a6367f8/lib/rdf/model/literal/double.rb', line 155

def ceil
  self.class.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).

Examples:

RDF::Literal(-1.0).finite?        #=> true
RDF::Literal(1.0/0.0).finite?     #=> false
RDF::Literal(0.0/0.0).finite?     #=> false

Returns:

Since:

  • 0.2.3



125
126
127
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-c7356a6367f8/lib/rdf/model/literal/double.rb', line 125

def finite?
  to_f.finite?
end

#floorRDF::Literal

Returns the largest number less than or equal to self.

Examples:

RDF::Literal(1.2).floor           #=> RDF::Literal(1)
RDF::Literal(-1.2).floor          #=> RDF::Literal(-2)
RDF::Literal(2.0).floor           #=> RDF::Literal(2)
RDF::Literal(-2.0).floor          #=> RDF::Literal(-2)

Returns:

Since:

  • 0.2.3



170
171
172
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-c7356a6367f8/lib/rdf/model/literal/double.rb', line 170

def floor
  self.class.new(to_f.floor)
end

#infinite?Integer

Returns nil, -1, or +1 depending on whether the value is finite, -INF, or +INF.

Examples:

RDF::Literal(0.0/0.0).infinite?   #=> nil
RDF::Literal(-1.0/0.0).infinite?  #=> -1
RDF::Literal(+1.0/0.0).infinite?  #=> 1

Returns:

Since:

  • 0.2.3



140
141
142
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-c7356a6367f8/lib/rdf/model/literal/double.rb', line 140

def infinite?
  to_f.infinite?
end

#nan?Boolean

Returns true if the value is an invalid IEEE floating point number.

Examples:

RDF::Literal(-1.0).nan?           #=> false
RDF::Literal(1.0/0.0).nan?        #=> false
RDF::Literal(0.0/0.0).nan?        #=> true

Returns:

Since:

  • 0.2.3



110
111
112
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-c7356a6367f8/lib/rdf/model/literal/double.rb', line 110

def nan?
  to_f.nan?
end

#nonzero?Boolean

Returns self if the value is not zero, nil otherwise.

Returns:

Since:

  • 0.2.3



205
206
207
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-c7356a6367f8/lib/rdf/model/literal/double.rb', line 205

def nonzero?
  to_f.nonzero? ? self : nil
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:

Since:

  • 0.2.1



187
188
189
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-c7356a6367f8/lib/rdf/model/literal/double.rb', line 187

def round
  self.class.new(to_f.round)
end

#to_sString

Returns the value as a string.

Returns:

Since:

  • 0.2.1



213
214
215
216
217
218
219
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-c7356a6367f8/lib/rdf/model/literal/double.rb', line 213

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.

Returns:

Since:

  • 0.2.3



196
197
198
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-c7356a6367f8/lib/rdf/model/literal/double.rb', line 196

def zero?
  to_f.zero?
end