예제 #1
0
        private bool AbsLessThenMin(MbfFloat minValue)
        {
            if (Exponent != minValue.Exponent)
            {
                return(Exponent < minValue.Exponent);
            }

            return(Mantissa < minValue.Mantissa);
        }
예제 #2
0
        private bool AbsGreaterThenMax(MbfFloat maxValue)
        {
            if (Exponent != maxValue.Exponent)
            {
                return(Exponent > maxValue.Exponent);
            }

            return(Mantissa > maxValue.Mantissa);
        }
예제 #3
0
        public void Add(MbfFloat right, bool normalize = true)
        {
            if (right.IsZero)
            {
                return;
            }

            if (IsZero)
            {
                Mantissa   = right.Mantissa;
                Exponent   = right.Exponent;
                IsNegitive = right.IsNegitive;
                return;
            }

            // ensure right has largest exponent
            if (Exponent > right.Exponent)
            {
                var temp = ToMbfFloat();
                Mantissa   = right.Mantissa;
                Exponent   = right.Exponent;
                IsNegitive = right.IsNegitive;

                right = temp;
            }

            // denormalise left to match exponents
            while (Exponent < right.Exponent)
            {
                Exponent  += 1;
                Mantissa >>= 1;
            }

            // add mantissas, taking sign into account
            if (IsNegitive == right.IsNegitive)
            {
                Mantissa += right.Mantissa;
            }
            else
            {
                if (Mantissa > right.Mantissa)
                {
                    Mantissa -= right.Mantissa;
                }
                else
                {
                    Mantissa   = right.Mantissa - Mantissa;
                    IsNegitive = right.IsNegitive;
                }
            }

            if (normalize)
            {
                Normalize();
            }
        }
예제 #4
0
        protected bool Equals(MbfFloat mbf)
        {
            if (IsZero)
            {
                return(mbf.IsZero);
            }

            return(mbf.IsNegitive == IsNegitive &&
                   mbf.Exponent == Exponent &&
                   mbf.Mantissa == Mantissa);
        }
예제 #5
0
        public MbfFloatParser(MbfFloat fp)
            : this(fp is MbfDouble)
        {
            if (fp == null)
            {
                throw new ArgumentNullException(nameof(fp));
            }

            Mantissa   = fp.Mantissa;
            Exponent   = fp.Exponent;
            IsNegitive = fp.IsNegitive;
        }
예제 #6
0
        public void Divide(MbfFloat right)
        {
            if (right.IsZero)
            {
                var max = IsDouble ? (MbfFloat)MbfDouble.Max : MbfSingle.Max;
                Exponent = max.Exponent;
                Mantissa = max.Exponent;

                throw new DivideByZeroException();
            }

            if (IsZero)
            {
                return;
            }

            // signs
            IsNegitive = IsNegitive != right.IsNegitive;

            // subtract exponentials
            Exponent -= (byte)(right.Exponent - right.MbfBias - 8);

            // long division of mantissas
            var leftMantissa  = Mantissa;
            var rightMantissa = right.Mantissa;

            Mantissa  = 0;
            Exponent += 1;
            while (rightMantissa > 0)
            {
                Mantissa <<= 1;
                Exponent  -= 1;

                if (leftMantissa > rightMantissa)
                {
                    leftMantissa -= rightMantissa;
                    Mantissa     += 1;
                }

                rightMantissa >>= 1;
            }

            Normalize();
        }