Methods

Class Index [+]

Quicksearch

Flt::DecNum::Trigonometry

Constants

PI_MARGIN

Attributes

pi_cache[RW]
pi_cache_digits[RW]

Private Class Methods

iarccot(x, unity) click to toggle source
     # File lib/flt/trigonometry.rb, line 658
658:         def iarccot(x, unity)
659:           xpow = unity / x
660:           n = 1
661:           sign = 1
662:           sum = 0
663:           loop do
664:               term = xpow / n
665:               break if term == 0
666:               sum += sign * (xpow/n)
667:               xpow /= x*x
668:               n += 2
669:               sign = -sign
670:           end
671:           sum
672:         end

Public Instance Methods

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

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.