Exemple #1
0
        /// <include file='../../docs.xml'
        /// path='docs/doc[@name="M:PeterO.Numbers.EContext.WithRounding(PeterO.Numbers.ERounding)"]/*'/>
        public EContext WithRounding(ERounding rounding)
        {
            EContext pc = this.Copy();

            pc.rounding = rounding;
            return(pc);
        }
Exemple #2
0
 /// <include file='../../docs.xml'
 /// path='docs/doc[@name="M:PeterO.Numbers.EContext.#ctor(System.Int32,PeterO.Numbers.ERounding,System.Int32,System.Int32,System.Boolean)"]/*'/>
 public EContext(
     int precision,
     ERounding rounding,
     int exponentMinSmall,
     int exponentMaxSmall,
     bool clampNormalExponents)
 {
     if (precision < 0)
     {
         throw new ArgumentException("precision (" + precision +
                                     ") is less than 0");
     }
     if (exponentMinSmall > exponentMaxSmall)
     {
         throw new ArgumentException("exponentMinSmall (" + exponentMinSmall +
                                     ") is more than " + exponentMaxSmall);
     }
     this.bigintPrecision = precision == 0 ? EInteger.Zero :
                            (EInteger)precision;
     this.rounding             = rounding;
     this.clampNormalExponents = clampNormalExponents;
     this.hasExponentRange     = true;
     this.adjustExponent       = true;
     this.exponentMax          = exponentMaxSmall == 0 ? EInteger.Zero :
                                 (EInteger)exponentMaxSmall;
     this.exponentMin = exponentMinSmall == 0 ? EInteger.Zero :
                        (EInteger)exponentMinSmall;
 }
Exemple #3
0
 /// <summary>Creates a new EContext object initialized with an
 /// unlimited exponent range, and the given rounding mode and maximum
 /// precision.</summary>
 /// <param name='precision'>Maximum number of digits
 /// (precision).</param>
 /// <param name='rounding'>The parameter <paramref name='rounding'/> is
 /// an ERounding object.</param>
 /// <returns>A context object for arbitrary-precision arithmetic
 /// settings.</returns>
 public static EContext ForPrecisionAndRounding(
     int precision,
     ERounding rounding)
 {
     return(new EContext(
                precision,
                rounding,
                0,
                0,
                false).WithUnlimitedExponents());
 }
Exemple #4
0
 private EContext(
     bool adjustExponent,
     EInteger bigintPrecision,
     bool clampNormalExponents,
     EInteger exponentMax,
     EInteger exponentMin,
     int flags,
     bool hasExponentRange,
     bool hasFlags,
     bool precisionInBits,
     ERounding rounding,
     bool simplified,
     int traps)
 {
     if (bigintPrecision == null)
     {
         throw new ArgumentNullException(nameof(bigintPrecision));
     }
     if (exponentMin == null)
     {
         throw new ArgumentNullException(nameof(exponentMin));
     }
     if (exponentMax == null)
     {
         throw new ArgumentNullException(nameof(exponentMax));
     }
     if (bigintPrecision.Sign < 0)
     {
         throw new ArgumentException("precision(" + bigintPrecision +
                                     ") is less than 0");
     }
     if (exponentMin.CompareTo(exponentMax) > 0)
     {
         throw new ArgumentException("exponentMinSmall(" + exponentMin +
                                     ") is more than " + exponentMax);
     }
     this.adjustExponent       = adjustExponent;
     this.bigintPrecision      = bigintPrecision;
     this.clampNormalExponents = clampNormalExponents;
     this.exponentMax          = exponentMax;
     this.exponentMin          = exponentMin;
     this.flags            = flags;
     this.hasExponentRange = hasExponentRange;
     this.hasFlags         = hasFlags;
     this.precisionInBits  = precisionInBits;
     this.rounding         = rounding;
     this.simplified       = simplified;
     this.traps            = traps;
 }
