# Class: RDF::Literal::Numeric

Inherits:
RDF::Literal
show all
Defined in:
lib/rdf/model/literal/numeric.rb

## Overview

Shared methods and class ancestry for numeric literal classes.

Since:

• 0.3.0

## Instance Method Summary collapse

• Exponent − Performs remainder of `self` divided by `other`.

• Returns the product of `self` times `other`.

• Exponent − Performs exponential (power) calculation on operators.

• Returns the sum of `self` plus `other`.

• Returns `self`.

• Returns the difference of `self` minus `other`.

• Returns `self` negated.

• Returns the quotient of `self` divided by `other`.

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

• Returns `true` if this literal is equal to `other`.

• Returns the absolute value of `self`.

• Returns the arc cosine of the argument.

• Returns the arc sine of the argument.

• Returns the arc tangent of the argument.

• Returns the angle in radians subtended at the origin by the point on a plane with coordinates (x, y) and the positive x-axis.

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

• Returns the cosine of the argument.

• Returns the value of `e``x`.

• Returns the value of `10``x`.

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

• Returns the natural logarithm of the argument.

• Returns the base-ten logarithm of the argument.

• Returns the number with no fractional part that is closest to the argument.

• Returns the sine of the argument.

• Returns the non-negative square root of the argument.

• Returns the tangent of the argument.

• Returns the value as a decimal number.

• Returns the value as a floating point number.

• #to_i ⇒ Integer (also: #to_int, #ord)

Returns the value as an integer.

• Returns the value as a rational number.

## Constructor Details

This class inherits a constructor from RDF::Literal

## Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class RDF::Literal

## Instance Method Details

### #%(other) ⇒ RDF::Literal

Exponent − Performs remainder of `self` divided by `other`.

From the XQuery function math:mod.

Parameters:

Returns:

Since:

• 0.2.3

 ``` 157 158 159 160 161 162 163 164 165 166 167``` ```# File 'lib/rdf/model/literal/numeric.rb', line 157 def %(other) if self.class == Double || [Double, ::Float].include?(other.class) self.class.new(to_f % other.to_f) elsif ((self.class == RDF::Literal::Float || other.class == RDF::Literal::Float) rescue false) self.class.new(to_f % other.to_f) elsif self.class == Decimal || other.class == Decimal self.class.new(to_d % (other.respond_to?(:to_d) ? other.to_d : BigDecimal(other.to_s))) else self.class.new(to_i % other.to_i) end end```

### #*(other) ⇒ RDF::Literal::Numeric

Returns the product of `self` times `other`.

From the XQuery function op:numeric-multiply.

Parameters:

Returns:

Since:

• 0.2.3

 ``` 119 120 121 122 123 124 125 126 127 128 129``` ```# File 'lib/rdf/model/literal/numeric.rb', line 119 def *(other) if self.class == Double || [Double, ::Float].include?(other.class) RDF::Literal::Double.new(to_f * other.to_f) elsif ((self.class == RDF::Literal::Float || other.class == RDF::Literal::Float) rescue false) 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```

### #**(other) ⇒ RDF::Literal::Numeric

Exponent − Performs exponential (power) calculation on operators.

Promotes values, as necessary, with the result type depending on the input values.

From the XQuery function math:pow.

Parameters:

Returns:

Since:

• 0.2.3

 ``` 142 143 144 145 146``` ```# File 'lib/rdf/model/literal/numeric.rb', line 142 def **(other) RDF::Literal(object ** (other.is_a?(RDF::Literal::Numeric) ? other.object : other)) rescue ZeroDivisionError RDF::Literal::Double.new('INF') end```

### #+(other) ⇒ RDF::Literal::Numeric

Note:

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.

Returns the sum of `self` plus `other`.

From the XQuery function op:numeric-add.

Parameters:

Returns:

Since:

• 0.2.3

 ``` 77 78 79 80 81 82 83 84 85 86 87``` ```# File 'lib/rdf/model/literal/numeric.rb', line 77 def +(other) if self.class == Double || [Double, ::Float].include?(other.class) RDF::Literal::Double.new(to_f + other.to_f) elsif ((self.class == RDF::Literal::Float || other.class == RDF::Literal::Float) rescue false) 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`.

Returns:

Since:

• 0.2.3

 ``` 54 55 56``` ```# File 'lib/rdf/model/literal/numeric.rb', line 54 def +@ self # unary plus end```

### #-(other) ⇒ RDF::Literal::Numeric

Returns the difference of `self` minus `other`.

From the XQuery function op:numeric-subtract.

Parameters:

Returns:

Since:

