Sha256: 6f6a9ecbd716c336316452e16d8b4967dd88fbc04f2f45265f1364195e76333b

Contents?: true

Size: 1.34 KB

Versions: 10

Compression:

Stored size: 1.34 KB

Contents

=begin
  Copyright 2006 Suraj N. Kurapati

  This file is part of Ruby-VPI.

  Ruby-VPI is free software; you can redistribute it and/or
  modify it under the terms of the GNU General Public License
  as published by the Free Software Foundation; either version 2
  of the License, or (at your option) any later version.

  Ruby-VPI is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with Ruby-VPI; if not, write to the Free Software Foundation,
  Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
=end

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

10 entries across 10 versions & 1 rubygems

Version Path
ruby-vpi-12.0.0 lib/ruby-vpi/float.rb
ruby-vpi-11.1.1 lib/ruby-vpi/float.rb
ruby-vpi-12.0.1 lib/ruby-vpi/float.rb
ruby-vpi-12.0.2 lib/ruby-vpi/float.rb
ruby-vpi-14.0.0 lib/ruby-vpi/float.rb
ruby-vpi-12.1.0 lib/ruby-vpi/float.rb
ruby-vpi-13.0.0 lib/ruby-vpi/float.rb
ruby-vpi-15.0.2 lib/ruby-vpi/float.rb
ruby-vpi-15.0.0 lib/ruby-vpi/float.rb
ruby-vpi-15.0.1 lib/ruby-vpi/float.rb