use Double for Units. DecimalEx lib had too many bugs

This commit is contained in:
ig 2023-04-25 11:27:57 +02:00
parent 216ddda78f
commit 36af277559
30 changed files with 176 additions and 232 deletions

View File

@ -1,6 +1,5 @@
using DecimalMath;
using InnovEnergy.Lib.Units.Generator;
using InnovEnergy.Lib.Utils;
using static System.Math;
namespace InnovEnergy.Lib.Units;
@ -10,16 +9,12 @@ public readonly partial struct Angle
public static String Unit => "rad";
public static String Symbol => "∠";
public static readonly Angle Pi = new Angle(DecimalEx.Pi);
public Angle(Decimal value)
public Angle(Double value)
{
var modulo = value.Modulo(DecimalEx.TwoPi);
var modulo = value % Tau; // tau is 2pi
Value = modulo > DecimalEx.Pi
? modulo - DecimalEx.TwoPi
Value = modulo > PI
? modulo - Tau
: modulo;
}
}

View File

@ -15,14 +15,14 @@ using T = Angle;
[JsonConverter(typeof(AngleConverter))]
public readonly partial struct Angle
{
public Decimal Value { get; }
public Double Value { get; }
public override String ToString() => Value.RoundToSignificantDigits(Units.DisplaySignificantDigits) + Unit;
// scalar multiplication
public static T operator *(Decimal scalar, T t) => new T(scalar * t.Value);
public static T operator *(T t, Decimal scalar) => new T(scalar * t.Value);
public static T operator /(T t, Decimal scalar) => new T(t.Value / scalar);
public static T operator *(Double scalar, T t) => new T(scalar * t.Value);
public static T operator *(T t, Double scalar) => new T(scalar * t.Value);
public static T operator /(T t, Double scalar) => new T(t.Value / scalar);
// addition
@ -41,10 +41,7 @@ public readonly partial struct Angle
// conversion
public static implicit operator T(Decimal d) => new T(d);
public static implicit operator T(Double d) => new T((Decimal)d);
public static implicit operator T(Int32 i) => new T(i);
public static implicit operator Decimal(T t) => t.Value;
public static implicit operator T(Double d) => new T(d);
// equality
@ -59,7 +56,7 @@ internal class AngleConverter : JsonConverter<Angle>
{
public override Angle Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return new Angle(reader.GetDecimal());
return new Angle(reader.GetDouble());
}
public override void Write(Utf8JsonWriter writer, Angle value, JsonSerializerOptions options)

View File

@ -8,7 +8,7 @@ public readonly partial struct ApparentPower
public static String Unit => "VA";
public static String Symbol => "S";
public ApparentPower(Decimal value)
public ApparentPower(Double value)
{
if (value < 0) throw new ArgumentException("Apparent power cannot be negative", nameof(value));
Value = value;

View File

@ -15,14 +15,14 @@ using T = ApparentPower;
[JsonConverter(typeof(ApparentPowerConverter))]
public readonly partial struct ApparentPower
{
public Decimal Value { get; }
public Double Value { get; }
public override String ToString() => Value.RoundToSignificantDigits(Units.DisplaySignificantDigits) + Unit;
// scalar multiplication
public static T operator *(Decimal scalar, T t) => new T(scalar * t.Value);
public static T operator *(T t, Decimal scalar) => new T(scalar * t.Value);
public static T operator /(T t, Decimal scalar) => new T(t.Value / scalar);
public static T operator *(Double scalar, T t) => new T(scalar * t.Value);
public static T operator *(T t, Double scalar) => new T(scalar * t.Value);
public static T operator /(T t, Double scalar) => new T(t.Value / scalar);
// addition
@ -41,10 +41,7 @@ public readonly partial struct ApparentPower
// conversion
public static implicit operator T(Decimal d) => new T(d);
public static implicit operator T(Double d) => new T((Decimal)d);
public static implicit operator T(Int32 i) => new T(i);
public static implicit operator Decimal(T t) => t.Value;
public static implicit operator T(Double d) => new T(d);
// equality
@ -59,7 +56,7 @@ internal class ApparentPowerConverter : JsonConverter<ApparentPower>
{
public override ApparentPower Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return new ApparentPower(reader.GetDecimal());
return new ApparentPower(reader.GetDouble());
}
public override void Write(Utf8JsonWriter writer, ApparentPower value, JsonSerializerOptions options)

View File

@ -1,12 +1,26 @@
using System.Diagnostics.CodeAnalysis;
namespace InnovEnergy.Lib.Units.Composite;
public record Ac1Bus : AcPhase
public class Ac1Bus : AcPhase
{
public Frequency Frequency { get; init; }
public Double Frequency { get; internal init; }
// [SuppressMessage("ReSharper", "RedundantCast")]
public static Ac1Bus FromVoltageCurrentFrequencyPhi(Double voltageRms,
Double currentRms,
Double frequency,
Double phi) => new()
{
Frequency = frequency,
Current = currentRms,
Voltage = voltageRms,
Power = AcPower.FromVoltageCurrentPhi(voltageRms, currentRms, phi)
};
}
// [SuppressMessage("ReSharper", "RedundantCast")]
// public static Ac1Bus operator |(Ac1Bus left, Ac1Bus right)
// {
// var f = left.Frequency | right.Frequency;
@ -20,8 +34,3 @@ public record Ac1Bus : AcPhase
// Phi = p.Phi
// };
// }
}

View File

@ -1,19 +1,16 @@
using static DecimalMath.DecimalEx;
namespace InnovEnergy.Lib.Units.Composite;
#pragma warning disable CS8618
public record Ac3Bus
public class Ac3Bus
{
public AcPhase L1 { get; init; }
public AcPhase L2 { get; init; }
public AcPhase L3 { get; init; }
public Frequency Frequency { get; init; }
public AcPhase L1 { get; internal init; }
public AcPhase L2 { get; internal init; }
public AcPhase L3 { get; internal init; }
public ApparentPower ApparentPower => L1.ApparentPower + L2.ApparentPower + L3.ApparentPower;
public ReactivePower ReactivePower => L1.ReactivePower + L2.ReactivePower + L3.ReactivePower;
public Power ActivePower => L1.ActivePower + L2.ActivePower + L3.ActivePower;
public Angle Phi => ATan2(ReactivePower, ActivePower);
public AcPower Power { get; internal init; }
public Double Frequency { get; internal init; }
}

View File

@ -1,66 +1,8 @@
using static DecimalMath.DecimalEx;
namespace InnovEnergy.Lib.Units.Composite;
#pragma warning disable CS8618
public record AcPhase : IBus
public class AcPhase : Bus
{
private readonly Voltage _Voltage;
public Voltage Voltage
{
get => _Voltage;
init => _Voltage = value >= 0m ? value : throw new ArgumentException("RMS value cannot be negative");
}
private readonly Current _Current;
public Current Current
{
get => _Current;
init => _Current = value >= 0m ? value : throw new ArgumentException("RMS value cannot be negative");
}
public Angle Phi { get; init; }
public ApparentPower ApparentPower => Voltage.Value * Current.Value ;
public Power ActivePower => ApparentPower.Value * PowerFactor;
public ReactivePower ReactivePower => ApparentPower.Value * Sin(Phi);
public Decimal PowerFactor => Cos(Phi);
// public static AcPhase operator |(AcPhase left, AcPhase right)
// {
// // the Voltages of two phases are expected to be in phase and equal
//
// var v = left.Voltage | right.Voltage;
//
// // currents (RMS) can be different and out of phase
// // https://www.johndcook.com/blog/2020/08/17/adding-phase-shifted-sine-waves/
//
// // IF
// // left(t) = ILeft sin(ωt)
// // right(t) = IRight sin(ωt + φ).
// // sum(t) = left(t) + right(t) = ISum sin(ωt + ψ).
//
// // THEN
// // ψ = arctan( IRight * sin(φ) / (ILeft + IRight cos(φ)) ).
// // C = IRight * sin(φ) / sin(ψ).
//
// // in this calculation left(t) has zero phase shift.
// // we can shift both waves by -left.Phi, so
// // φ := right.phi - left.phi
//
//
// var phi = right.Phi - left.Phi;
// var phiSum = ATan2(right.Current * Sin(phi), left.Current + right.Current * Cos(phi));
// var iSum = right.Current * Sin(phi) / Sin(phiSum);
//
// return new AcPhase
// {
// Voltage = v,
// Current = iSum,
// Phi = phiSum
// };
// }
public AcPower Power { get; internal init; }
}

View File

@ -0,0 +1,34 @@
using static System.Math;
namespace InnovEnergy.Lib.Units.Composite;
public class AcPower
{
public Double Apparent { get; internal init; }
public Double Active { get; internal init; }
public Double Reactive { get; internal init; }
public Double Phi { get; internal init; }
public Double CosPhi { get; internal init; }
public static AcPower FromVoltageCurrentPhi(Double voltageRms, Double currentRms, Double phi)
{
if (voltageRms < 0) throw new ArgumentException("RMS value cannot be negative", nameof(voltageRms));
if (currentRms < 0) throw new ArgumentException("RMS value cannot be negative", nameof(currentRms));
phi = NormalizePhi(phi);
var cosPhi = Cos(phi);
var apparent = voltageRms * currentRms;
return new AcPower
{
Apparent = apparent,
Active = apparent * cosPhi,
Reactive = apparent * Sin(phi),
Phi = phi,
CosPhi = cosPhi
};
}
private static Double NormalizePhi(Double phi) => (phi + PI) % Tau - PI; // Tau is 2pi
}

View File

@ -4,8 +4,8 @@ namespace InnovEnergy.Lib.Units.Composite;
[SuppressMessage("ReSharper", "MemberCanBeProtected.Global")]
public interface IBus
public abstract class Bus
{
public Voltage Voltage { get; }
public Current Current { get; }
public Double Voltage { get; internal init; }
public Double Current { get; internal init; }
}

View File

@ -1,9 +1,13 @@
namespace InnovEnergy.Lib.Units.Composite;
public record DcBus : IBus
public class DcBus : Bus
{
public Voltage Voltage { get; init; }
public Current Current { get; init; }
public Double Power { get; internal init; }
public Power Power => Current * Voltage;
public static DcBus FromVoltageCurrent(Double voltage, Double current) => new()
{
Voltage = voltage,
Current = current,
Power = current * voltage,
};
}

View File

@ -8,7 +8,7 @@ public readonly partial struct Current
public static String Unit => "A";
public static String Symbol => "I";
public Current(Decimal value) => Value = value;
public Current(Double value) => Value = value;
// P=UI
public static Power operator *(Current current, Voltage voltage) => new Power(current.Value * voltage.Value);

View File

@ -15,14 +15,14 @@ using T = Current;
[JsonConverter(typeof(CurrentConverter))]
public readonly partial struct Current
{
public Decimal Value { get; }
public Double Value { get; }
public override String ToString() => Value.RoundToSignificantDigits(Units.DisplaySignificantDigits) + Unit;
// scalar multiplication
public static T operator *(Decimal scalar, T t) => new T(scalar * t.Value);
public static T operator *(T t, Decimal scalar) => new T(scalar * t.Value);
public static T operator /(T t, Decimal scalar) => new T(t.Value / scalar);
public static T operator *(Double scalar, T t) => new T(scalar * t.Value);
public static T operator *(T t, Double scalar) => new T(scalar * t.Value);
public static T operator /(T t, Double scalar) => new T(t.Value / scalar);
// addition
@ -41,10 +41,7 @@ public readonly partial struct Current
// conversion
public static implicit operator T(Decimal d) => new T(d);
public static implicit operator T(Double d) => new T((Decimal)d);
public static implicit operator T(Int32 i) => new T(i);
public static implicit operator Decimal(T t) => t.Value;
public static implicit operator T(Double d) => new T(d);
// equality
@ -59,7 +56,7 @@ internal class CurrentConverter : JsonConverter<Current>
{
public override Current Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return new Current(reader.GetDecimal());
return new Current(reader.GetDouble());
}
public override void Write(Utf8JsonWriter writer, Current value, JsonSerializerOptions options)

View File

@ -9,8 +9,8 @@ public readonly partial struct Energy
public static String Unit => "kWh";
public static String Symbol => "E";
public Energy(Decimal value) => Value = value;
public Energy(Double value) => Value = value;
public static Power operator /(Energy energy, TimeSpan timeSpan) => energy.Value * 1000m / (Decimal) timeSpan.TotalHours ;
public static Power operator /(Energy energy, UnixTimeSpan timeSpan) => energy.Value * 3_600_000m / timeSpan.Ticks;
public static Power operator /(Energy energy, TimeSpan timeSpan) => energy.Value * 1000 / timeSpan.TotalHours ;
public static Power operator /(Energy energy, UnixTimeSpan timeSpan) => energy.Value * 3_600_000 / timeSpan.Ticks;
}

View File

@ -15,14 +15,14 @@ using T = Energy;
[JsonConverter(typeof(EnergyConverter))]
public readonly partial struct Energy
{
public Decimal Value { get; }
public Double Value { get; }
public override String ToString() => Value.RoundToSignificantDigits(Units.DisplaySignificantDigits) + Unit;
// scalar multiplication
public static T operator *(Decimal scalar, T t) => new T(scalar * t.Value);
public static T operator *(T t, Decimal scalar) => new T(scalar * t.Value);
public static T operator /(T t, Decimal scalar) => new T(t.Value / scalar);
public static T operator *(Double scalar, T t) => new T(scalar * t.Value);
public static T operator *(T t, Double scalar) => new T(scalar * t.Value);
public static T operator /(T t, Double scalar) => new T(t.Value / scalar);
// addition
@ -41,10 +41,7 @@ public readonly partial struct Energy
// conversion
public static implicit operator T(Decimal d) => new T(d);
public static implicit operator T(Double d) => new T((Decimal)d);
public static implicit operator T(Int32 i) => new T(i);
public static implicit operator Decimal(T t) => t.Value;
public static implicit operator T(Double d) => new T(d);
// equality
@ -59,7 +56,7 @@ internal class EnergyConverter : JsonConverter<Energy>
{
public override Energy Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return new Energy(reader.GetDecimal());
return new Energy(reader.GetDouble());
}
public override void Write(Utf8JsonWriter writer, Energy value, JsonSerializerOptions options)

View File

@ -9,7 +9,7 @@ public readonly partial struct Frequency
public static String Unit => "Hz";
public static String Symbol => "f";
public Frequency(Decimal value)
public Frequency(Double value)
{
if (value < 0)
throw new ArgumentException(nameof(Frequency) + " cannot be negative", nameof(value));

View File

@ -15,14 +15,14 @@ using T = Frequency;
[JsonConverter(typeof(FrequencyConverter))]
public readonly partial struct Frequency
{
public Decimal Value { get; }
public Double Value { get; }
public override String ToString() => Value.RoundToSignificantDigits(Units.DisplaySignificantDigits) + Unit;
// scalar multiplication
public static T operator *(Decimal scalar, T t) => new T(scalar * t.Value);
public static T operator *(T t, Decimal scalar) => new T(scalar * t.Value);
public static T operator /(T t, Decimal scalar) => new T(t.Value / scalar);
public static T operator *(Double scalar, T t) => new T(scalar * t.Value);
public static T operator *(T t, Double scalar) => new T(scalar * t.Value);
public static T operator /(T t, Double scalar) => new T(t.Value / scalar);
// addition
@ -41,10 +41,7 @@ public readonly partial struct Frequency
// conversion
public static implicit operator T(Decimal d) => new T(d);
public static implicit operator T(Double d) => new T((Decimal)d);
public static implicit operator T(Int32 i) => new T(i);
public static implicit operator Decimal(T t) => t.Value;
public static implicit operator T(Double d) => new T(d);
// equality
@ -59,7 +56,7 @@ internal class FrequencyConverter : JsonConverter<Frequency>
{
public override Frequency Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return new Frequency(reader.GetDecimal());
return new Frequency(reader.GetDouble());
}
public override void Write(Utf8JsonWriter writer, Frequency value, JsonSerializerOptions options)

View File

@ -15,14 +15,14 @@ using T = Template;
[JsonConverter(typeof(TemplateConverter))]
public readonly partial struct Template
{
public Decimal Value { get; }
public Double Value { get; }
public override String ToString() => Value.RoundToSignificantDigits(Units.DisplaySignificantDigits) + Unit;
// scalar multiplication
public static T operator *(Decimal scalar, T t) => new T(scalar * t.Value);
public static T operator *(T t, Decimal scalar) => new T(scalar * t.Value);
public static T operator /(T t, Decimal scalar) => new T(t.Value / scalar);
public static T operator *(Double scalar, T t) => new T(scalar * t.Value);
public static T operator *(T t, Double scalar) => new T(scalar * t.Value);
public static T operator /(T t, Double scalar) => new T(t.Value / scalar);
// addition
@ -41,10 +41,7 @@ public readonly partial struct Template
// conversion
public static implicit operator T(Decimal d) => new T(d);
public static implicit operator T(Double d) => new T((Decimal)d);
public static implicit operator T(Int32 i) => new T(i);
public static implicit operator Decimal(T t) => t.Value;
public static implicit operator T(Double d) => new T(d);
// equality
@ -59,7 +56,7 @@ internal class TemplateConverter : JsonConverter<Template>
{
public override Template Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return new Template(reader.GetDecimal());
return new Template(reader.GetDouble());
}
public override void Write(Utf8JsonWriter writer, Template value, JsonSerializerOptions options)

View File

@ -8,5 +8,5 @@ public readonly partial struct Percent
public static String Unit => "%";
public static String Symbol => "%"; // ??
public Percent(Decimal value) => Value = value;
public Percent(Double value) => Value = value;
}

View File

@ -15,14 +15,14 @@ using T = Percent;
[JsonConverter(typeof(PercentConverter))]
public readonly partial struct Percent
{
public Decimal Value { get; }
public Double Value { get; }
public override String ToString() => Value.RoundToSignificantDigits(Units.DisplaySignificantDigits) + Unit;
// scalar multiplication
public static T operator *(Decimal scalar, T t) => new T(scalar * t.Value);
public static T operator *(T t, Decimal scalar) => new T(scalar * t.Value);
public static T operator /(T t, Decimal scalar) => new T(t.Value / scalar);
public static T operator *(Double scalar, T t) => new T(scalar * t.Value);
public static T operator *(T t, Double scalar) => new T(scalar * t.Value);
public static T operator /(T t, Double scalar) => new T(t.Value / scalar);
// addition
@ -41,10 +41,7 @@ public readonly partial struct Percent
// conversion
public static implicit operator T(Decimal d) => new T(d);
public static implicit operator T(Double d) => new T((Decimal)d);
public static implicit operator T(Int32 i) => new T(i);
public static implicit operator Decimal(T t) => t.Value;
public static implicit operator T(Double d) => new T(d);
// equality
@ -59,7 +56,7 @@ internal class PercentConverter : JsonConverter<Percent>
{
public override Percent Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return new Percent(reader.GetDecimal());
return new Percent(reader.GetDouble());
}
public override void Write(Utf8JsonWriter writer, Percent value, JsonSerializerOptions options)

View File

@ -9,14 +9,14 @@ public readonly partial struct Power
public static String Unit => "W";
public static String Symbol => "P";
public Power(Decimal value) => Value = value;
public Power(Double value) => Value = value;
// P=UI
public static Voltage operator /(Power power, Current current) => new Voltage(power.Value / current.Value);
public static Current operator /(Power power, Voltage voltage) => new Current(power.Value / voltage.Value);
public static Energy operator *(Power power, TimeSpan timeSpan) => power.Value / 1000m * (Decimal) timeSpan.TotalHours;
public static Energy operator *(TimeSpan timeSpan, Power power) => power.Value / 1000m * (Decimal) timeSpan.TotalHours;
public static Energy operator *(Power power, TimeSpan timeSpan) => power.Value / 1000 * timeSpan.TotalHours;
public static Energy operator *(TimeSpan timeSpan, Power power) => power.Value / 1000 * timeSpan.TotalHours;
public static Energy operator *(Power power, UnixTimeSpan timeSpan) => power.Value * timeSpan.Ticks / 3_600_000;
public static Energy operator *(UnixTimeSpan timeSpan, Power power) => power.Value * timeSpan.Ticks / 3_600_000;

View File

@ -15,14 +15,14 @@ using T = Power;
[JsonConverter(typeof(PowerConverter))]
public readonly partial struct Power
{
public Decimal Value { get; }
public Double Value { get; }
public override String ToString() => Value.RoundToSignificantDigits(Units.DisplaySignificantDigits) + Unit;
// scalar multiplication
public static T operator *(Decimal scalar, T t) => new T(scalar * t.Value);
public static T operator *(T t, Decimal scalar) => new T(scalar * t.Value);
public static T operator /(T t, Decimal scalar) => new T(t.Value / scalar);
public static T operator *(Double scalar, T t) => new T(scalar * t.Value);
public static T operator *(T t, Double scalar) => new T(scalar * t.Value);
public static T operator /(T t, Double scalar) => new T(t.Value / scalar);
// addition
@ -41,10 +41,7 @@ public readonly partial struct Power
// conversion
public static implicit operator T(Decimal d) => new T(d);
public static implicit operator T(Double d) => new T((Decimal)d);
public static implicit operator T(Int32 i) => new T(i);
public static implicit operator Decimal(T t) => t.Value;
public static implicit operator T(Double d) => new T(d);
// equality
@ -59,7 +56,7 @@ internal class PowerConverter : JsonConverter<Power>
{
public override Power Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return new Power(reader.GetDecimal());
return new Power(reader.GetDouble());
}
public override void Write(Utf8JsonWriter writer, Power value, JsonSerializerOptions options)

View File

@ -9,5 +9,5 @@ public readonly partial struct ReactivePower
public static String Unit => "var";
public static String Symbol => "Q";
public ReactivePower(Decimal value) => Value = value;
public ReactivePower(Double value) => Value = value;
}

View File

@ -15,14 +15,14 @@ using T = ReactivePower;
[JsonConverter(typeof(ReactivePowerConverter))]
public readonly partial struct ReactivePower
{
public Decimal Value { get; }
public Double Value { get; }
public override String ToString() => Value.RoundToSignificantDigits(Units.DisplaySignificantDigits) + Unit;
// scalar multiplication
public static T operator *(Decimal scalar, T t) => new T(scalar * t.Value);
public static T operator *(T t, Decimal scalar) => new T(scalar * t.Value);
public static T operator /(T t, Decimal scalar) => new T(t.Value / scalar);
public static T operator *(Double scalar, T t) => new T(scalar * t.Value);
public static T operator *(T t, Double scalar) => new T(scalar * t.Value);
public static T operator /(T t, Double scalar) => new T(t.Value / scalar);
// addition
@ -41,10 +41,7 @@ public readonly partial struct ReactivePower
// conversion
public static implicit operator T(Decimal d) => new T(d);
public static implicit operator T(Double d) => new T((Decimal)d);
public static implicit operator T(Int32 i) => new T(i);
public static implicit operator Decimal(T t) => t.Value;
public static implicit operator T(Double d) => new T(d);
// equality
@ -59,7 +56,7 @@ internal class ReactivePowerConverter : JsonConverter<ReactivePower>
{
public override ReactivePower Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return new ReactivePower(reader.GetDecimal());
return new ReactivePower(reader.GetDouble());
}
public override void Write(Utf8JsonWriter writer, ReactivePower value, JsonSerializerOptions options)

View File

@ -11,7 +11,7 @@ public readonly partial struct Resistance
public static String Symbol => "R";
public Resistance(Decimal value) => Value = value;
public Resistance(Double value) => Value = value;
// U=RI
public static Voltage operator *(Resistance resistance, Current current) => new Voltage(resistance.Value * current.Value);

View File

@ -15,14 +15,14 @@ using T = Resistance;
[JsonConverter(typeof(ResistanceConverter))]
public readonly partial struct Resistance
{
public Decimal Value { get; }
public Double Value { get; }
public override String ToString() => Value.RoundToSignificantDigits(Units.DisplaySignificantDigits) + Unit;
// scalar multiplication
public static T operator *(Decimal scalar, T t) => new T(scalar * t.Value);
public static T operator *(T t, Decimal scalar) => new T(scalar * t.Value);
public static T operator /(T t, Decimal scalar) => new T(t.Value / scalar);
public static T operator *(Double scalar, T t) => new T(scalar * t.Value);
public static T operator *(T t, Double scalar) => new T(scalar * t.Value);
public static T operator /(T t, Double scalar) => new T(t.Value / scalar);
// addition
@ -41,10 +41,7 @@ public readonly partial struct Resistance
// conversion
public static implicit operator T(Decimal d) => new T(d);
public static implicit operator T(Double d) => new T((Decimal)d);
public static implicit operator T(Int32 i) => new T(i);
public static implicit operator Decimal(T t) => t.Value;
public static implicit operator T(Double d) => new T(d);
// equality
@ -59,7 +56,7 @@ internal class ResistanceConverter : JsonConverter<Resistance>
{
public override Resistance Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return new Resistance(reader.GetDecimal());
return new Resistance(reader.GetDouble());
}
public override void Write(Utf8JsonWriter writer, Resistance value, JsonSerializerOptions options)

View File

@ -11,5 +11,5 @@ public readonly partial struct Temperature
public static String Unit => "°C";
public static String Symbol => "T";
public Temperature(Decimal value) => Value = value;
public Temperature(Double value) => Value = value;
}

View File

@ -15,14 +15,14 @@ using T = Temperature;
[JsonConverter(typeof(TemperatureConverter))]
public readonly partial struct Temperature
{
public Decimal Value { get; }
public Double Value { get; }
public override String ToString() => Value.RoundToSignificantDigits(Units.DisplaySignificantDigits) + Unit;
// scalar multiplication
public static T operator *(Decimal scalar, T t) => new T(scalar * t.Value);
public static T operator *(T t, Decimal scalar) => new T(scalar * t.Value);
public static T operator /(T t, Decimal scalar) => new T(t.Value / scalar);
public static T operator *(Double scalar, T t) => new T(scalar * t.Value);
public static T operator *(T t, Double scalar) => new T(scalar * t.Value);
public static T operator /(T t, Double scalar) => new T(t.Value / scalar);
// addition
@ -41,10 +41,7 @@ public readonly partial struct Temperature
// conversion
public static implicit operator T(Decimal d) => new T(d);
public static implicit operator T(Double d) => new T((Decimal)d);
public static implicit operator T(Int32 i) => new T(i);
public static implicit operator Decimal(T t) => t.Value;
public static implicit operator T(Double d) => new T(d);
// equality
@ -59,7 +56,7 @@ internal class TemperatureConverter : JsonConverter<Temperature>
{
public override Temperature Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return new Temperature(reader.GetDecimal());
return new Temperature(reader.GetDouble());
}
public override void Write(Utf8JsonWriter writer, Temperature value, JsonSerializerOptions options)

View File

@ -5,17 +5,16 @@ public static class Units
public static Byte DisplaySignificantDigits { get; set; } = 3;
public static Byte JsonSignificantDigits { get; set; } = 3;
public static Current A (this Decimal value) => value;
public static Voltage V (this Decimal value) => value;
public static Power W (this Decimal value) => value;
public static ReactivePower Var (this Decimal value) => value;
public static ApparentPower Va (this Decimal value) => value;
public static Resistance Ohm (this Decimal value) => value;
public static Frequency Hz (this Decimal value) => value;
public static Angle Rad (this Decimal value) => value;
public static Temperature Celsius(this Decimal value) => value;
public static Energy KWh (this Decimal value) => value;
public static Percent Percent(this Decimal value) => value;
public static Current A (this Double value) => value;
public static Voltage V (this Double value) => value;
public static Power W (this Double value) => value;
public static ReactivePower Var (this Double value) => value;
public static ApparentPower Va (this Double value) => value;
public static Resistance Ohm (this Double value) => value;
public static Frequency Hz (this Double value) => value;
public static Angle Rad (this Double value) => value;
public static Temperature Celsius(this Double value) => value;
public static Energy KWh (this Double value) => value;
}
public static class Prefixes

View File

@ -9,7 +9,7 @@ public readonly partial struct Voltage
public static String Unit => "V";
public static String Symbol => "U";
public Voltage(Decimal value) => Value = value;
public Voltage(Double value) => Value = value;
// U=RI

View File

@ -15,14 +15,14 @@ using T = Voltage;
[JsonConverter(typeof(VoltageConverter))]
public readonly partial struct Voltage
{
public Decimal Value { get; }
public Double Value { get; }
public override String ToString() => Value.RoundToSignificantDigits(Units.DisplaySignificantDigits) + Unit;
// scalar multiplication
public static T operator *(Decimal scalar, T t) => new T(scalar * t.Value);
public static T operator *(T t, Decimal scalar) => new T(scalar * t.Value);
public static T operator /(T t, Decimal scalar) => new T(t.Value / scalar);
public static T operator *(Double scalar, T t) => new T(scalar * t.Value);
public static T operator *(T t, Double scalar) => new T(scalar * t.Value);
public static T operator /(T t, Double scalar) => new T(t.Value / scalar);
// addition
@ -41,10 +41,7 @@ public readonly partial struct Voltage
// conversion
public static implicit operator T(Decimal d) => new T(d);
public static implicit operator T(Double d) => new T((Decimal)d);
public static implicit operator T(Int32 i) => new T(i);
public static implicit operator Decimal(T t) => t.Value;
public static implicit operator T(Double d) => new T(d);
// equality
@ -59,7 +56,7 @@ internal class VoltageConverter : JsonConverter<Voltage>
{
public override Voltage Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return new Voltage(reader.GetDecimal());
return new Voltage(reader.GetDouble());
}
public override void Write(Utf8JsonWriter writer, Voltage value, JsonSerializerOptions options)