Module: Flt::IEEE

Defined in:
lib/float-formats/formats.rb

Overview

Helper methods to define IEEE 754r formats

Class Method Summary collapse

Class Method Details

.binary(name, parameters) ⇒ Object

Define an IEEE binary format by passing parameters in a hash; :significand and :exponent are used to defined the fields, optional parameters may follow.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/float-formats/formats.rb', line 19

def self.binary(name, parameters)
  significand_bits = parameters[:significand]
  exponent_bits = parameters[:exponent]
  Flt.define(name, {
    base: BinaryFormat,
    fields: [:significand, significand_bits, :exponent, exponent_bits, :sign, 1],
    bias: 2**(exponent_bits-1)-1, bias_mode: :scientific_significand,
    hidden_bit: true,
    endianness: :little_endian,
    round: :half_even,
    gradual_underflow: true,
    infinity: true, nan: true
    }.merge(parameters)
  )
end

.decimal(name, parameters) ⇒ Object

Define an IEEE decimal format by passing parameters in a hash; :significand and :exponent are used to defined the fields, optional parameters may follow.



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/float-formats/formats.rb', line 47

def self.decimal(name,parameters)
  significand_continuation_bits = parameters[:significand]
  exponent_continuation_bits = parameters[:exponent]
  Flt.define(name, {
    base: DPDFormat,
    fields: [:significand_continuation, significand_continuation_bits, :exponent_continuation, exponent_continuation_bits, :combination, 5, :sign, 1],
    normalized: false,
    endianness: :big_endian,
    gradual_underflow: true,
    infinity: true, nan: true,
    round: :half_even
  }.merge(parameters))
end

.interchange_binary(name, width_in_bits, options = {}) ⇒ Object

Define an IEEE binary interchange format given its width in bits



36
37
38
39
40
41
42
# File 'lib/float-formats/formats.rb', line 36

def self.interchange_binary(name,width_in_bits, options={})
  unless (width_in_bits%32)==0 && (width_in_bits/32)>=4
    raise "Invalid IEEE binary interchange format definition: size (#{width_in_bits}) is not valid"
  end
  p = width_in_bits - (4*Math.log(width_in_bits)/Math.log(2)).round.to_i + 13
  binary(name, { significand: p-1, exponent: width_in_bits-p }.merge(options))
end

.interchange_decimal(name, width_in_bits, options = {}) ⇒ Object

Define an IEEE decimal interchange format given its width in bits



62
63
64
65
66
67
68
# File 'lib/float-formats/formats.rb', line 62

def self.interchange_decimal(name,width_in_bits, options={})
  raise "Invalid IEEE decimal interchange format definition: size (#{width_in_bits}) is not valid" unless (width_in_bits%32)==0
  p = width_in_bits*9/32 - 2
  t = (p-1)*10/3
  w = width_in_bits - t - 6
  decimal(name,{ significand: t, exponent: w }.merge(options))
end