lib/rdf/model/literal/double.rb in rdf-0.2.2 vs lib/rdf/model/literal/double.rb in rdf-0.2.3

- old
+ new

@@ -1,9 +1,15 @@ module RDF; class Literal ## # An floating point number literal. # + # @example 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 http://www.w3.org/TR/xmlschema-2/#double # @since 0.2.1 class Double < Literal DATATYPE = XSD.double GRAMMAR = /^[\+\-]?\d+(\.\d*([eE][\+\-]?\d+)?)?$/.freeze # FIXME: support 'INF', '-INF' and 'NaN' @@ -41,9 +47,168 @@ 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 unless @object.nil? self + end + + ## + # Returns `true` if the value is an invalid IEEE floating point number. + # + # @example + # RDF::Literal(-1.0).nan? #=> false + # RDF::Literal(1.0/0.0).nan? #=> false + # RDF::Literal(0.0/0.0).nan? #=> true + # + # @return [Boolean] + # @since 0.2.3 + def nan? + to_f.nan? + end + + ## + # Returns `true` if the value is a valid IEEE floating point number (it + # is not infinite, and `nan?` is `false`). + # + # @example + # RDF::Literal(-1.0).finite? #=> true + # RDF::Literal(1.0/0.0).finite? #=> false + # RDF::Literal(0.0/0.0).finite? #=> false + # + # @return [Boolean] + # @since 0.2.3 + def finite? + to_f.finite? + end + + ## + # Returns `nil`, `-1`, or `+1` depending on whether the value is finite, + # `-INF`, or `+INF`. + # + # @example + # RDF::Literal(0.0/0.0).infinite? #=> nil + # RDF::Literal(-1.0/0.0).infinite? #=> -1 + # RDF::Literal(+1.0/0.0).infinite? #=> 1 + # + # @return [Integer] + # @since 0.2.3 + def infinite? + to_f.infinite? + end + + ## + # Returns the smallest integer greater than or equal to `self`. + # + # @example + # 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) + # + # @return [RDF::Literal] + # @since 0.2.3 + def ceil + RDF::Literal(to_f.ceil) + end + + ## + # Returns the largest integer less than or equal to `self`. + # + # @example + # 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) + # + # @return [RDF::Literal] + # @since 0.2.3 + def floor + RDF::Literal(to_f.floor) + end + + ## + # Returns the absolute value of `self`. + # + # @return [RDF::Literal] + # @since 0.2.3 + def abs + (f = to_f) && f > 0 ? self : RDF::Literal(f.abs) + end + + ## + # Returns `true` if the value is zero. + # + # @return [Boolean] + # @since 0.2.3 + def zero? + to_f.zero? + end + + ## + # Returns `self` if the value is not zero, `nil` otherwise. + # + # @return [Boolean] + # @since 0.2.3 + def nonzero? + to_f.nonzero? ? self : nil + end + + ## + # Returns `self`. + # + # @return [RDF::Literal] + # @since 0.2.3 + def +@ + self # unary plus + end + + ## + # Returns `self` negated. + # + # @return [RDF::Literal] + # @since 0.2.3 + def -@ + RDF::Literal(-to_f) # unary minus + end + + ## + # Returns the sum of `self` plus `other`. + # + # @param [#to_f] other + # @return [RDF::Literal] + # @since 0.2.3 + def +(other) + RDF::Literal(to_f + other.to_f) + end + + ## + # Returns the difference of `self` minus `other`. + # + # @param [#to_f] other + # @return [RDF::Literal] + # @since 0.2.3 + def -(other) + RDF::Literal(to_f - other.to_f) + end + + ## + # Returns the product of `self` times `other`. + # + # @param [#to_f] other + # @return [RDF::Literal] + # @since 0.2.3 + def *(other) + RDF::Literal(to_f * other.to_f) + end + + ## + # Returns the quotient of `self` divided by `other`. + # + # @param [#to_f] other + # @return [RDF::Literal] + # @since 0.2.3 + def /(other) + RDF::Literal(to_f / other.to_f) end ## # Returns the value as a string. #