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

Instance Method Summary collapse

Class Attribute Details

.pi_cacheObject

Returns the value of attribute pi_cache.


129
130
131
# File 'lib/flt/trigonometry.rb', line 129

def pi_cache
  @pi_cache
end

.pi_digitsObject

Returns the value of attribute pi_digits.


129
130
131
# File 'lib/flt/trigonometry.rb', line 129

def pi_digits
  @pi_digits
end

.pi_valueObject

Returns the value of attribute pi_value.


129
130
131
# File 'lib/flt/trigonometry.rb', line 129

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.


48
49
50
# File 'lib/flt/trigonometry.rb', line 48

def acos(x)
  acos_base(num_class[x])
end

#acosh(x) ⇒ Object

Hyperbolic arccosine


114
115
116
# File 'lib/flt/trigonometry.rb', line 114

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.


42
43
44
# File 'lib/flt/trigonometry.rb', line 42

def asin(x)
  asin_base(num_class[x])
end

#asinh(x) ⇒ Object

Hyperbolic arcsine


109
110
111
# File 'lib/flt/trigonometry.rb', line 109

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.


29
30
31
# File 'lib/flt/trigonometry.rb', line 29

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.


36
37
38
# File 'lib/flt/trigonometry.rb', line 36

def atan2(y, x)
  atan2_base(num_class[y], num_class[x])
end

#atanh(x) ⇒ Object

Hyperbolic arctangent


119
120
121
# File 'lib/flt/trigonometry.rb', line 119

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.


13
14
15
# File 'lib/flt/trigonometry.rb', line 13

def cos(x)
  cos_base(num_class[x])
end

#cosh(x) ⇒ Object

Hyperbolic cosine


99
100
101
# File 'lib/flt/trigonometry.rb', line 99

def cosh(x)
  cosh_base(num_class[x])
end

#e(digits = nil) ⇒ Object


82
83
84
85
86
87
# File 'lib/flt/trigonometry.rb', line 82

def e(digits=nil)
  num_class.context(self) do |local_context|
    local_context.precision = digits if digits
    num_class.Num(1).exp
  end
end

#halfObject


89
90
91
# File 'lib/flt/trigonometry.rb', line 89

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


53
54
55
# File 'lib/flt/trigonometry.rb', line 53

def hypot(x, y)
  hypot_base(num_class[x], num_class[y])
end

#pi(round_digits = nil) ⇒ Object

Pi


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/flt/trigonometry.rb', line 58

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.


18
19
20
# File 'lib/flt/trigonometry.rb', line 18

def sin(x)
  sin_base(num_class[x])
end

#sinh(x) ⇒ Object

Hyperbolic sine


94
95
96
# File 'lib/flt/trigonometry.rb', line 94

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.


23
24
25
# File 'lib/flt/trigonometry.rb', line 23

def tan(x)
  tan_base(num_class[x])
end

#tanh(x) ⇒ Object

Hyperbolic tangent


104
105
106
# File 'lib/flt/trigonometry.rb', line 104

def tanh(x)
  tanh_base(num_class[x])
end