Ejemplo n.º 1
0
        public static UnsignedFraction ToUnsignedFraction(double value)
        {
            if (value < 0)
            {
                throw new InvalidCastException();
            }

            if (value > UInt32.MaxValue)
            {
                throw new OverflowException();
            }

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

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

            fraction.Numerator = (UInt32)System.Math.Floor(value);
            fraction.Denominator = denominator;
            return fraction;
        }
Ejemplo n.º 2
0
 public static byte[] ToByte(UnsignedFraction 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;
 }