Class: Float

Inherits:
Object show all
Defined in:
lib/cbor-nan.rb,
lib/cbor-diagnostic.rb

Instance Method Summary collapse

Instance Method Details

#cbor_diagnostic(options = {}) ⇒ Object

do a little bit of JSON.stringify gaming (ECMA-262, 9.8.1)



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/cbor-diagnostic.rb', line 21

def cbor_diagnostic(options = {})           # do a little bit of JSON.stringify gaming (ECMA-262, 9.8.1)
  if options[:nan] && nan?
    pl = cbor_nan_toggle
    return "nan'#{('%a' % pl).gsub("+", "")}'"
  end
  a = abs
  if a < 1 && a >= 1e-6
    inspect.sub(/(\d)[.](\d+)e-(\d+)/) {"0.#{"0" * ($3.to_i - 1)}#{$1}#{$2}"}
  else
    inspect.sub(/(e[+-])0+/) {$1}
  end
end

#cbor_nan_toggleObject

precondition: 1.0 < |self| < 2.0 or nan?



2
3
4
5
6
# File 'lib/cbor-nan.rb', line 2

def cbor_nan_toggle               # precondition: 1.0 < |self| < 2.0 or nan?
  a = [self].pack("G")
  a.setbyte(0, a.getbyte(0) ^ 0x40)
  a.unpack("G").first
end