Class: Basic101::BasicNumeric
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from BasicMath
basic_math_op
comparison_op
Methods inherited from BasicObject
#eval, #to_float, #to_integer, #to_string, type_name, #type_name
Constructor Details
Returns a new instance of BasicNumeric.
17
18
19
|
# File 'lib/basic101/basic_numeric.rb', line 17
def initialize(value)
@value = value
end
|
Instance Attribute Details
#value ⇒ Object
Returns the value of attribute value.
15
16
17
|
# File 'lib/basic101/basic_numeric.rb', line 15
def value
@value
end
|
Instance Method Details
#<=>(other) ⇒ Object
21
22
23
24
|
# File 'lib/basic101/basic_numeric.rb', line 21
def <=>(other)
return nil unless other.is_a?(BasicNumeric)
value <=> other.value
end
|
#abs ⇒ Object
64
65
66
|
# File 'lib/basic101/basic_numeric.rb', line 64
def abs
self.class.new(@value.abs)
end
|
#cos ⇒ Object
88
89
90
|
# File 'lib/basic101/basic_numeric.rb', line 88
def cos
BasicFloat.new(Math.cos(@value))
end
|
#exp ⇒ Object
72
73
74
|
# File 'lib/basic101/basic_numeric.rb', line 72
def exp
BasicFloat.new(Math::E ** @value).simplest
end
|
#log ⇒ Object
80
81
82
|
# File 'lib/basic101/basic_numeric.rb', line 80
def log
BasicFloat.new(Math.log(@value))
end
|
#negate ⇒ Object
50
51
52
|
# File 'lib/basic101/basic_numeric.rb', line 50
def negate
self.class.new(-value)
end
|
#print_new_line(output) ⇒ Object
60
61
62
|
# File 'lib/basic101/basic_numeric.rb', line 60
def print_new_line(output)
output.print "\n"
end
|
#print_string(output) ⇒ Object
54
55
56
57
58
|
# File 'lib/basic101/basic_numeric.rb', line 54
def print_string(output)
s = format + ' '
s = ' ' + s unless s =~ /^-/
output.print s
end
|
#sgn ⇒ Object
76
77
78
|
# File 'lib/basic101/basic_numeric.rb', line 76
def sgn
BasicInteger.new(@value <=> 0)
end
|
#simplest ⇒ Object
42
43
44
45
46
47
48
|
# File 'lib/basic101/basic_numeric.rb', line 42
def simplest
if @value.modulo(1) == 0
to_integer
else
self
end
end
|
#sin ⇒ Object
84
85
86
|
# File 'lib/basic101/basic_numeric.rb', line 84
def sin
BasicFloat.new(Math.sin(@value))
end
|
#sqr ⇒ Object
68
69
70
|
# File 'lib/basic101/basic_numeric.rb', line 68
def sqr
BasicFloat.new(@value ** 0.5).simplest
end
|
#tan ⇒ Object
92
93
94
|
# File 'lib/basic101/basic_numeric.rb', line 92
def tan
BasicFloat.new(Math.tan(@value))
end
|
#to_b ⇒ Object
38
39
40
|
# File 'lib/basic101/basic_numeric.rb', line 38
def to_b
to_i != 0
end
|
#to_f ⇒ Object
34
35
36
|
# File 'lib/basic101/basic_numeric.rb', line 34
def to_f
@value.to_f
end
|
#to_i ⇒ Object
30
31
32
|
# File 'lib/basic101/basic_numeric.rb', line 30
def to_i
@value.to_i
end
|
#to_numeric ⇒ Object
26
27
28
|
# File 'lib/basic101/basic_numeric.rb', line 26
def to_numeric
self
end
|