Sha256: 5c040a04b3e78e53ec389f0d9cbd0fa6cb3bcae7d8e8d01aba42ba07343cdb44

Contents?: true

Size: 1.19 KB

Versions: 8

Compression:

Stored size: 1.19 KB

Contents

require 'bigdecimal'

module Polyfill
  module V2_5
    module Integer
      using Polyfill(Integer: %w[#ceil #floor #round #truncate], version: '2.4')

      module ClassMethods
        def sqrt(n)
          n = InternalUtils.to_int(n)
          if n < 0
            raise Math::DomainError, 'Numerical argument is out of domain - "isqrt"'
          end

          res = 0
          bit = 1 << ((n.size * 8) - 2)

          bit >>= 2 while bit > n

          while bit != 0
            res_bit = res + bit
            if n >= res_bit
              n -= res_bit
              res = (res >> 1) + bit
            else
              res >>= 1
            end

            bit >>= 2
          end

          res
        end
      end

      def ceil(*)
        super.to_i
      end

      def floor(*)
        super.to_i
      end

      def round(*)
        super.to_i
      end

      def truncate(*)
        super.to_i
      end

      def allbits?(mask)
        imask = InternalUtils.to_int(mask)
        self & imask == imask
      end

      def anybits?(mask)
        self & InternalUtils.to_int(mask) != 0
      end

      def nobits?(mask)
        self & InternalUtils.to_int(mask) == 0
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
polyfill-1.9.0 lib/polyfill/v2_5/integer.rb
polyfill-1.8.0 lib/polyfill/v2_5/integer.rb
polyfill-1.7.0 lib/polyfill/v2_5/integer.rb
polyfill-1.6.0 lib/polyfill/v2_5/integer.rb
polyfill-1.5.0 lib/polyfill/v2_5/integer.rb
polyfill-1.4.0 lib/polyfill/v2_5/integer.rb
polyfill-1.3.0 lib/polyfill/v2_5/integer.rb
polyfill-1.2.0 lib/polyfill/v2_5/integer.rb