Skip to content

Commit

Permalink
some CompoundString imrpvements
Browse files Browse the repository at this point in the history
  • Loading branch information
burdoto committed Jul 12, 2023
1 parent 124d129 commit 2ba862c
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions common/Units.cs
Original file line number Diff line number Diff line change
Expand Up @@ -270,19 +270,29 @@ public FactorUnitChain(bool compoundString, IList<(Unit unit, double factorFromP
public string ToCompoundString(UnitValue it)
{
var str = string.Empty;
var i = chain.Count - 1;

for (var i = chain.Count - 1; i > -1; i--)
while ((it | chain[i].unit) < 1)
i--;
if (i < 0) i = 0;

for (; i > -1; i--)
{
var link = chain[i];
var val = it | link.unit;
using var iter = chain.Reverse().GetEnumerator();

if (val >= 1)
{
var here = (int)val * link.unit;
var here = (double)val * link.unit;
str += here.ToString();
it -= here;
}
else if (i >= chain.Count)
{
var here = (int)val * link.unit;
str += here.ToString();
break;
}
}

return str;
Expand Down Expand Up @@ -533,10 +543,10 @@ public override List<UnitAccumulatorStrategy> Strategies

public static UnitValue operator +(double right, UnitValue left) => left + right;
public static UnitValue operator +(UnitValue left, double right) => left + (left as Unit) * right;
public static UnitValue operator +(UnitValue left, UnitValue right) => (left as Unit) * (left.Value + right.Value); // TODO does not work right when subtracting different si prefixes
public static UnitValue operator +(UnitValue left, UnitValue right) => (left as Unit) * ((double) left + (double) right);
public static UnitValue operator -(double right, UnitValue left) => left - right;
public static UnitValue operator -(UnitValue left, double right) => left - (left as Unit) * right;
public static UnitValue operator -(UnitValue left, UnitValue right) => (left as Unit) * (left.Value - right.Value); // TODO does not work right when subtracting different si prefixes
public static UnitValue operator -(UnitValue left, UnitValue right) => (left as Unit) * ((double) left - (double) right);
public static UnitValue operator *(double right, UnitValue l) => l * right;
public static UnitValue operator *(UnitValue l, double right) => l * (Units.EmptyUnit * right);
public static UnitValue operator *(UnitValue l, UnitValue r)
Expand Down

0 comments on commit 2ba862c

Please sign in to comment.