Skip to content

Commit fc016a6

Browse files
rhannequinmrkn
authored andcommitted
Add support for tangent function
1 parent 996c51b commit fc016a6

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

lib/bigdecimal/math.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
# sqrt(x, prec)
88
# sin (x, prec)
99
# cos (x, prec)
10+
# tan (x, prec)
1011
# atan(x, prec) Note: |x|<1, x=0.9999 may not converge.
1112
# PI (prec)
1213
# E (prec) == exp(1.0,prec)
@@ -132,6 +133,24 @@ def cos(x, prec)
132133
y
133134
end
134135

136+
# call-seq:
137+
# tan(decimal, numeric) -> BigDecimal
138+
#
139+
# Computes the tangent of +decimal+ to the specified number of digits of
140+
# precision, +numeric+.
141+
#
142+
# If +decimal+ is Infinity or NaN, returns NaN.
143+
#
144+
# BigMath.tan(BigDecimal("0.0"), 4).to_s
145+
# #=> "0.0"
146+
#
147+
# BigMath.tan(BigMath.PI(4) / 4, 4).to_s
148+
# #=> "0.999999999999999999999955588155008544487055622030633191403625599381672572e0"
149+
#
150+
def tan(x, prec)
151+
sin(x, prec) / cos(x, prec)
152+
end
153+
135154
# call-seq:
136155
# atan(decimal, numeric) -> BigDecimal
137156
#

test/bigdecimal/test_bigmath.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,16 @@ def test_cos
5353
assert_in_delta(0.0, cos(PI(N) * BigDecimal("301.5"), N))
5454
end
5555

56+
def test_tan
57+
assert_in_delta(0.0, tan(BigDecimal("0.0"), N))
58+
assert_in_delta(0.0, tan(PI(N), N))
59+
assert_in_delta(1.0, tan(PI(N) / 4, N))
60+
assert_in_delta(Math.sqrt(3.0), tan(PI(N) / 3, N))
61+
assert_in_delta(0.0, tan(-PI(N), N))
62+
assert_in_delta(-1.0, tan(-PI(N) / 4, N))
63+
assert_in_delta(-Math.sqrt(3.0), tan(-PI(N) / 3, N))
64+
end
65+
5666
def test_atan
5767
assert_equal(0.0, atan(BigDecimal("0.0"), N))
5868
assert_in_delta(Math::PI/4, atan(BigDecimal("1.0"), N))

0 commit comments

Comments
 (0)