// Encodes a FbDecFloat to its IEEE-754 format.
    public byte[] EncodeDecimal(FbDecFloat @decimal)
    {
        var decBytes = new byte[_decimalFormat.FormatByteLength];

        if (@decimal.Negative)
        {
            decBytes[0] = NegativeBit;
        }

        if (@decimal.Type == DecimalType.Finite)
        {
            EncodeFinite(@decimal, decBytes);
        }
        else
        {
            decBytes[0] |= GetSpecialBits(@decimal.Type);
        }

        // this (and related) code works with BE
        if (BitConverter.IsLittleEndian)
        {
            Array.Reverse(decBytes);
        }
        return(decBytes);
    }
Exemple #2
0
 public void Write(FbDecFloat value, int size)
 {
     WriteOpaque(size switch
     {
         16 => TypeEncoder.EncodeDec16(value),
         34 => TypeEncoder.EncodeDec34(value),
         _ => throw new ArgumentOutOfRangeException(),
     });
 public Task Write(FbDecFloat value, int size, AsyncWrappingCommonArgs async)
 {
     return(WriteOpaque(size switch
     {
         16 => TypeEncoder.EncodeDec16(value),
         34 => TypeEncoder.EncodeDec34(value),
         _ => throw new ArgumentOutOfRangeException(),
     }, async));
 public void ReadsValueCorrectly(FbDecFloat value, string castValue)
 {
     using (var cmd = Connection.CreateCommand())
     {
         cmd.CommandText = $"select cast('{castValue}' as decfloat(16)) from rdb$database";
         var result = (FbDecFloat)cmd.ExecuteScalar();
         Assert.AreEqual(value, result);
     }
 }
 public void PassesValueCorrectly(FbDecFloat value, string dummy)
 {
     using (var cmd = Connection.CreateCommand())
     {
         cmd.CommandText = "select cast(@value as decfloat(16)) from rdb$database";
         cmd.Parameters.AddWithValue("value", value);
         var result = (FbDecFloat)cmd.ExecuteScalar();
         Assert.AreEqual(value, result);
     }
 }
Exemple #6
0
        public static byte[] EncodeDec34(FbDecFloat value)
        {
            var result = DecimalCodec.DecFloat34.EncodeDecimal(value);

            if (BitConverter.IsLittleEndian)
            {
                Array.Reverse(result);
            }
            return(result);
        }
    void EncodeFinite(FbDecFloat @decimal, byte[] decBytes)
    {
        var biasedExponent       = _decimalFormat.BiasedExponent(@decimal.Exponent);
        var coefficient          = @decimal.Coefficient;
        var mostSignificantDigit = _coefficientCoder.EncodeValue(coefficient, decBytes);
        var expMSB        = UnsignedRightShift(biasedExponent, _decimalFormat.ExponentContinuationBits);
        var expTwoBitCont = UnsignedRightShift(biasedExponent, _decimalFormat.ExponentContinuationBits - 2) & 0b011;

        if (mostSignificantDigit <= 7)
        {
            decBytes[0] |= (byte)((expMSB << 5)
                                  | (mostSignificantDigit << 2)
                                  | expTwoBitCont);
        }
        else
        {
            decBytes[0] |= (byte)(Combination2
                                  | (expMSB << 3)
                                  | ((mostSignificantDigit & 0b01) << 2)
                                  | expTwoBitCont);
        }
        EncodeExponentContinuation(decBytes, biasedExponent, _decimalFormat.ExponentContinuationBits - 2);
    }
 public void EqualityFalse(FbDecFloat expected, FbDecFloat actual)
 {
     Assert.AreNotEqual(expected, actual);
 }
 public void EqualityTrue(FbDecFloat expected, FbDecFloat actual)
 {
     Assert.AreEqual(expected, actual);
 }