• 0.2.3

 ``` 98 99 100 101 102 103 104 105 106 107 108``` ```# File 'lib/rdf/model/literal/numeric.rb', line 98 def -(other) if self.class == Double || [Double, ::Float].include?(other.class) RDF::Literal::Double.new(to_f - other.to_f) elsif ((self.class == RDF::Literal::Float || other.class == RDF::Literal::Float) rescue false) 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.

Returns:

Since:

• 0.2.3

 ``` 63 64 65``` ```# File 'lib/rdf/model/literal/numeric.rb', line 63 def -@ self.class.new(-self.object) end```

### #/(other) ⇒ RDF::Literal::Numeric

Returns the quotient of `self` divided by `other`.

As a special case, if the types of both \$arg1 and \$arg2 are xsd:integer, then the return type is xsd:decimal.

From the XQuery function op:numeric-divide.

Parameters:

Returns:

Raises:

• (ZeroDivisionError)

if divided by zero

Since:

• 0.2.3

 ``` 182 183 184 185 186 187 188 189 190``` ```# File 'lib/rdf/model/literal/numeric.rb', line 182 def /(other) if self.class == Double || [Double, ::Float].include?(other.class) RDF::Literal::Double.new(to_f / other.to_f) elsif ((self.class == RDF::Literal::Float || other.class == RDF::Literal::Float) rescue false) RDF::Literal::Float.new(to_f / other.to_f) else RDF::Literal::Decimal.new(to_d / (other.respond_to?(:to_d) ? other.to_d : BigDecimal(other.to_s))) end end```

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

Compares this literal to `other` for sorting purposes.

Parameters:

• other (Object)

Returns:

• (Integer)

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

Since:

• 0.3.0

 ``` 13 14 15 16 17 18 19 20 21 22 23 24 25 26``` ```# File 'lib/rdf/model/literal/numeric.rb', line 13 def <=>(other) # If lexically invalid, use regular literal testing return super unless self.valid? && (!other.respond_to?(:valid?) || other.valid?) case other when ::Numeric to_d <=> other when Double to_f <=> other.to_f when Numeric to_d <=> other.to_d else super end end```

### #==(other) ⇒ Boolean

Returns `true` if this literal is equal to `other`.

Parameters:

• other (Object)

Returns:

• (Boolean)

`true` or `false`

Since:

• 0.3.0

 ``` 34 35 36 37 38 39 40 41 42 43 44 45 46 47``` ```# File 'lib/rdf/model/literal/numeric.rb', line 34 def ==(other) # If lexically invalid, use regular literal testing return super unless self.valid? && (!other.respond_to?(:valid?) || other.valid?) case other when Literal::Numeric (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```

### #abs ⇒ RDF::Literal

Returns the absolute value of `self`.

From the XQuery function fn:abs.

Returns:

Raises:

• (NotImplementedError)

unless implemented in subclass

Since:

• 0.3.0

 ``` 200 201 202``` ```# File 'lib/rdf/model/literal/numeric.rb', line 200 def abs raise NotImplementedError end```

### #acos ⇒ Double

Returns the arc cosine of the argument.

Returns:

Since:

• 0.3.0

 ``` 344 345 346 347 348``` ```# File 'lib/rdf/model/literal/numeric.rb', line 344 def acos Double.new(Math.acos(self.to_f)) rescue Math::DomainError Double.new(::Float::NAN) end```

### #asin ⇒ Double

Returns the arc sine of the argument.

Returns:

Since:

• 0.3.0

 ``` 333 334 335 336 337``` ```# File 'lib/rdf/model/literal/numeric.rb', line 333 def asin Double.new(Math.asin(self.to_f)) rescue Math::DomainError Double.new(::Float::NAN) end```

### #atan ⇒ Double

Returns the arc tangent of the argument.

Returns:

Since:

• 0.3.0

 ``` 355 356 357 358 359``` ```# File 'lib/rdf/model/literal/numeric.rb', line 355 def atan Double.new(Math.atan(self.to_f)) rescue Math::DomainError Double.new(::Float::NAN) end```

### #atan2(arg) ⇒ Double

Returns the angle in radians subtended at the origin by the point on a plane with coordinates (x, y) and the positive x-axis.

Parameters:

Returns:

Since:

• 0.3.0

 ``` 367 368 369 370 371``` ```# File 'lib/rdf/model/literal/numeric.rb', line 367 def atan2(arg) Double.new(Math.atan2(self.to_f, arg.to_f)) rescue Math::DomainError Double.new(::Float::NAN) end```

### #ceil ⇒ RDF::Literal

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

From the XQuery function fn:ceil.

#### Examples:

``RDF::Literal(1).ceil            #=> RDF::Literal(1)``

Returns:

Since:

• 0.3.0

 ``` 226 227 228``` ```# File 'lib/rdf/model/literal/numeric.rb', line 226 def ceil self end```

