Skip to content

Commit 168a359

Browse files
committed
Add XPath math functions and constants to Literal::Numeric. PI, #exp, #exp10, #log, #log10, #sqrt, #sin, #cos, #tan, #asin, #acos, #atan, #atan2.
1 parent c9a7677 commit 168a359

3 files changed

Lines changed: 453 additions & 8 deletions

File tree

lib/rdf/model/literal/double.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ def initialize(value, datatype: nil, lexical: nil, **options)
3434
end
3535
end
3636

37+
# Approximation of the mathematical constant π
38+
#
39+
# From the XQuery function [math:pi](https://www.w3.org/TR/xpath-functions/#func-math-pi).
40+
#
41+
# @return [Double]
42+
# @see https://www.w3.org/TR/xpath-functions/#func-math-pi
43+
PI = Double.new(Math::PI)
44+
3745
##
3846
# Converts this literal into its canonical lexical representation.
3947
#

lib/rdf/model/literal/numeric.rb

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,135 @@ def floor
241241
self
242242
end
243243

244+
##
245+
# Returns the value of `e`<sup>`x`</sup>.
246+
#
247+
# @return [Double]
248+
# @see https://www.w3.org/TR/xpath-functions/#func-math-exp
249+
def exp
250+
Double.new(Math.exp(self.to_f))
251+
end
252+
253+
##
254+
# Returns the value of `10`<sup>`x`</sup>.
255+
#
256+
# @return [Double]
257+
# @see https://www.w3.org/TR/xpath-functions/#func-math-exp10
258+
def exp10
259+
Double.new(10**self.to_f)
260+
end
261+
262+
##
263+
# Returns the natural logarithm of the argument.
264+
#
265+
# @return [Double]
266+
# @see https://www.w3.org/TR/xpath-functions/#func-math-log
267+
def log
268+
Double.new(Math.log(self.to_f))
269+
rescue Math::DomainError
270+
Double.new(::Float::NAN)
271+
end
272+
273+
##
274+
# Returns the base-ten logarithm of the argument.
275+
#
276+
# @return [Double]
277+
# @see https://www.w3.org/TR/xpath-functions/#func-math-log10
278+
def log10
279+
Double.new(Math.log10(self.to_f))
280+
rescue Math::DomainError
281+
Double.new(::Float::NAN)
282+
end
283+
284+
##
285+
# Returns the non-negative square root of the argument.
286+
#
287+
# @return [Double]
288+
# @see https://www.w3.org/TR/xpath-functions/#func-math-sqrt
289+
def sqrt
290+
Double.new(Math.sqrt(self.to_f))
291+
rescue Math::DomainError
292+
Double.new(::Float::NAN)
293+
end
294+
295+
##
296+
# Returns the sine of the argument. The argument is an angle in radians.
297+
#
298+
# @return [Double]
299+
# @see https://www.w3.org/TR/xpath-functions/#func-math-sin
300+
def sin
301+
Double.new(Math.sin(self.to_f))
302+
rescue Math::DomainError
303+
Double.new(::Float::NAN)
304+
end
305+
306+
##
307+
# Returns the cosine of the argument. The argument is an angle in radians.
308+
#
309+
# @return [Double]
310+
# @see https://www.w3.org/TR/xpath-functions/#func-math-cos
311+
def cos
312+
Double.new(Math.cos(self.to_f))
313+
rescue Math::DomainError
314+
Double.new(::Float::NAN)
315+
end
316+
317+
##
318+
# Returns the tangent of the argument. The argument is an angle in radians.
319+
#
320+
# @return [Double]
321+
# @see https://www.w3.org/TR/xpath-functions/#func-math-tan
322+
def tan
323+
Double.new(Math.tan(self.to_f))
324+
rescue Math::DomainError
325+
Double.new(::Float::NAN)
326+
end
327+
328+
##
329+
# Returns the arc sine of the argument.
330+
#
331+
# @return [Double]
332+
# @see https://www.w3.org/TR/xpath-functions/#func-math-asin
333+
def asin
334+
Double.new(Math.asin(self.to_f))
335+
rescue Math::DomainError
336+
Double.new(::Float::NAN)
337+
end
338+
339+
##
340+
# Returns the arc cosine of the argument.
341+
#
342+
# @return [Double]
343+
# @see https://www.w3.org/TR/xpath-functions/#func-math-acos
344+
def acos
345+
Double.new(Math.acos(self.to_f))
346+
rescue Math::DomainError
347+
Double.new(::Float::NAN)
348+
end
349+
350+
##
351+
# Returns the arc tangent of the argument.
352+
#
353+
# @return [Double]
354+
# @see https://www.w3.org/TR/xpath-functions/#func-math-atan
355+
def atan
356+
Double.new(Math.atan(self.to_f))
357+
rescue Math::DomainError
358+
Double.new(::Float::NAN)
359+
end
360+
361+
##
362+
# Returns the angle in radians subtended at the origin by the point on a plane with coordinates (x, y) and the positive x-axis.
363+
#
364+
# @param [#to_f] arg
365+
# @return [Double]
366+
# @see https://www.w3.org/TR/xpath-functions/#func-math-atan2
367+
def atan2(arg)
368+
Double.new(Math.atan2(self.to_f, arg.to_f))
369+
rescue Math::DomainError
370+
Double.new(::Float::NAN)
371+
end
372+
244373
##
245374
# Returns the value as an integer.
246375
#

0 commit comments

Comments
 (0)