Пример #1
0
        public static SignedFraction ToSignedFraction(double value)
        {
            if (value > Int32.MaxValue)
            {
                throw new OverflowException();
            }

            if (value < Int32.MinValue)
            {
                throw new InvalidCastException();
            }

            var fraction = new SignedFraction();
            Int32 denominator = 1;
            while (value - System.Math.Floor(value) != 0 && value < Int32.MaxValue && denominator < Int32.MaxValue)
            {
                value *= 10;
                denominator *= 10;
            }

            if (value > Int32.MaxValue || denominator > Int32.MaxValue)
            {
                throw new OverflowException();
            }

            fraction.Numerator = (Int32)System.Math.Floor(value);
            fraction.Denominator = denominator;
            return fraction;
        }
Пример #2
0
 public static byte[] ToByte(SignedFraction value, Definitions.Endian endian)
 {
     var ret = new byte[8];
     Array.Copy(Util.ToByte(value.Numerator, 4, endian), 0, ret, 0, 4);
     Array.Copy(Util.ToByte(value.Denominator, 4, endian), 0, ret, 4, 4);
     return ret;
 }