Ejemplo n.º 1
0
 void CheckArgumentAutoStdPrefix(AtomicMeasureUnit m, AutoStandardPrefix stdPrefix)
 {
     if (m.AutoStandardPrefix != stdPrefix)
     {
         ThrowArgumentException(m, $"new AutoStandardPrefix '{stdPrefix}' differ from '{m.AutoStandardPrefix}'.");
     }
 }
Ejemplo n.º 2
0
        ExponentMeasureUnit CreateExponentPrefixed(int exp, ExpFactor adjustment, AtomicMeasureUnit atomic)
        {
            ExponentMeasureUnit u;

            if (!adjustment.IsNeutral)
            {
                var best = MeasureStandardPrefix.FindBest(adjustment, atomic.AutoStandardPrefix);
                atomic = RegisterPrefixed(best.Adjustment, best.Prefix, atomic);
            }
            u = RegisterExponent(exp, atomic);
            return(u);
        }
Ejemplo n.º 3
0
 public void Add(AtomicMeasureUnit u, int exp)
 {
     if (u.AtomicMeasureUnit.Normalization == None)
     {
         var fp = u.AtomicMeasureUnit.NormalizationFactor.ExpFactor.Power(exp);
         _dimensionLessFactor = _dimensionLessFactor.Multiply(fp);
     }
     else
     {
         for (int i = 0; i < _normM.Count; ++i)
         {
             if (_normM[i] == u)
             {
                 _normE[i] += exp;
                 return;
             }
         }
         _normM.Add(u);
         _normE.Add(exp);
     }
 }
Ejemplo n.º 4
0
 /// <summary>
 /// Applies this metric to a <see cref="AtomicMeasureUnit"/>.
 /// </summary>
 /// <param name="unit">The original unit.</param>
 /// <returns>The resulting unit.</returns>
 public AtomicMeasureUnit On(AtomicMeasureUnit unit) => Combine(this, unit, true).Result;
Ejemplo n.º 5
0
 /// <summary>
 /// Applies this metric to a <see cref="AtomicMeasureUnit"/>.
 /// </summary>
 /// <param name="unit">The original unit.</param>
 /// <returns>The resulting unit.</returns>
 public AtomicMeasureUnit this[AtomicMeasureUnit unit] => Combine(this, unit, true).Result;
Ejemplo n.º 6
0
 static (ExpFactor Adjustment, AtomicMeasureUnit Result) Combine(MeasureStandardPrefix prefix, AtomicMeasureUnit u, bool allowAdjustmentPrefix)
 {
     if (prefix.Base == 0)
     {
         return(ExpFactor.Neutral, u);
     }
     if (u is PrefixedMeasureUnit p)
     {
         var newExp = prefix.Factor.Multiply(p.Prefix.Factor).Multiply(p.AdjustmentFactor);
         if (newExp.IsNeutral)
         {
             return(ExpFactor.Neutral, p.AtomicMeasureUnit);
         }
         var best = FindBest(newExp, p.AtomicMeasureUnit.AutoStandardPrefix);
         if (!allowAdjustmentPrefix)
         {
             return(best.Adjustment, u.Context.RegisterPrefixed(ExpFactor.Neutral, best.Prefix, p.AtomicMeasureUnit));
         }
         return(ExpFactor.Neutral, u.Context.RegisterPrefixed(best.Adjustment, best.Prefix, p.AtomicMeasureUnit));
     }
     if ((u.AutoStandardPrefix & AutoStandardPrefix.Binary) == 0 && prefix.Base == 2 ||
         (u.AutoStandardPrefix & AutoStandardPrefix.Metric) == 0 && prefix.Base == 10)
     {
         return(ExpFactor.Neutral, u.Context.RegisterPrefixed(prefix.Factor, None, u));
     }
     return(ExpFactor.Neutral, u.Context.RegisterPrefixed(ExpFactor.Neutral, prefix, u));
 }
Ejemplo n.º 7
0
 /// <summary>
 /// Applies this metric to a <see cref="AtomicMeasureUnit"/>, allowing the creation of
 /// potentially adjusted <see cref="PrefixedMeasureUnit"/>.
 /// </summary>
 /// <param name="unit">The original unit.</param>
 /// <param name="allowAdjustmentPrefix">
 /// When true, this is the same as calling <see cref="this[AtomicMeasureUnit]"/>: the resulting <see cref="PrefixedMeasureUnit"/>
 /// can have a non neutral <see cref="PrefixedMeasureUnit.AdjustmentFactor"/>.
 /// When false, if there is the need of an adjusment prefix (for instance on a "DeciMega") the adjustement will be returned and
 /// the resulting AtomicMeasureUnit will have a neutral adjustment.
 /// </param>
 /// <returns>The resulting adjustment and unit.</returns>
 public (ExpFactor Adjustment, AtomicMeasureUnit Result) On(AtomicMeasureUnit unit, bool allowAdjustmentPrefix) => Combine(this, unit, allowAdjustmentPrefix);
Ejemplo n.º 8
0
        /// <summary>
        /// The isNormalized is nullable: when automatically creating PrefixedUnit this is null and :
        ///  - if the unit must be created it won't be the normalized one.
        ///  - if the unit is found, we don't check the potential race condition.
        /// Only when explicitly registering/creating the unit with true or false, the resulting unit IsNormalized
        /// property is checked to prevent race conditions.
        /// </summary>
        internal PrefixedMeasureUnit RegisterPrefixed(ExpFactor adjustment, MeasureStandardPrefix p, AtomicMeasureUnit u, bool?isNormalized = null)
        {
            var names = PrefixedMeasureUnit.ComputeNames(adjustment, p, u);

            return(Register(names.A, names.N, () => new PrefixedMeasureUnit(this, names, adjustment, p, u, isNormalized ?? false), m => !isNormalized.HasValue || m.IsNormalized == isNormalized));
        }
Ejemplo n.º 9
0
        internal ExponentMeasureUnit RegisterExponent(int exp, AtomicMeasureUnit u)
        {
            var names = ExponentMeasureUnit.ComputeNames(exp, u);

            return(Register(names.A, names.N, () => new ExponentMeasureUnit(this, names, exp, u), null));
        }
Ejemplo n.º 10
0
        /// <summary>
        /// The isNormalized is nullable: when automatically creating PrefixedUnit this is null and:
        ///  - if the unit must be created it won't be the normalized one.
        ///  - if the unit is found, we don't check the potential race condition.
        /// Only when explicitly registering/creating the unit with true or false, the resulting unit IsNormalized
        /// property is checked to prevent race conditions.
        /// </summary>
        internal PrefixedMeasureUnit RegisterPrefixed(ExpFactor adjustment, MeasureStandardPrefix p, AtomicMeasureUnit u, bool?isNormalized = null)
        {
            var names = PrefixedMeasureUnit.ComputeNames(adjustment, p, u);

            return(Register(names.A, names.N, () => new PrefixedMeasureUnit(this, names, adjustment, p, u, isNormalized ?? false), m =>
            {
                if (isNormalized.HasValue && m.IsNormalized != isNormalized)
                {
                    ThrowArgumentException(m, $"new IsNormalized '{isNormalized}' differ from '{m.IsNormalized}'.");
                }
            }));
        }