-
Notifications
You must be signed in to change notification settings - Fork 78
Add support for tangent function #231
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
# sqrt(x, prec) | ||
# sin (x, prec) | ||
# cos (x, prec) | ||
# tan (x, prec) | ||
# atan(x, prec) | ||
# PI (prec) | ||
# E (prec) == exp(1.0,prec) | ||
|
@@ -131,6 +132,26 @@ def cos(x, prec) | |
y | ||
end | ||
|
||
# call-seq: | ||
# tan(decimal, numeric) -> BigDecimal | ||
# | ||
# Computes the tangent of +decimal+ to the specified number of digits of | ||
# precision, +numeric+. | ||
# | ||
# If +decimal+ is Infinity or NaN, returns NaN. | ||
# | ||
# BigMath.tan(BigDecimal("0.0"), 4).to_s | ||
# #=> "0.0" | ||
# | ||
# BigMath.tan(BigMath.PI(4) / 4, 4).to_s | ||
# #=> "0.999999999999999999999955588155008544487055622030633191403625599381672572e0" | ||
# | ||
def tan(x, prec) | ||
sine = sin(x, prec) | ||
cosine = sqrt(1 - sine**2, prec) | ||
sine / cosine | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @rhannequin Could you please mention the origin such as the paper reference of this algorithm in the comment? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mrkn I don't believe tanθ is defined as sinθ / (1 - sin2θ), but should be sinθ / cosθ and cosθ is √(1 - sin2θ). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @nobu The current code was updated against my comment above. Please compare the commit timestamp and the comment timestamp. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
# call-seq: | ||
# atan(decimal, numeric) -> BigDecimal | ||
# | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,6 +53,17 @@ def test_cos | |
assert_in_delta(0.0, cos(PI(N) * BigDecimal("301.5"), N)) | ||
end | ||
|
||
def test_tan | ||
assert_in_delta(0.0, tan(BigDecimal("0.0"), N)) | ||
assert_in_delta(0.0, tan(PI(N), N)) | ||
assert_in_delta(1.0, tan(PI(N) / 4, N)) | ||
assert_in_delta(sqrt(BigDecimal(3), N), tan(PI(N) / 3, N)) | ||
assert_in_delta(sqrt(BigDecimal(3), 10 * N), tan(PI(10 * N) / 3, 10 * N)) | ||
assert_in_delta(0.0, tan(-PI(N), N)) | ||
assert_in_delta(-1.0, tan(-PI(N) / 4, N)) | ||
assert_in_delta(-sqrt(BigDecimal(3), N), tan(-PI(N) / 3, N)) | ||
Comment on lines
+57
to
+64
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need to specify an appropriate BigDecimal for the |
||
end | ||
|
||
def test_atan | ||
assert_equal(0.0, atan(BigDecimal("0.0"), N)) | ||
assert_in_delta(Math::PI/4, atan(BigDecimal("1.0"), N)) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As I wrote in #319, I want to redefine$\sin x = \sqrt{1 - \cos^2 x}$ for $|x| \le \pi$ .
sin
bySo I want to rewrite these lines as follow:
The$x$ within the range $[-\pi, \pi]$ and calculates the result sign from the original $x$ .
adjust_x_and_detect_sign_of_tangent
method adjustsThe
guarantee_precision
method performs Ziv's loop to ensure the result of the block has the desired precision.