Class: IcAgent::Candid::FloatClass
Instance Method Summary
collapse
#_build_type_table_impl, #check_type
Methods inherited from BaseType
_build_type_table_impl, #build_type_table, check_type, covariant, decode_value, #display, encode_type, encode_value
Constructor Details
Returns a new instance of FloatClass.
409
410
411
412
413
|
# File 'lib/ic_agent/candid.rb', line 409
def initialize(bits)
super()
@bits = bits
raise ArgumentError, 'not a valid float type' unless [32, 64].include?(@bits)
end
|
Instance Method Details
#covariant(x) ⇒ Object
415
416
417
|
# File 'lib/ic_agent/candid.rb', line 415
def covariant(x)
x.is_a?(Float)
end
|
#decode_value(b, t) ⇒ Object
438
439
440
441
442
443
444
445
446
447
448
|
# File 'lib/ic_agent/candid.rb', line 438
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) ⇒ Object
429
430
431
432
433
434
435
436
|
# File 'lib/ic_agent/candid.rb', line 429
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) ⇒ Object
419
420
421
422
423
424
425
426
427
|
# File 'lib/ic_agent/candid.rb', line 419
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 ⇒ Object
454
455
456
457
458
459
460
|
# File 'lib/ic_agent/candid.rb', line 454
def id
if @bits == 32
TypeIds::Float32
else
TypeIds::Float64
end
end
|
#name ⇒ Object
450
451
452
|
# File 'lib/ic_agent/candid.rb', line 450
def name
"float#{@bits}"
end
|