Sha256: 5fd7f1b60f0909f174a7e5d4cffefc794571b6306f810132aee0f5a26e40ae9a

Contents?: true

Size: 742 Bytes

Versions: 6

Compression:

Stored size: 742 Bytes

Contents

# Library for hardware-related floating point operations.
#--
# Copyright 2006 Suraj N. Kurapati
# See the file named LICENSE for details.

class String
  # Converts this string into a floating point number
  # using the given radix. The default radix is 10.
  def to_f aRadix = 10
    whole, frac = split('.', 2)
    whole = whole.to_i(aRadix).to_f

    if frac
      f = 0.0

      frac.length.times do |i|
        power = i.next
        weight = aRadix ** -power
        digit = frac[i, 1].to_i(aRadix)

        f += digit * weight
      end

      f = -f if self =~ /^-/
      whole += f
    end

    whole
  end
end

class Float
  # Returns the mantissa of this floating point number
  def mantissa
    f = abs
    f - f.floor
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
ruby-vpi-18.0.2 lib/ruby-vpi/float.rb
ruby-vpi-19.0.0 lib/ruby-vpi/float.rb
ruby-vpi-20.0.0 lib/ruby-vpi/float.rb
ruby-vpi-18.0.1 lib/ruby-vpi/float.rb
ruby-vpi-21.1.0 lib/ruby-vpi/float.rb
ruby-vpi-21.0.0 lib/ruby-vpi/float.rb