Class: RDF::Literal::Double

Inherits:
RDF::Literal show all
Includes:
Numeric
Defined in:
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:

Constant Summary

DATATYPE =

Since:

  • 0.2.1

XSD.double
GRAMMAR =

FIXME: support 'INF', '-INF' and 'NaN'

Since:

  • 0.2.1

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

Constants inherited from RDF::Literal

FALSE, TRUE, ZERO

Instance Method Summary (collapse)

Methods inherited from RDF::Literal

#anonymous?, #canonicalize, #eql?, #has_datatype?, #has_language?, #hash, #inspect, #invalid?, #literal?, #object, #valid?, #validate!, #value

Methods included from Term

#constant?, #variable?

Methods included from Value

#graph?, #inspect, #inspect!, #iri?, #literal?, #node?, #resource?, #statement?, #to_ntriples, #to_rdf, #uri?, #variable?

Constructor Details

- (Double) initialize(value, options = {})

A new instance of Double

Parameters:

  • (Float, #to_f) value
  • (Hash) options (defaults to: {})

    a customizable set of options

Options Hash (options):

  • (String) :lexical — default: nil

Since:

  • 0.2.1



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rdf/model/literal/double.rb', line 22

def initialize(value, options = {})
  @datatype = RDF::URI(options[:datatype] || DATATYPE)
  @string   = options[:lexical] if options.has_key?(:lexical)
  @string   = value if !defined?(@string) && value.is_a?(String)
  @object   = case
    when value.is_a?(::String) then case value
      when 'INF'  then 1/0.0
      when '-INF' then -1/0.0
      when 'NaN'  then 0/0.0
      else Float(value) rescue nil
    end
    when value.is_a?(::Float)     then value
    when value.respond_to?(:to_f) then value.to_f
    else Float(value.to_s) rescue nil # FIXME
  end
end

Instance Method Details

- (RDF::Literal) *(other)

Returns the product of self times other.

Parameters:

  • (#to_f) other

Returns:

Since:

  • 0.2.3



234
235
236
# File 'lib/rdf/model/literal/double.rb', line 234

def *(other)
  RDF::Literal(to_f * other.to_f)
end

- (RDF::Literal) +(other)

Returns the sum of self plus other.

Parameters:

  • (#to_f) other

Returns:

Since:

  • 0.2.3



214
215
216
# File 'lib/rdf/model/literal/double.rb', line 214

def +(other)
  RDF::Literal(to_f + other.to_f)
end

- (RDF::Literal) +@

Returns self.

Returns:

Since:

  • 0.2.3



195
196
197
# File 'lib/rdf/model/literal/double.rb', line 195

def +@
  self # unary plus
end

- (RDF::Literal) -(other)

Returns the difference of self minus other.

Parameters:

  • (#to_f) other

Returns:

Since:

  • 0.2.3



224
225
226
# File 'lib/rdf/model/literal/double.rb', line 224

def -(other)
  RDF::Literal(to_f - other.to_f)
end

- (RDF::Literal) -@

Returns self negated.

Returns:

Since:

  • 0.2.3



204
205
206
# File 'lib/rdf/model/literal/double.rb', line 204

def -@
  RDF::Literal(-to_f, :datatype => datatype) # unary minus
end

- (RDF::Literal) /(other)

Returns the quotient of self divided by other.

Parameters:

  • (#to_f) other

Returns:

Since:

  • 0.2.3



244
245
246
# File 'lib/rdf/model/literal/double.rb', line 244

def /(other)
  RDF::Literal(to_f / other.to_f)
end

- (Integer) <=>(other)

Compares this literal to other for sorting purposes.

Parameters:

  • (Object) other

Returns:

Since:

  • 0.3.0



68
69
70
71
72
73
74
75
76
# File 'lib/rdf/model/literal/double.rb', line 68

def <=>(other)
  case other
    when ::Numeric
      to_d <=> other
    when RDF::Literal::Decimal, RDF::Literal::Double
      to_d <=> other.to_d
    else super
  end
end

- (Boolean) ==(other) Also known as: ===

Returns true if this literal is equivalent to other.

Parameters:

  • (Object) other

Returns:

Since:

  • 0.3.0



84
85
86
# File 'lib/rdf/model/literal/double.rb', line 84

def ==(other)
  (cmp = (self <=> other)) ? cmp.zero? : false
end

- (RDF::Literal) abs

Returns the absolute value of self.

Returns:

Since:

  • 0.2.3



168
169
170
# File 'lib/rdf/model/literal/double.rb', line 168

def abs
  (f = to_f) && f > 0 ? self : RDF::Literal(f.abs)
end

- (RDF::Literal) canonicalize!

Converts this literal into its canonical lexical representation.

Returns:

See Also:

Since:

  • 0.2.1



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/rdf/model/literal/double.rb', line 44

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 = ('%.16E' % @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') # remove the optional leading '+' sign and any extra leading zeroes
      "#{i}.#{f}E#{e}"
  end
  @object = Float(@string) unless @object.nil?
  self
end

- (RDF::Literal) ceil

Returns the smallest integer 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



144
145
146
# File 'lib/rdf/model/literal/double.rb', line 144

def ceil
  RDF::Literal(to_f.ceil)
end

- (Boolean) finite?

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



114
115
116
# File 'lib/rdf/model/literal/double.rb', line 114

def finite?
  to_f.finite?
end

- (RDF::Literal) floor

Returns the largest integer 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



159
160
161
# File 'lib/rdf/model/literal/double.rb', line 159

def floor
  RDF::Literal(to_f.floor)
end

- (Integer) infinite?

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



129
130
131
# File 'lib/rdf/model/literal/double.rb', line 129

def infinite?
  to_f.infinite?
end

- (Boolean) nonzero?

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

Returns:

Since:

  • 0.2.3



186
187
188
# File 'lib/rdf/model/literal/double.rb', line 186

def nonzero?
  to_f.nonzero? ? self : nil
end

- (BigDecimal) to_d

Returns the value as a decimal number.

Returns:

  • (BigDecimal)

Since:

  • 0.2.1



280
281
282
# File 'lib/rdf/model/literal/double.rb', line 280

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.

Returns:

  • (Float)

Since:

  • 0.2.1



272
273
274
# File 'lib/rdf/model/literal/double.rb', line 272

def to_f
  @object.to_f
end

- (Integer) to_i

Returns the value as an integer.

Returns:

Since:

  • 0.2.1



264
265
266
# File 'lib/rdf/model/literal/double.rb', line 264

def to_i
  @object.to_i
end

- (Rational) to_r

Returns the value as a rational number.

Returns:

  • (Rational)

Since:

  • 0.2.1



288
289
290
# File 'lib/rdf/model/literal/double.rb', line 288

def to_r
  @object.to_r # only available on Ruby 1.9+
end

- (String) to_s

Returns the value as a string.

Returns:

  • (String)

Since:

  • 0.2.1



252
253
254
255
256
257
258
# File 'lib/rdf/model/literal/double.rb', line 252

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

- (Boolean) zero?

Returns true if the value is zero.

Returns:

Since:

  • 0.2.3



177
178
179
# File 'lib/rdf/model/literal/double.rb', line 177

def zero?
  to_f.zero?
end