Class CBORFloat

java.lang.Object
org.webpki.cbor.CBORObject
org.webpki.cbor.CBORFloat
All Implemented Interfaces:
Cloneable, Comparable<CBORObject>

public class CBORFloat extends CBORObject
Class for holding CBOR float objects.

Floating-point numbers are expressed in the IEEE 754 format using the length 16, 32, and 64 bit on the "wire". Which length to use is governed by the size and precision required to (minimally) correctly represent a value.

See Also:
  • Constructor Details

    • CBORFloat

      public CBORFloat(double value)
      Creates a CBOR float object.

      This constructor only implements support for finite ("regular") floating point numbers. That is, a Double.NaN argument causes a CBORException to be thrown.

      CBORObject.getFloat64() is the decoder counterpart.

      For NaN and Infinity support see createExtendedFloat(double).

      Parameters:
      value - Floating-point value
  • Method Details

    • createExtendedFloat

      public static CBORObject createExtendedFloat(double value)
      Creates an "extended" CBOR float object.

      Unlike CBORFloat(double), this method also supports the Double.NaN, Double.POSITIVE_INFINITY, and Double.NEGATIVE_INFINITY non-finite variants.

      CBORObject.getExtendedFloat64() is the decoder counterpart.

      Note that return type is either CBORFloat or CBORNonFinite, depending on if the argument is a "regular" floating-point value or one of the non-finite variants.

      Parameters:
      value - Floating-point value
      Returns:
      CBORObject
      Throws:
      CBORException
      See Also:
    • createFloat32

      public static CBORFloat createFloat32(double value)
      Creates a CBOR float32 object.

      This method creates a CBORFloat object, where the value is converted to fit CBOR float32 representation.

      If the value (after applying IEEE 754 conversion rules), is out of range, or is non-finite, a CBORException is thrown.

      Note that this method returns a float16 compatible object if the value and precision is equivalent to the float32 representation (e.g. 2.5).

      Parameters:
      value - Floating-point value
      Returns:
      CBORFloat
      Throws:
      CBORException
      See Also:
    • createFloat16

      public static CBORFloat createFloat16(double value)
      Creates a CBOR float16 object.

      This method creates a CBORFloat object, where the value is converted to fit CBOR float16 representation.

      If the value (after applying IEEE 754 conversion rules), is out of range, or is non-finite, a CBORException is thrown.

      Parameters:
      value - Floating-point value
      Returns:
      CBORFloat
      Throws:
      CBORException
      See Also:
    • length

      public int length()
      Get length of the serialized IEEE 754 object.

      Note that you must cast a CBORObject to CBORFloat in order to access length().

      Returns:
      Length in bytes: 2, 4, or 8.