Class: FixedPoint
- Inherits:
-
Object
show all
- Defined in:
- lib/wdd-ruby-ext/utils/fixed_point.rb
Constant Summary
collapse
- PRECISION =
2
- ARITHMETIC_METHODS =
%w{+ -}
- COMPARE_METHODS =
%w{> < >= <= <=>}
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(initial = 0.0, raw_value = nil, precision = PRECISION) ⇒ FixedPoint
Returns a new instance of FixedPoint.
14
15
16
17
18
19
20
21
22
|
# File 'lib/wdd-ruby-ext/utils/fixed_point.rb', line 14
def initialize( initial = 0.0, raw_value = nil, precision=PRECISION )
@precision = precision
@factor = 10**@precision
if raw_value
@value = raw_value
else
@value = (BigDecimal.new(initial.to_s.gsub(',',''), precision)*@factor).to_i
end
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object
73
74
75
76
77
78
79
80
81
82
83
84
|
# File 'lib/wdd-ruby-ext/utils/fixed_point.rb', line 73
def method_missing method, *args
if ARITHMETIC_METHODS.include? method.to_s
arg = (args[0] * @factor).to_i
result = @value.send(method, arg)
new_obj = FixedPoint.new(0, result.to_i, @precision)
elsif COMPARE_METHODS.include? method.to_s
arg = (args[0] * @factor).to_i
result = @value.send(method, arg)
else
super
end
end
|
Instance Attribute Details
Returns the value of attribute factor.
11
12
13
|
# File 'lib/wdd-ruby-ext/utils/fixed_point.rb', line 11
def factor
@factor
end
|
#precision ⇒ Object
Returns the value of attribute precision.
12
13
14
|
# File 'lib/wdd-ruby-ext/utils/fixed_point.rb', line 12
def precision
@precision
end
|
Returns the value of attribute value.
10
11
12
|
# File 'lib/wdd-ruby-ext/utils/fixed_point.rb', line 10
def value
@value
end
|
Instance Method Details
55
56
57
58
59
60
61
62
|
# File 'lib/wdd-ruby-ext/utils/fixed_point.rb', line 55
def *( arg )
if arg.is_a? FixedPoint
result = (@value * arg.value)/arg.factor
else
result = (@value * (arg*@factor).to_i) / @factor
end
new_obj = FixedPoint.new(0, result.to_i, @precision)
end
|
51
52
53
|
# File 'lib/wdd-ruby-ext/utils/fixed_point.rb', line 51
def -@
FixedPoint.new(0, -@value, @precision)
end
|
64
65
66
67
68
69
70
71
|
# File 'lib/wdd-ruby-ext/utils/fixed_point.rb', line 64
def /( arg )
if arg.is_a? FixedPoint
result = (@value*arg.factor) * arg.value
else
result = (@value*@factor) / (arg*@factor).to_i
end
new_obj = FixedPoint.new(0, result.to_i, @precision)
end
|
33
34
35
36
37
38
39
40
41
|
# File 'lib/wdd-ruby-ext/utils/fixed_point.rb', line 33
def == obj
if obj.is_a? Numeric
@value == (obj * @factor).to_i
elsif obj.is_a? FixedPoint
@value == obj.value
else
false
end
end
|
43
44
45
46
47
48
49
|
# File 'lib/wdd-ruby-ext/utils/fixed_point.rb', line 43
def abs
if @value > 0
FixedPoint.new(0, @value, @precision)
else
FixedPoint.new(0, -@value, @precision)
end
end
|
29
30
31
|
# File 'lib/wdd-ruby-ext/utils/fixed_point.rb', line 29
def to_i
(@value / @factor).to_i
end
|
24
25
26
27
|
# File 'lib/wdd-ruby-ext/utils/fixed_point.rb', line 24
def to_s
@value.to_s
"%.0#{@precision}f" % (@value.to_f/@factor)
end
|