// Implements major type 7 encoding per https://tools.ietf.org/html/rfc7049#section-2.1 /// <summary>Writes a single-precision floating point number (major type 7).</summary> /// <param name="value">The value to write.</param> /// <exception cref="InvalidOperationException"><para>Writing a new value exceeds the definite length of the parent data item.</para> /// <para>-or-</para> /// <para>The major type of the encoded value is not permitted in the parent data item.</para> /// <para>-or-</para> /// <para>The written data is not accepted under the current conformance mode.</para></exception> public void WriteSingle(float value) { if (!CborConformanceModeHelpers.RequiresPreservingFloatPrecision(ConformanceMode) && TryConvertSingleToHalf(value, out var half)) { WriteHalf(half); } else { WriteSingleCore(value); } }
/// <summary> /// Writes a double-precision floating point number (major type 7). /// </summary> /// <param name="value">The value to write.</param> /// <exception cref="InvalidOperationException"> /// Writing a new value exceeds the definite length of the parent data item. -or- /// The major type of the encoded value is not permitted in the parent data item. -or- /// The written data is not accepted under the current conformance mode /// </exception> public void WriteDouble(double value) { if (!CborConformanceModeHelpers.RequiresPreservingFloatPrecision(ConformanceMode) && FloatSerializationHelpers.TryConvertDoubleToSingle(value, out float single)) { if (FloatSerializationHelpers.TryConvertSingleToHalf(single, out Half half)) { WriteHalf(half); } else { WriteSingleCore(single); } } else { WriteDoubleCore(value); } }