Class: XPath::XPathNumber

Inherits:
Object
  • Object
show all
Includes:
XPathObject
Defined in:
lib/xml/xpath.rb

Instance Method Summary collapse

Methods included from XPathObject

#**, #<, #<=, #>, #>=, #at, #funcall, #predicate, #to_boolean, #to_string

Constructor Details

#initialize(num) ⇒ XPathNumber

Returns a new instance of XPathNumber.

Raises:

  • (::TypeError)


685
686
687
688
# File 'lib/xml/xpath.rb', line 685

def initialize(num)
  raise ::TypeError, "must be a Float" unless num.is_a? Float
  @value = num
end

Instance Method Details

#%(other) ⇒ Object



753
754
755
756
757
758
759
760
# File 'lib/xml/xpath.rb', line 753

def %(other)
  n = other.to_f
  f = @value % n
  f = -f if @value < 0
  f = -f if n < 0
  @value = f
  self
end

#*(other) ⇒ Object



743
744
745
746
# File 'lib/xml/xpath.rb', line 743

def *(other)
  @value *= other.to_f
  self
end

#+(other) ⇒ Object



733
734
735
736
# File 'lib/xml/xpath.rb', line 733

def +(other)
  @value += other.to_f
  self
end

#-(other) ⇒ Object



738
739
740
741
# File 'lib/xml/xpath.rb', line 738

def -(other)
  @value -= other.to_f
  self
end

#-@Object



762
763
764
765
# File 'lib/xml/xpath.rb', line 762

def -@
  @value = -@value
  self
end

#/(other) ⇒ Object



748
749
750
751
# File 'lib/xml/xpath.rb', line 748

def /(other)
  @value /= other.to_f
  self
end

#==(other) ⇒ Object



725
726
727
728
729
730
731
# File 'lib/xml/xpath.rb', line 725

def ==(other)
  if other.is_a? XPathNodeSet or other.is_a? XPathBoolean then
    other == self
  else
    @value == other.to_f
  end
end

#ceilObject



772
773
774
775
# File 'lib/xml/xpath.rb', line 772

def ceil
  @value = @value.ceil.to_f
  self
end

#floorObject



767
768
769
770
# File 'lib/xml/xpath.rb', line 767

def floor
  @value = @value.floor.to_f
  self
end

#roundObject



777
778
779
780
781
782
783
784
785
786
787
788
789
# File 'lib/xml/xpath.rb', line 777

def round
  f = @value
  unless f.nan? or f.infinite? then
    if f >= 0.0 then
      @value = f.round.to_f
    elsif f - f.truncate >= -0.5 then
      @value = f.ceil.to_f
    else
      @value = f.floor.to_f
    end
  end
  self
end

#to_fObject



704
705
706
# File 'lib/xml/xpath.rb', line 704

def to_f
  @value
end

#to_number(context) ⇒ Object



720
721
722
# File 'lib/xml/xpath.rb', line 720

def to_number(context)
  self
end

#to_predicateObject



716
717
718
# File 'lib/xml/xpath.rb', line 716

def to_predicate
  to_f
end

#to_rubyObject



712
713
714
# File 'lib/xml/xpath.rb', line 712

def to_ruby
  to_f
end

#to_strObject



690
691
692
693
694
695
696
697
698
699
700
701
702
# File 'lib/xml/xpath.rb', line 690

def to_str
  if @value.nan? then
    'NaN'
  elsif @value.infinite? then
    if @value < 0 then
      '-Infinity'
    else
      'Infinity'
    end
  else
    sprintf("%.100f", @value).gsub(/\.?0+\z/, '')    # enough?
  end
end

#true?Boolean

Returns:

  • (Boolean)


708
709
710
# File 'lib/xml/xpath.rb', line 708

def true?
  @value != 0.0 and not @value.nan?
end