Ejemplo n.º 1
0
 public Complex RealAbs()
 {
     if (!Imag.IsZero)
     {
         throw new InvalidOperationException();
     }
     return(new Complex(Real.Abs(), Imag, IsExact));
 }
Ejemplo n.º 2
0
        public static string ToString(Expression Value, Units Units, string format, IFormatProvider formatProvider)
        {
            StringBuilder SB = new StringBuilder();

            Constant constant = Value as Constant;

            if (constant != null)
            {
                if (format != null && format.StartsWith("+"))
                {
                    if (constant.Value >= 0)
                    {
                        SB.Append("+");
                    }
                    format = format.Remove(0, 1);
                }

                // Find out how many digits the format has.
                double round = 1.0;
                for (int significant = 12; significant > 0; --significant)
                {
                    round = 1.0 + 5 * Math.Pow(10.0, -significant);

                    if (double.Parse(round.ToString(format)) != 1.0)
                    {
                        break;
                    }
                }

                // Find the order of magnitude of the value.
                Real order = Real.Log10(Real.Abs(constant.Value.IsNaN() ? 0 : constant.Value) * round);
                if (order < -20)
                {
                    order = 0;
                }
                else if (order == Real.Infinity)
                {
                    order = 0;
                }

                int prefix = Math.Max(Math.Min((int)Real.Floor(order / 3) * 3, MaxPrefix), MinPrefix);

                Value = Value / (((Real)10) ^ prefix);
                SB.Append(((IFormattable)Value).ToString(format, formatProvider));
                SB.Append(" ");
                SB.Append(Prefixes.Single(i => i.Value == prefix).Key);
            }
            else if (Value != null)
            {
                SB.Append(Value.ToString());
                SB.Append(" ");
            }
            else
            {
                SB.Append("0");
                SB.Append(" ");
            }
            SB.Append(Units);

            return(SB.ToString().Trim());
        }