Class: CBOR::Fbox
- Inherits:
-
Box
- Object
- Struct
- Box
- CBOR::Fbox
show all
- Defined in:
- lib/cbor-diag-support.rb
Constant Summary
collapse
- FLOAT_EI =
{
"1" => 2,
"2" => 4,
"3" => 8
}
Constants inherited
from Box
Box::INTEGER_EI
Instance Attribute Summary
Attributes inherited from Box
#options, #value
Instance Method Summary
collapse
Methods inherited from Box
#cbor_diagnostic, from_instance, #inspect, make_head, #to_s
Instance Method Details
#make_float(plusbytes, fv) ⇒ Object
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
|
# File 'lib/cbor-diag-support.rb', line 131
def make_float(plusbytes, fv)
ret =
if fv.nan?
ds = [fv].pack("G")
firstword = ds.unpack("n").first
raise "NaN exponent error #{firstword}" unless firstword & 0x7FF0 == 0x7FF0
iv = ds.unpack("Q>").first
ret = case plusbytes
when 2
if iv & 0x3ffffffffff == 0 [0xf9, (firstword & 0xFC00) + ((iv >> 42) & 0x3ff)].pack("Cn")
end
when 4
if iv & 0x1fffffff == 0 [0xfa, (ds.getbyte(0) << 24) + ((iv >> 29) & 0xffffff)].pack("CN")
end
when 8
"\xfb".b << ds
end
else
if plusbytes == 8
[0xfb, fv].pack("CG") else
ss = [fv].pack("g") if ss.unpack("g").first == fv
if plusbytes == 4
"\xfa".b << ss
else
if hs = Half.encode_from_single(fv, ss)
"\xf9".b << hs
end
end
end
end
end
raise ArgumentError, "cbor-diagnostic: make_float #{plusbytes.inspect} #{fv.inspect}" unless ret
ret
end
|
#to_cbor ⇒ Object
174
175
176
177
178
179
180
181
182
|
# File 'lib/cbor-diag-support.rb', line 174
def to_cbor
if ei = options[:ei]
plusbytes = FLOAT_EI[ei]
raise ArgumentError, "cbor-diagnostic: unknown encoding indicator _#{ei} for Float':\n" unless plusbytes
make_float(plusbytes, value)
else
CBOR.encode(value)
end
end
|