Exemple #5
0
 /// <summary>Copies this EContext with the specified rounding
 /// mode.</summary>
 /// <param name='rounding'>Desired value of the Rounding
 /// property.</param>
 /// <returns>A context object for arbitrary-precision arithmetic
 /// settings.</returns>
 public EContext WithRounding(ERounding rounding)
 {
     return(new EContext(
                this.adjustExponent,
                this.bigintPrecision,
                this.clampNormalExponents,
                this.exponentMax,
                this.exponentMin,
                this.flags,
                this.hasExponentRange,
                this.hasFlags,
                this.precisionInBits,
                rounding,
                this.simplified,
                this.traps));
 }
Exemple #6
0
 /// <summary>Creates a new EContext object initialized with an
 /// unlimited precision, an unlimited exponent range, and the given
 /// rounding mode.</summary>
 /// <param name='rounding'>The rounding mode for the new precision
 /// context.</param>
 /// <returns>A context object for arbitrary-precision arithmetic
 /// settings.</returns>
 public static EContext ForRounding(ERounding rounding)
 {
     if (rounding == ERounding.HalfEven)
     {
         return(ForRoundingHalfEven);
     }
     if (rounding == ERounding.Down)
     {
         return(ForRoundingDown);
     }
     return(new EContext(
                0,
                rounding,
                0,
                0,
                false).WithUnlimitedExponents());
 }
#pragma warning disable CS0618 // certain ERounding values are obsolete
        private T SignalOverflow2(EContext pc, bool neg)
        {
            if (pc != null)
            {
                ERounding roundingOnOverflow = pc.Rounding;
                if (pc.HasFlags)
                {
                    pc.Flags |= EContext.FlagOverflow |
                                EContext.FlagInexact | EContext.FlagRounded;
                }
                if (pc.HasMaxPrecision && pc.HasExponentRange &&
                    (roundingOnOverflow == ERounding.Down ||
                     roundingOnOverflow == ERounding.ZeroFiveUp ||
                     roundingOnOverflow == ERounding.OddOrZeroFiveUp ||
                     roundingOnOverflow == ERounding.Odd ||
                     (roundingOnOverflow == ERounding.Ceiling && neg) ||
                     (roundingOnOverflow == ERounding.Floor && !neg)))
                {
                    // Set to the highest possible value for
                    // the given precision
                    EInteger    overflowMant  = EInteger.Zero;
                    FastInteger fastPrecision = FastInteger.FromBig(pc.Precision);
                    overflowMant = this.GetHelper().MultiplyByRadixPower(
                        EInteger.One,
                        fastPrecision);
                    overflowMant -= EInteger.One;
                    FastInteger clamp =
                        FastInteger.FromBig(pc.EMax).Increment().Subtract(fastPrecision);
                    return(this.GetHelper().CreateNewWithFlags(
                               overflowMant,
                               clamp.AsEInteger(),
                               neg ? BigNumberFlags.FlagNegative : 0));
                }
            }
            int flagneg = neg ? BigNumberFlags.FlagNegative : 0;

            return(this.GetHelper().GetArithmeticSupport() ==
                   BigNumberFlags.FiniteOnly ?
                   default(T) : this.GetHelper().CreateNewWithFlags(
                       EInteger.Zero,
                       EInteger.Zero,
                       flagneg | BigNumberFlags.FlagInfinity));
        }
Exemple #8
0
 /// <summary>Initializes a new instance of the
 /// <see cref='PeterO.Numbers.EContext'/> class,.</summary>
 /// <param name='bigintPrecision'>The value of the Precision
 /// property.</param>
 /// <param name='rounding'>The value of the Rounding property.</param>
 /// <param name='exponentMin'>The value of the EMin property.</param>
 /// <param name='exponentMax'>The value of the EMax property.</param>
 /// <param name='clampNormalExponents'>The value of the
 /// ClampNormalExponents property.</param>
 public EContext(
     EInteger bigintPrecision,
     ERounding rounding,
     EInteger exponentMin,
     EInteger exponentMax,
     bool clampNormalExponents) : this(
         true,
         bigintPrecision,
         clampNormalExponents,
         exponentMax,
         exponentMin,
         0,
         true,
         false,
         false,
         rounding,
         false,
         0)
 {
 }
