File: float_integer_conversion.rb

package info (click to toggle)
ruby-backports 3.25.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,912 kB
  • sloc: ruby: 11,757; makefile: 6
file content (17 lines) | stat: -rw-r--r-- 344 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
module Backports
  class << self
    def float_to_integer(float)
      map_via_packing(float, 'D', 'q')
    end

    def integer_to_float(int)
      map_via_packing(int, 'q', 'D')
    end

    private
    def map_via_packing(nb, pack, unpack)
      result, = [nb.abs].pack(pack).unpack(unpack)
      nb < 0 ? -result : result
    end
  end
end