Skip to content

Commit

Permalink
Closes #25
Browse files Browse the repository at this point in the history
  • Loading branch information
andreashuber-lawo committed May 23, 2016
1 parent 1d3310f commit 5772696
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
15 changes: 13 additions & 2 deletions Lawo.EmberPlusSharp/Ember/EmberReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,19 @@ private static double ReadReal(ReadBuffer readBuffer, int length)
}

var exponent = Read8Bit(readBuffer, exponentLength, true);
var mantissa = Read8Bit(readBuffer, mantissaLength, false);

if (exponent == 1024)
{
if (mantissa == 0)
{
return signBits == 0 ? double.PositiveInfinity : double.NegativeInfinity;
}
else if (mantissa == Constants.DoubleMantissaMask)
{
return double.NaN;
}
}

// https://en.wikipedia.org/wiki/Double-precision_floating-point_format
if ((exponent <= -Constants.DoubleExponentBias) || (exponent > Constants.DoubleExponentBias))
Expand All @@ -545,8 +558,6 @@ private static double ReadReal(ReadBuffer readBuffer, int length)
"The exponent of the Real at position {0} exceeds the expected range.", position);
}

var mantissa = Read8Bit(readBuffer, mantissaLength, false);

if (mantissa == 0)
{
throw CreateEmberException("The mantissa of the Real at position {0} is zero.", position);
Expand Down
6 changes: 6 additions & 0 deletions Lawo.EmberPlusSharpTest/Ember/EmberReaderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,12 @@ public void RealTest()
AssertDecode(InnerNumber.Real, double.PositiveInfinity, 0x60, 0x03, 0x09, 0x01, 0x40);
AssertDecode(InnerNumber.Real, double.NegativeInfinity, 0x60, 0x03, 0x09, 0x01, 0x41);
AssertDecode(InnerNumber.Real, double.NaN, 0x60, 0x03, 0x09, 0x01, 0x42);

// Incorrectly encoded special values
AssertDecode(InnerNumber.Real, double.PositiveInfinity, 0x60, 0x06, 0x09, 0x04, 0x81, 0x04, 0x00, 0x00);
AssertDecode(InnerNumber.Real, double.NegativeInfinity, 0x60, 0x06, 0x09, 0x04, 0xC1, 0x04, 0x00, 0x00);
AssertDecode(InnerNumber.Real, double.NaN, 0x60, 0x0C, 0x09, 0x0A, 0x81, 0x04, 0x00, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF);

AssertDecode(InnerNumber.Real, -0.0, 0x60, 0x03, 0x09, 0x01, 0x43);
AssertDecode(InnerNumber.Real, 1.0, 0x60, 0x05, 0x09, 0x03, 0x80, 0x00, 0x01);
AssertDecode(InnerNumber.Real, -1.0, 0x60, 0x05, 0x09, 0x03, 0xC0, 0x00, 0x01);
Expand Down

0 comments on commit 5772696

Please sign in to comment.