Module: Flt::Trigonometry
- Defined in:
- lib/flt/trigonometry.rb
Overview
Trigonometry functions. The angular units used by these functions can be specified with the angle
attribute of the context. The accepted values are:
-
:rad for radians
-
:deg for degrees
-
:grad for gradians
These functions are injected in Context objects.
Defined Under Namespace
Modules: Support
Class Attribute Summary collapse
-
.pi_cache ⇒ Object
Returns the value of attribute pi_cache.
-
.pi_digits ⇒ Object
Returns the value of attribute pi_digits.
-
.pi_value ⇒ Object
Returns the value of attribute pi_value.
Instance Method Summary collapse
-
#acos(x) ⇒ Object
Arc-cosine.
-
#acosh(x) ⇒ Object
Hyperbolic arccosine.
-
#asin(x) ⇒ Object
Arc-sine.
-
#asinh(x) ⇒ Object
Hyperbolic arcsine.
-
#atan(x) ⇒ Object
Arc-tangent.
-
#atan2(y, x) ⇒ Object
Arc-tangent with two arguments (principal value of the argument of the complex number x+i*y).
-
#atanh(x) ⇒ Object
Hyperbolic arctangent.
-
#cos(x) ⇒ Object
Cosine of an angle given in the units specified by the context
angle
attribute. -
#cosh(x) ⇒ Object
Hyperbolic cosine.
- #e(digits = nil) ⇒ Object
- #half ⇒ Object
-
#hypot(x, y) ⇒ Object
Length of the hypotenuse of a right-angle triangle (modulus or absolute value of the complex x+i*y).
-
#pi(round_digits = nil) ⇒ Object
Pi.
-
#sin(x) ⇒ Object
Sine of an angle given in the units specified by the context
angle
attribute. -
#sinh(x) ⇒ Object
Hyperbolic sine.
-
#tan(x) ⇒ Object
Tangent of an angle given in the units specified by the context
angle
attribute. -
#tanh(x) ⇒ Object
Hyperbolic tangent.
Class Attribute Details
.pi_cache ⇒ Object
Returns the value of attribute pi_cache.
131 132 133 |
# File 'lib/flt/trigonometry.rb', line 131 def pi_cache @pi_cache end |
.pi_digits ⇒ Object
Returns the value of attribute pi_digits.
131 132 133 |
# File 'lib/flt/trigonometry.rb', line 131 def pi_digits @pi_digits end |
.pi_value ⇒ Object
Returns the value of attribute pi_value.
131 132 133 |
# File 'lib/flt/trigonometry.rb', line 131 def pi_value @pi_value end |
Instance Method Details
#acos(x) ⇒ Object
Arc-cosine. The result is in the units specified by the context angle
attribute. If the angular units are radians the result is in [-pi/2, pi/2]; it is in [-90,90] in degrees.
50 51 52 |
# File 'lib/flt/trigonometry.rb', line 50 def acos(x) acos_base(num_class[x]) end |
#acosh(x) ⇒ Object
Hyperbolic arccosine
116 117 118 |
# File 'lib/flt/trigonometry.rb', line 116 def acosh(x) acosh_base(num_class[x]) end |
#asin(x) ⇒ Object
Arc-sine. The result is in the units specified by the context angle
attribute. If the angular units are radians the result is in [-pi/2, pi/2]; it is in [-90,90] in degrees.
44 45 46 |
# File 'lib/flt/trigonometry.rb', line 44 def asin(x) asin_base(num_class[x]) end |
#asinh(x) ⇒ Object
Hyperbolic arcsine
111 112 113 |
# File 'lib/flt/trigonometry.rb', line 111 def asinh(x) asinh_base(num_class[x]) end |
#atan(x) ⇒ Object
Arc-tangent. The result is in the units specified by the context angle
attribute. If the angular units are radians the result is in [-pi/2, pi/2]; it is in [-90,90] in degrees.
31 32 33 |
# File 'lib/flt/trigonometry.rb', line 31 def atan(x) atan_base(num_class[x]) end |
#atan2(y, x) ⇒ Object
Arc-tangent with two arguments (principal value of the argument of the complex number x+i*y). The result is in the units specified by the context angle
attribute. If the angular units are radians the result is in [-pi, pi]; it is in [-180,180] in degrees.
38 39 40 |
# File 'lib/flt/trigonometry.rb', line 38 def atan2(y, x) atan2_base(num_class[y], num_class[x]) end |
#atanh(x) ⇒ Object
Hyperbolic arctangent
121 122 123 |
# File 'lib/flt/trigonometry.rb', line 121 def atanh(x) atanh_base(num_class[x]) end |
#cos(x) ⇒ Object
Cosine of an angle given in the units specified by the context angle
attribute.
15 16 17 |
# File 'lib/flt/trigonometry.rb', line 15 def cos(x) cos_base(num_class[x]) end |
#cosh(x) ⇒ Object
Hyperbolic cosine
101 102 103 |
# File 'lib/flt/trigonometry.rb', line 101 def cosh(x) cosh_base(num_class[x]) end |
#e(digits = nil) ⇒ Object
84 85 86 87 88 89 |
# File 'lib/flt/trigonometry.rb', line 84 def e(digits=nil) num_class.context(self) do |local_context| local_context.precision = digits if digits num_class.Num(1).exp end end |
#half ⇒ Object
91 92 93 |
# File 'lib/flt/trigonometry.rb', line 91 def half @half ||= num_class.one_half end |
#hypot(x, y) ⇒ Object
Length of the hypotenuse of a right-angle triangle (modulus or absolute value of the complex x+i*y).
55 56 57 |
# File 'lib/flt/trigonometry.rb', line 55 def hypot(x, y) hypot_base(num_class[x], num_class[y]) end |
#pi(round_digits = nil) ⇒ Object
Pi
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/flt/trigonometry.rb', line 60 def pi(round_digits=nil) round_digits ||= self.precision if Trigonometry.pi_digits < round_digits # provisional implementation (very slow) lasts = 0 t, s, n, na, d, da = Trigonometry.pi_cache num_class.context(self) do |local_context| local_context.precision = round_digits + 6 tol = Rational(1,num_class.int_radix_power(local_context.precision+1)) while (s-lasts)>tol lasts = s n, na = n+na, na+8 d, da = d+da, da+32 t = (t * n) / d s += t end Trigonometry.pi_value = num_class[s] Trigonometry.pi_digits = round_digits Trigonometry.pi_cache = [t, s, n, na, d, da] end end num_class.context(self, :precision=>round_digits){+Trigonometry.pi_value} end |
#sin(x) ⇒ Object
Sine of an angle given in the units specified by the context angle
attribute.
20 21 22 |
# File 'lib/flt/trigonometry.rb', line 20 def sin(x) sin_base(num_class[x]) end |
#sinh(x) ⇒ Object
Hyperbolic sine
96 97 98 |
# File 'lib/flt/trigonometry.rb', line 96 def sinh(x) sinh_base(num_class[x]) end |
#tan(x) ⇒ Object
Tangent of an angle given in the units specified by the context angle
attribute.
25 26 27 |
# File 'lib/flt/trigonometry.rb', line 25 def tan(x) tan_base(num_class[x]) end |
#tanh(x) ⇒ Object
Hyperbolic tangent
106 107 108 |
# File 'lib/flt/trigonometry.rb', line 106 def tanh(x) tanh_base(num_class[x]) end |