Exemple #9
0
 /// <summary>Initializes a new instance of the
 /// <see cref='PeterO.Numbers.EContext'/> class.</summary>
 /// <param name='precision'>The value of the Precision
 /// property.</param>
 /// <param name='rounding'>The value of the Rounding property.</param>
 /// <param name='exponentMinSmall'>The value of the EMin
 /// property.</param>
 /// <param name='exponentMaxSmall'>The value of the EMax
 /// property.</param>
 /// <param name='clampNormalExponents'>The value of the
 /// ClampNormalExponents property.</param>
 public EContext(
     int precision,
     ERounding rounding,
     int exponentMinSmall,
     int exponentMaxSmall,
     bool clampNormalExponents) : this(
         true,
         EInteger.FromInt32(precision),
         clampNormalExponents,
         EInteger.FromInt32(exponentMaxSmall),
         EInteger.FromInt32(exponentMinSmall),
         0,
         true,
         false,
         false,
         rounding,
         false,
         0)
 {
 }
Exemple #10
0
 /// <include file='../../docs.xml'
 /// path='docs/doc[@name="M:PeterO.Numbers.EContext.WithRounding(PeterO.Numbers.ERounding)"]/*'/>
 public EContext WithRounding(ERounding rounding) {
   EContext pc = this.Copy();
   pc.rounding = rounding;
   return pc;
 }
Exemple #11
0
   /// <include file='../../docs.xml'
   /// path='docs/doc[@name="M:PeterO.Numbers.EContext.ForRounding(PeterO.Numbers.ERounding)"]/*'/>
   public static EContext ForRounding(ERounding rounding) {
     if (rounding == ERounding.HalfEven) {
       return ForRoundingHalfEven;
     }
     if (rounding == ERounding.Down) {
       return ForRoundingDown;
     }
     return new EContext(
 0,
 rounding,
 0,
 0,
 false).WithUnlimitedExponents();
   }
Exemple #12
0
   /// <include file='../../docs.xml'
   /// path='docs/doc[@name="M:PeterO.Numbers.EContext.ForPrecisionAndRounding(System.Int32,PeterO.Numbers.ERounding)"]/*'/>
   public static EContext ForPrecisionAndRounding(
     int precision,
     ERounding rounding) {
     return new EContext(
 precision,
 rounding,
 0,
 0,
 false).WithUnlimitedExponents();
   }
Exemple #13
0
   /// <include file='../../docs.xml'
   /// path='docs/doc[@name="M:PeterO.Numbers.EContext.#ctor(System.Int32,PeterO.Numbers.ERounding,System.Int32,System.Int32,System.Boolean)"]/*'/>
   public EContext(
 int precision,
 ERounding rounding,
 int exponentMinSmall,
 int exponentMaxSmall,
 bool clampNormalExponents) {
     if (precision < 0) {
       throw new ArgumentException("precision (" + precision +
         ") is less than 0");
     }
     if (exponentMinSmall > exponentMaxSmall) {
       throw new ArgumentException("exponentMinSmall (" + exponentMinSmall +
         ") is more than " + exponentMaxSmall);
     }
     this.bigintPrecision = precision == 0 ? EInteger.Zero :
       (EInteger)precision;
     this.rounding = rounding;
     this.clampNormalExponents = clampNormalExponents;
     this.hasExponentRange = true;
     this.adjustExponent = true;
     this.exponentMax = exponentMaxSmall == 0 ? EInteger.Zero :
       (EInteger)exponentMaxSmall;
     this.exponentMin = exponentMinSmall == 0 ? EInteger.Zero :
       (EInteger)exponentMinSmall;
   }
Exemple #14
0
   private void TestRoundToExponentOne(
     string input,
     string expected,
     int exponent,
     ERounding rounding) {
     EDecimal inputED = EDecimal.FromString(input);
     inputED = inputED.RoundToExponent(
 exponent,
 EContext.ForRounding(rounding));
     Assert.AreEqual(expected, inputED.ToString());
   }