Class: Radix::Float

Inherits:
Numeric
  • Object
show all
Defined in:
lib/radix/float.rb

Overview

Radix::Float is simple a Ruby Float class that can handle bases.

TODO: Make fully immutable. After that we can catch @digits and the library should be a good bit faster.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Numeric

#*, #+, #-, #/

Instance Attribute Details

#baseObject (readonly)

Base of the number.



15
16
17
# File 'lib/radix/float.rb', line 15

def base
  @base
end

#codeObject (readonly)

Base encoding table.



18
19
20
# File 'lib/radix/float.rb', line 18

def code
  @code
end

#valueObject (readonly)

Internal floating point value.



12
13
14
# File 'lib/radix/float.rb', line 12

def value
  @value
end

Instance Method Details

#%(other) ⇒ Object Also known as: modulo

Modulo



120
121
122
# File 'lib/radix/float.rb', line 120

def %(other)
  operation(:%, other)
end

#**(other) ⇒ Object

Power



115
116
117
# File 'lib/radix/float.rb', line 115

def **(other)
  operation(:**, other)
end

#<=>(other) ⇒ Object



165
166
167
# File 'lib/radix/float.rb', line 165

def <=>(other)
  to_f <=> other.to_f
end

#==(other) ⇒ Object

Simple equality requires equal values only.



155
156
157
158
159
160
161
162
# File 'lib/radix/float.rb', line 155

def ==(other)
  case other
  when Float, Integer  # Radix
    value == other.value
  else
    value == other
  end
end

#absObject



128
129
130
# File 'lib/radix/float.rb', line 128

def abs
  self.class.new(value.abs, base)
end

#ceilObject



133
134
135
# File 'lib/radix/float.rb', line 133

def ceil
  self.class.new(value.ceil, base)
end

#coerce(o) ⇒ Object



185
186
187
# File 'lib/radix/float.rb', line 185

def coerce(o)
  [Radix::Float.new(o), self]  
end

#convert(new_base) ⇒ Object



110
111
112
# File 'lib/radix/float.rb', line 110

def convert(new_base)
  self.class.new(value, new_base)
end

#digitsObject



90
91
92
93
94
95
96
97
# File 'lib/radix/float.rb', line 90

def digits
  i, f = base_conversion(value, base)
  if negative?
    ['-'] + i + [DOT] + f
  else
    i + [DOT] + f
  end
end

#digits_encodedObject



100
101
102
# File 'lib/radix/float.rb', line 100

def digits_encoded
  base_encode(digits)
end

#eql?(num) ⇒ Boolean

Strict equality requires same class as well as value.

Returns:

  • (Boolean)


150
151
152
# File 'lib/radix/float.rb', line 150

def eql?(num)
  self.class.equal?(num.class) && self == num
end

#floorObject



138
139
140
# File 'lib/radix/float.rb', line 138

def floor
  self.class.new(value.floor, base)
end

#inspectObject



85
86
87
# File 'lib/radix/float.rb', line 85

def inspect
  "#{digits.join(' ')} (#{base})"
end

#negative?Boolean

Returns true if the number negative?

Returns:

  • (Boolean)


105
106
107
# File 'lib/radix/float.rb', line 105

def negative?
  value < 0
end

#roundObject



143
144
145
146
147
# File 'lib/radix/float.rb', line 143

def round
  return self.class.new((value + 0.5).floor, base) if self > 0.0
  return self.class.new((value - 0.5).ceil,  base) if self < 0.0
  return self.class.new(0, base)
end

#to_a(base = nil) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/radix/float.rb', line 58

def to_a(base=nil)
  if base
    convert(base).digits_encoded
  else
    digits_encoded
  end
end

#to_fObject



53
54
55
# File 'lib/radix/float.rb', line 53

def to_f
  value.to_f
end

#to_iObject Also known as: to_int



45
46
47
# File 'lib/radix/float.rb', line 45

def to_i
  to_f.to_i
end

#to_s(base = nil, divider = nil) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/radix/float.rb', line 67

def to_s(base=nil, divider=nil)
  divider = divider.to_s if divider
  if base
    convert(base).to_s(nil, divider)
  else
    if code
      digits_encoded.join(divider)
    else
      if @base > 10
        digits.join(divider || DIVIDER)
      else
        digits.join(divider)
      end
    end
  end
end