# Class: RDF::Literal::Double

Inherits:
Numeric
show all
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)``````

Since:

• 0.2.1

## Constant Summary

DATATYPE =

Since:

• 0.2.1

`XSD.double`
GRAMMAR =

Since:

• 0.2.1

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

## Instance Method Summary (collapse)

• Compares this literal to `other` for sorting purposes.

• Returns the absolute value of `self`.

• Converts this literal into its canonical lexical representation.

• Returns the smallest integer greater than or equal to `self`.

• Returns `true` if the value is a valid IEEE floating point number (it is not infinite, and `nan?` is `false`).

• Returns the largest integer less than or equal to `self`.

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

• constructor

A new instance of Double.

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

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

• Returns the value as a string.

• Returns `true` if the value is zero.

### Methods inherited from Numeric

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

## Constructor Details

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

Returns a new instance of Double

Parameters:

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

a customizable set of options

Options Hash (options):

• :lexical (String) — default: nil

Since:

• 0.2.1

 ``` 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, options = {}) @datatype = RDF::URI(options[:datatype] || self.class.const_get(:DATATYPE)) @string = options[:lexical] if options.has_key?(:lexical) @string ||= value if 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

### - (Integer) <=>(other)

Compares this literal to `other` for sorting purposes.

Parameters:

• other (Object)

Returns:

• (Integer)

`-1`, `0`, or `1`

Since:

• 0.3.0

 ``` 66 67 68 69 70 71 72 73 74``` ```# File 'lib/rdf/model/literal/double.rb', line 66 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```

### - (RDF::Literal) abs

Returns the absolute value of `self`.

Returns:

Since:

• 0.2.3

 ``` 155 156 157``` ```# File 'lib/rdf/model/literal/double.rb', line 155 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:

• `self`

Since:

• 0.2.1

 ``` 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58``` ```# File '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 = ('%.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

 ``` 131 132 133``` ```# File 'lib/rdf/model/literal/double.rb', line 131 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

 ``` 101 102 103``` ```# File 'lib/rdf/model/literal/double.rb', line 101 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

 ``` 146 147 148``` ```# File 'lib/rdf/model/literal/double.rb', line 146 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

 ``` 116 117 118``` ```# File 'lib/rdf/model/literal/double.rb', line 116 def infinite? to_f.infinite? end```

### - (Boolean) nan?

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

 ``` 86 87 88``` ```# File 'lib/rdf/model/literal/double.rb', line 86 def nan? to_f.nan? end```

### - (Boolean) nonzero?

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

Returns:

Since:

• 0.2.3

 ``` 173 174 175``` ```# File 'lib/rdf/model/literal/double.rb', line 173 def nonzero? to_f.nonzero? ? self : nil end```

### - (String) to_s

Returns the value as a string.

Returns:

• (String)

Since:

• 0.2.1

 ``` 181 182 183 184 185 186 187``` ```# File 'lib/rdf/model/literal/double.rb', line 181 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

 ``` 164 165 166``` ```# File 'lib/rdf/model/literal/double.rb', line 164 def zero? to_f.zero? end```