Skip to content

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions lib/bigdecimal/math.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Comment on lines +150 to +152
Copy link
Member

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 by $\sin x = \sqrt{1 - \cos^2 x}$ for $|x| \le \pi$.
So I want to rewrite these lines as follow:

    x, sign = adjust_x_and_detect_sgin_of_tangent(x)
    t = guarantee_precision(prec) do |n|
      c = cos(x, n)
      s = sqrt(1 - c**2, n)
      s.div(c, n)
    end
    sign >= 0 ? t : -t

The adjust_x_and_detect_sign_of_tangent method adjusts $x$ within the range $[-\pi, \pi]$ and calculates the result sign from the original $x$.
The guarantee_precision method performs Ziv's loop to ensure the result of the block has the desired precision.

end
Copy link
Member

@mrkn mrkn Oct 31, 2022

Choose a reason for hiding this comment

The 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?
And, I want to know how this algorithm is better than just doing sine/(1 - sine**2). Could you explain?

Copy link
Member

Choose a reason for hiding this comment

The 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θ).

Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sqrt(1 - sine**2, prec) is not cosine. It is cosine.abs
Test case of x around -PI/4, 3*PI/4, -3*PI/4 are missing.


# call-seq:
# atan(decimal, numeric) -> BigDecimal
#
Expand Down
11 changes: 11 additions & 0 deletions test/bigdecimal/test_bigmath.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to specify an appropriate BigDecimal for the delta parameter in each assertion.
I'll rewrite them later.

end

def test_atan
assert_equal(0.0, atan(BigDecimal("0.0"), N))
assert_in_delta(Math::PI/4, atan(BigDecimal("1.0"), N))
Expand Down
Loading