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.
15
16
17
|
# File 'lib/basic101/basic_numeric.rb', line 15
def initialize(value)
@value = value
end
|
Instance Attribute Details
#value ⇒ Object
Returns the value of attribute value.
13
14
15
|
# File 'lib/basic101/basic_numeric.rb', line 13
def value
@value
end
|
Instance Method Details
#<=>(other) ⇒ Object
19
20
21
22
|
# File 'lib/basic101/basic_numeric.rb', line 19
def <=>(other)
return nil unless other.is_a?(BasicNumeric)
value <=> other.value
end
|
#abs ⇒ Object
62
63
64
|
# File 'lib/basic101/basic_numeric.rb', line 62
def abs
self.class.new(@value.abs)
end
|
#cos ⇒ Object
86
87
88
|
# File 'lib/basic101/basic_numeric.rb', line 86
def cos
BasicFloat.new(Math.cos(@value))
end
|
#exp ⇒ Object
70
71
72
|
# File 'lib/basic101/basic_numeric.rb', line 70
def exp
BasicFloat.new(Math::E ** @value).simplest
end
|
#log ⇒ Object
78
79
80
|
# File 'lib/basic101/basic_numeric.rb', line 78
def log
BasicFloat.new(Math.log(@value))
end
|
#negate ⇒ Object
48
49
50
|
# File 'lib/basic101/basic_numeric.rb', line 48
def negate
self.class.new(-value)
end
|
#print_new_line(output) ⇒ Object
58
59
60
|
# File 'lib/basic101/basic_numeric.rb', line 58
def print_new_line(output)
output.print "\n"
end
|
#print_string(output) ⇒ Object
52
53
54
55
56
|
# File 'lib/basic101/basic_numeric.rb', line 52
def print_string(output)
s = format + ' '
s = ' ' + s unless s =~ /^-/
output.print s
end
|
#sgn ⇒ Object
74
75
76
|
# File 'lib/basic101/basic_numeric.rb', line 74
def sgn
BasicInteger.new(@value <=> 0)
end
|
#simplest ⇒ Object
40
41
42
43
44
45
46
|
# File 'lib/basic101/basic_numeric.rb', line 40
def simplest
if @value.modulo(1) == 0
to_integer
else
self
end
end
|
#sin ⇒ Object
82
83
84
|
# File 'lib/basic101/basic_numeric.rb', line 82
def sin
BasicFloat.new(Math.sin(@value))
end
|
#sqr ⇒ Object
66
67
68
|
# File 'lib/basic101/basic_numeric.rb', line 66
def sqr
BasicFloat.new(@value ** 0.5).simplest
end
|
#tan ⇒ Object
90
91
92
|
# File 'lib/basic101/basic_numeric.rb', line 90
def tan
BasicFloat.new(Math.tan(@value))
end
|
#to_b ⇒ Object
36
37
38
|
# File 'lib/basic101/basic_numeric.rb', line 36
def to_b
to_i != 0
end
|
#to_f ⇒ Object
32
33
34
|
# File 'lib/basic101/basic_numeric.rb', line 32
def to_f
@value.to_f
end
|
#to_i ⇒ Object
28
29
30
|
# File 'lib/basic101/basic_numeric.rb', line 28
def to_i
@value.to_i
end
|
#to_numeric ⇒ Object
24
25
26
|
# File 'lib/basic101/basic_numeric.rb', line 24
def to_numeric
self
end
|