Skip to content
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

Define the TAU circle constant [Feature #4897] #644

Closed
wants to merge 1 commit into from
Closed
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
15 changes: 15 additions & 0 deletions ext/bigdecimal/lib/bigdecimal/math.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
# cos (x, prec)
# atan(x, prec) Note: |x|<1, x=0.9999 may not converge.
# PI (prec)
# TAU (prec) == PI(prec)*2
# E (prec) == exp(1.0,prec)
#
# where:
Expand Down Expand Up @@ -215,6 +216,20 @@ def PI(prec)
pi
end

# call-seq:
# TAU(numeric) -> BigDecimal
#
# Computes the value of tau (2 * pi) to the specified number of digits of precision,
# +numeric+.
#
# BigMath.TAU(10).to_s
# #=> "0.6283185307179586476925286777627707573914824E1"
#
def TAU(prec)
raise ArgumentError, "Zero or negative precision for TAU" if prec <= 0
PI(prec) * BigDecimal("2")
end

# call-seq:
# E(numeric) -> BigDecimal
#
Expand Down
2 changes: 2 additions & 0 deletions math.c
Original file line number Diff line number Diff line change
Expand Up @@ -977,8 +977,10 @@ InitVM_Math(void)
#ifdef M_PI
/* Definition of the mathematical constant PI as a Float number. */
rb_define_const(rb_mMath, "PI", DBL2NUM(M_PI));
rb_define_const(rb_mMath, "TAU", DBL2NUM(M_PI*2.0));
#else
rb_define_const(rb_mMath, "PI", DBL2NUM(atan(1.0)*4.0));
rb_define_const(rb_mMath, "TAU", DBL2NUM(atan(1.0)*8.0));
#endif

#ifdef M_E
Expand Down