Module: Flt::DecNum::Trigonometry
- Includes:
- Trigonometry::Support
- Defined in:
- lib/flt/trigonometry.rb
Constant Summary collapse
- PI_MARGIN =
10
Class Attribute Summary collapse
-
.pi_cache ⇒ Object
Returns the value of attribute pi_cache.
-
.pi_cache_digits ⇒ Object
Returns the value of attribute pi_cache_digits.
Instance Method Summary collapse
Methods included from Trigonometry::Support
Class Attribute Details
.pi_cache ⇒ Object
Returns the value of attribute pi_cache.
616 617 618 |
# File 'lib/flt/trigonometry.rb', line 616 def pi_cache @pi_cache end |
.pi_cache_digits ⇒ Object
Returns the value of attribute pi_cache_digits.
616 617 618 |
# File 'lib/flt/trigonometry.rb', line 616 def pi_cache_digits @pi_cache_digits end |
Instance Method Details
#pi(round_digits = nil) ⇒ Object
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 |
# File 'lib/flt/trigonometry.rb', line 619 def pi(round_digits=nil) round_digits ||= self.precision digits = round_digits if Trigonometry.pi_cache_digits <= digits # we need at least one more truncated digit continue = true while continue margin = PI_MARGIN # margin to reduce recomputing with more digits to avoid ending in 0 or 5 digits += margin + 1 fudge = 10 unity = 10**(digits+fudge) v = 4*(4*iarccot(5, unity) - iarccot(239, unity)) v = v.to_s[0,digits] # if the last digit is 0 or 5 the truncated value may not be good for rounding loop do #last_digit = v%10 last_digit = v[-1,1].to_i continue = (last_digit==5 || last_digit==0) if continue && margin>0 # if we have margin we back-up one digit margin -= 1 v = v[0...-1] else break end end end Trigonometry.pi_cache_digits = digits + margin - PI_MARGIN # @pi_cache.size Trigonometry.pi_cache = v # DecNum(+1, v, 1-digits) # cache truncated value end # Now we avoid rounding too much because it is slow l = round_digits + 1 while (l<Num[16]::Trigonometry.pi_cache_digits) && [0,5].include?(Trigonometry.pi_cache[l-1,1].to_i) l += 1 end v = Trigonometry.pi_cache[0,l] num_class.context(self, :precision=>round_digits){+num_class.Num(+1,v.to_i,1-l)} end |