class IcAgent::Candid::FloatClass

Represents an IDL Float

Public Class Methods

new(bits) click to toggle source
Calls superclass method IcAgent::Candid::PrimitiveType::new
# File lib/ic_agent/candid.rb, line 418
def initialize(bits)
  super()
  @bits = bits
  raise ArgumentError, 'not a valid float type' unless [32, 64].include?(@bits)
end

Public Instance Methods

covariant(x) click to toggle source
# File lib/ic_agent/candid.rb, line 424
def covariant(x)
  x.is_a?(Float)
end
decode_value(b, t) click to toggle source
# File lib/ic_agent/candid.rb, line 447
def decode_value(b, t)
  check_type(t)
  by = IcAgent::Candid.safe_read(b, @bits / 8)
  if @bits == 32
    by.hex2str.unpack('f')[0]
  elsif @bits == 64
    by.hex2str.unpack('d')[0]
  else
    raise ValueError, 'The length of float have to be 32 bits or 64 bits '
  end
end
encode_type(type_table = nil) click to toggle source
# File lib/ic_agent/candid.rb, line 438
def encode_type(type_table = nil)
  opcode = if @bits == 32
             TypeIds::Float32
           else
             TypeIds::Float64
           end
  LEB128.encode_signed(opcode).string
end
encode_value(val) click to toggle source
# File lib/ic_agent/candid.rb, line 428
def encode_value(val)
  if @bits == 32
    [val].pack('f')
  elsif @bits == 64
    [val].pack('d')
  else
    raise ValueError, 'The length of float have to be 32 bits or 64 bits '
  end
end
id() click to toggle source
# File lib/ic_agent/candid.rb, line 463
def id
  if @bits == 32
    TypeIds::Float32
  else
    TypeIds::Float64
  end
end
name() click to toggle source
# File lib/ic_agent/candid.rb, line 459
def name
  "float#{@bits}"
end