Skip to content

Commit

Permalink
Add new metric prefixes
Browse files Browse the repository at this point in the history
  • Loading branch information
lamont-granquist committed May 15, 2023
1 parent 6dc55c3 commit a380a1f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
6 changes: 3 additions & 3 deletions MechJeb2/MechJebLib/Utils/Statics.cs
Expand Up @@ -412,8 +412,8 @@ public static double DoubleArrayMagnitude(IList<double> array)
return Math.Sqrt(sumsq);
}

private static readonly string[] _posPrefix = { " ", "k", "M", "G", "T", "P", "E", "Z", "Y" };
private static readonly string[] _negPrefix = { " ", "m", "μ", "n", "p", "f", "a", "z", "y" };
private static readonly string[] _posPrefix = { " ", "k", "M", "G", "T", "P", "E", "Z", "Y", "R", "Q" };
private static readonly string[] _negPrefix = { " ", "m", "μ", "n", "p", "f", "a", "z", "y", "r", "q" };

public static string ToSI(this double d, int maxPrecision = -99, int sigFigs = 4)
{
Expand All @@ -427,7 +427,7 @@ public static string ToSI(this double d, int maxPrecision = -99, int sigFigs = 4
int exponent = (int)Math.Floor(Math.Log10(Math.Abs(d) + offset));

int index = d != 0 ? (int)Math.Abs(Math.Floor(exponent / 3.0)) : 0; // index of the SI prefix
if (index > 8) index = 8; // there's only 8 SI prefixes
if (index > 10) index = 10; // there's only 10 SI prefixes

int siExponent = Math.Sign(exponent) * index * 3; // the SI prefix exponent

Expand Down
24 changes: 17 additions & 7 deletions MechJebLibTest/Utils/StaticTests.cs
Expand Up @@ -52,8 +52,10 @@ public void ClampTest()
[Fact]
public void ToSITest()
{
Assert.Equal("0.000001000 y", 1e-30.ToSI());
Assert.Equal("0.001000 y", 1e-27.ToSI());
Assert.Equal("0.000001000 q", 1e-36.ToSI());
Assert.Equal("0.001000 q", 1e-33.ToSI());
Assert.Equal("1.000 q", 1e-30.ToSI());
Assert.Equal("1.000 r", 1e-27.ToSI());
Assert.Equal("1.000 y", 1e-24.ToSI());
Assert.Equal("1.000 z", 1e-21.ToSI());
Assert.Equal("1.000 a", 1e-18.ToSI());
Expand Down Expand Up @@ -87,8 +89,10 @@ public void ToSITest()
Assert.Equal("1.000 E", 1e18.ToSI());
Assert.Equal("1.000 Z", 1e21.ToSI());
Assert.Equal("1.000 Y", 1e24.ToSI());
Assert.Equal("1000 Y", 1e27.ToSI());
Assert.Equal("1000000 Y", 1e30.ToSI());
Assert.Equal("1.000 R", 1e27.ToSI());
Assert.Equal("1.000 Q", 1e30.ToSI());
Assert.Equal("1000 Q", 1e33.ToSI());
Assert.Equal("1000000 Q", 1e36.ToSI());
Assert.Equal("-1.000 ", (-1d).ToSI());
Assert.Equal("-12.00 ", (-12d).ToSI());
Assert.Equal("-123.0 ", (-123d).ToSI());
Expand All @@ -102,9 +106,15 @@ public void ToSITest()
Assert.Equal("Infinity", double.PositiveInfinity.ToSI());
Assert.Equal("-Infinity", double.NegativeInfinity.ToSI());

Assert.Equal("0 y", 1.23456e-27.ToSI(-1));
Assert.Equal("0 y", 1.23456e-26.ToSI(-1));
Assert.Equal("0 y", 1.23456e-25.ToSI(-1));
Assert.Equal("0 q", 1.23456e-33.ToSI(-1));
Assert.Equal("0 q", 1.23456e-32.ToSI(-1));
Assert.Equal("0 q", 1.23456e-31.ToSI(-1));
Assert.Equal("1 q", 1.23456e-30.ToSI(-1));
Assert.Equal("12 q", 1.23456e-29.ToSI(-1));
Assert.Equal("123 q", 1.23456e-28.ToSI(-1));
Assert.Equal("1 r", 1.23456e-27.ToSI(-1));
Assert.Equal("12 r", 1.23456e-26.ToSI(-1));
Assert.Equal("123 r", 1.23456e-25.ToSI(-1));
Assert.Equal("1 y", 1.23456e-24.ToSI(-1));
Assert.Equal("12 y", 1.23456e-23.ToSI(-1));
Assert.Equal("123 y", 1.23456e-22.ToSI(-1));
Expand Down

0 comments on commit a380a1f

Please sign in to comment.