mirror of
https://github.com/StockSharp/StockSharp.git
synced 2026-12-04 08:32:23 +00:00
A NUMBER THAT CANNOT BE NEGATED. The writer took the absolute value of a negative number before writing its digits, and the most negative long has no positive counterpart - negating it overflows. It builds the magnitude without negating now, and a test writes long.MinValue and reads it back. The Long test beside it still says in a comment that the text format cannot round-trip that value exactly, and works around it with MinValue + 1. If the new test goes red, that comment was right and this is incomplete; it is left standing rather than quietly corrected, because which of the two is true is a question for a run. A TAG THAT IS NOT A TAG. A tag is a positive number followed by an equals sign, and anything else was read on regardless: a zero, a negative number or a tag with no value would each have named a field nobody sent and carried whatever bytes followed into it. Such a frame is refused. A stream that ends between messages still reads as the empty tag, which is how a session that closed cleanly ends, and the reader no longer throws on it. A SCALED NUMBER THAT LOST ITS DIGITS. Its decimal value went through a double, which cannot hold every decimal a price can be; it is built from the mantissa and exponent directly. Adding a scaled zero to a number now leaves that number rather than moving its exponent. AN EMPTY BOOK THAT EMPTIED EVERY BOOK. Told that one instrument has no quotes left, the dialect cleared the quotes it held for all of them. It clears the one it was told about, and the empty book it hands on names the subscription that asked - a book a subscriber cannot attribute is a book it cannot apply. TEXT THAT WAS NOT LATIN. The conversion to Latin ran over the whole string through a helper that returned it unchanged when there was nothing to convert, and built a new string every time regardless; it now walks the characters once and leaves an empty value alone.
96 lines
2.9 KiB
C#
96 lines
2.9 KiB
C#
namespace StockSharp.Fix.Native;
|
|
|
|
using System.Globalization;
|
|
using System.Numerics;
|
|
|
|
/// <summary>
|
|
/// Scaled numbers, like floating point numbers are represented as a mantissa and an exponent.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// Initializes a new instance of the <see cref="ScaledNumber"/>.
|
|
/// </remarks>
|
|
/// <param name="exponent">Exponent.</param>
|
|
/// <param name="mantissa">Mantissa.</param>
|
|
public readonly struct ScaledNumber(int exponent, long mantissa)
|
|
{
|
|
|
|
/// <summary>
|
|
/// Mantissa.
|
|
/// </summary>
|
|
public readonly long Mantissa = mantissa;
|
|
|
|
/// <summary>
|
|
/// Exponent.
|
|
/// </summary>
|
|
public readonly int Exponent = exponent;
|
|
|
|
/// <summary>
|
|
/// The numerical value is obtained by multiplying the mantissa with the base-10 power of the exponent.
|
|
/// </summary>
|
|
public double AsDouble => Mantissa * Math.Pow(10, Exponent);
|
|
|
|
/// <summary>
|
|
/// The numerical value is obtained by multiplying the mantissa with the base-10 power of the exponent.
|
|
/// </summary>
|
|
public decimal AsDecimal => MathHelper.ToDecimal(Mantissa, Exponent);
|
|
|
|
/// <inheritdoc />
|
|
public override string ToString() => Exponent == 0 ? Mantissa.ToString() : AsDouble.ToString(CultureInfo.InvariantCulture);
|
|
|
|
/// <summary>
|
|
/// Add the two objects <see cref="ScaledNumber"/>.
|
|
/// </summary>
|
|
/// <param name="n1">First object <see cref="ScaledNumber"/>.</param>
|
|
/// <param name="n2">Second object <see cref="ScaledNumber"/>.</param>
|
|
/// <returns>The result of addition.</returns>
|
|
public static ScaledNumber operator +(ScaledNumber n1, ScaledNumber n2)
|
|
{
|
|
if (n1.Mantissa == 0)
|
|
return n2;
|
|
|
|
if (n2.Mantissa == 0)
|
|
return n1;
|
|
|
|
var higher = n1.Exponent >= n2.Exponent ? n1 : n2;
|
|
var lower = n1.Exponent >= n2.Exponent ? n2 : n1;
|
|
var exponentDifference = (long)higher.Exponent - lower.Exponent;
|
|
|
|
// A long mantissa carries at most 19 decimal digits. Past this gap the lower-scaled
|
|
// operand cannot affect the rounded representation of the higher-scaled operand.
|
|
if (exponentDifference > 38)
|
|
return higher;
|
|
|
|
var sum = (BigInteger)higher.Mantissa * BigInteger.Pow(10, (int)exponentDifference) + lower.Mantissa;
|
|
return FromBigInteger(sum, lower.Exponent);
|
|
}
|
|
|
|
private static ScaledNumber FromBigInteger(BigInteger value, int exponent)
|
|
{
|
|
if (value.IsZero)
|
|
return new ScaledNumber(0, 0);
|
|
|
|
var resultExponent = (long)exponent;
|
|
var divisor = BigInteger.One;
|
|
|
|
while (true)
|
|
{
|
|
var mantissa = BigInteger.DivRem(value, divisor, out var remainder);
|
|
|
|
if (mantissa >= long.MinValue && mantissa <= long.MaxValue)
|
|
{
|
|
if (divisor > BigInteger.One && BigInteger.Abs(remainder) * 2 >= divisor)
|
|
mantissa += value.Sign;
|
|
|
|
if (mantissa >= long.MinValue && mantissa <= long.MaxValue)
|
|
return new ScaledNumber(checked((int)resultExponent), (long)mantissa);
|
|
}
|
|
|
|
if (resultExponent == int.MaxValue)
|
|
throw new OverflowException("The scaled number exponent is too large.");
|
|
|
|
divisor *= 10;
|
|
resultExponent++;
|
|
}
|
|
}
|
|
}
|