### #cos ⇒ Double

Returns the cosine of the argument. The argument is an angle in radians.

Returns:

Since:

• 0.3.0

 ``` 311 312 313 314 315``` ```# File 'lib/rdf/model/literal/numeric.rb', line 311 def cos Double.new(Math.cos(self.to_f)) rescue Math::DomainError Double.new(::Float::NAN) end```

### #exp ⇒ Double

Returns the value of `e``x`.

Returns:

Since:

• 0.3.0

 ``` 249 250 251``` ```# File 'lib/rdf/model/literal/numeric.rb', line 249 def exp Double.new(Math.exp(self.to_f)) end```

### #exp10 ⇒ Double

Returns the value of `10``x`.

Returns:

Since:

• 0.3.0

 ``` 258 259 260``` ```# File 'lib/rdf/model/literal/numeric.rb', line 258 def exp10 Double.new(10**self.to_f) end```

### #floor ⇒ RDF::Literal

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

From the XQuery function fn:floor.

#### Examples:

``RDF::Literal(1).floor            #=> RDF::Literal(1)``

Returns:

Since:

• 0.3.0

 ``` 240 241 242``` ```# File 'lib/rdf/model/literal/numeric.rb', line 240 def floor self end```

### #log ⇒ Double

Returns the natural logarithm of the argument.

Returns:

Since:

• 0.3.0

 ``` 267 268 269 270 271``` ```# File 'lib/rdf/model/literal/numeric.rb', line 267 def log Double.new(Math.log(self.to_f)) rescue Math::DomainError Double.new(::Float::NAN) end```

### #log10 ⇒ Double

Returns the base-ten logarithm of the argument.

Returns:

Since:

• 0.3.0

 ``` 278 279 280 281 282``` ```# File 'lib/rdf/model/literal/numeric.rb', line 278 def log10 Double.new(Math.log10(self.to_f)) rescue Math::DomainError Double.new(::Float::NAN) end```

### #round ⇒ RDF::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.

From the XQuery function fn:round.

Returns:

Raises:

• (NotImplementedError)

unless implemented in subclass

Since:

• 0.3.0

 ``` 212 213 214``` ```# File 'lib/rdf/model/literal/numeric.rb', line 212 def round raise NotImplementedError end```

### #sin ⇒ Double

Returns the sine of the argument. The argument is an angle in radians.

Returns:

Since:

• 0.3.0

 ``` 300 301 302 303 304``` ```# File 'lib/rdf/model/literal/numeric.rb', line 300 def sin Double.new(Math.sin(self.to_f)) rescue Math::DomainError Double.new(::Float::NAN) end```

### #sqrt ⇒ Double

Returns the non-negative square root of the argument.

Returns:

Since:

• 0.3.0

 ``` 289 290 291 292 293``` ```# File 'lib/rdf/model/literal/numeric.rb', line 289 def sqrt Double.new(Math.sqrt(self.to_f)) rescue Math::DomainError Double.new(::Float::NAN) end```

### #tan ⇒ Double

Returns the tangent of the argument. The argument is an angle in radians.

Returns:

Since:

• 0.3.0

 ``` 322 323 324 325 326``` ```# File 'lib/rdf/model/literal/numeric.rb', line 322 def tan Double.new(Math.tan(self.to_f)) rescue Math::DomainError Double.new(::Float::NAN) end```

### #to_d ⇒ BigDecimal

Returns the value as a decimal number.

Returns:

• (BigDecimal)

Since:

• 0.3.0

 ``` 398 399 400 401 402``` ```# File 'lib/rdf/model/literal/numeric.rb', line 398 def to_d @object.respond_to?(:to_d) ? @object.to_d : BigDecimal(@object.to_s) rescue FloatDomainError ::Float::NAN end```

### #to_f ⇒ Float

Returns the value as a floating point number.

The usual accuracy limits and errors of binary float arithmetic apply.

Returns:

• (Float)

• BigDecimal#to_f

Since:

• 0.3.0

 ``` 390 391 392``` ```# File 'lib/rdf/model/literal/numeric.rb', line 390 def to_f @object.to_f end```

### #to_i ⇒ IntegerAlso known as: to_int, ord

Returns the value as an integer.

Returns:

Since:

• 0.3.0

 ``` 377 378 379``` ```# File 'lib/rdf/model/literal/numeric.rb', line 377 def to_i @object.to_i end```

### #to_r ⇒ Rational

Returns the value as a rational number.

Returns:

• (Rational)

Since:

• 0.3.0

 ``` 408 409 410``` ```# File 'lib/rdf/model/literal/numeric.rb', line 408 def to_r @object.to_r end```