Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert Power to double #1195

Merged
merged 5 commits into from
Feb 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 14 additions & 15 deletions Common/UnitDefinitions/Power.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"Name": "Power",
"BaseUnit": "Watt",
"ValueType": "decimal",
"XmlDocSummary": "In physics, power is the rate of doing work. It is equivalent to an amount of energy consumed per unit time.",
"BaseDimensions": {
"L": 2,
Expand Down Expand Up @@ -30,8 +29,8 @@
{
"SingularName": "MechanicalHorsepower",
"PluralName": "MechanicalHorsepower",
"FromUnitToBaseFunc": "{x} * 745.69m",
"FromBaseToUnitFunc": "{x} / 745.69m",
"FromUnitToBaseFunc": "{x} * 745.69",
"FromBaseToUnitFunc": "{x} / 745.69",
"Localization": [
{
"Culture": "en-US",
Expand All @@ -42,8 +41,8 @@
{
"SingularName": "MetricHorsepower",
"PluralName": "MetricHorsepower",
"FromUnitToBaseFunc": "{x} * 735.49875m",
"FromBaseToUnitFunc": "{x} / 735.49875m",
"FromUnitToBaseFunc": "{x} * 735.49875",
"FromBaseToUnitFunc": "{x} / 735.49875",
"Localization": [
{
"Culture": "en-US",
Expand All @@ -54,8 +53,8 @@
{
"SingularName": "ElectricalHorsepower",
"PluralName": "ElectricalHorsepower",
"FromUnitToBaseFunc": "{x} * 746m",
"FromBaseToUnitFunc": "{x} / 746m",
"FromUnitToBaseFunc": "{x} * 746",
"FromBaseToUnitFunc": "{x} / 746",
"Localization": [
{
"Culture": "en-US",
Expand All @@ -66,8 +65,8 @@
{
"SingularName": "BoilerHorsepower",
"PluralName": "BoilerHorsepower",
"FromUnitToBaseFunc": "{x} * 9812.5m",
"FromBaseToUnitFunc": "{x} / 9812.5m",
"FromUnitToBaseFunc": "{x} * 9812.5",
"FromBaseToUnitFunc": "{x} / 9812.5",
"Localization": [
{
"Culture": "en-US",
Expand All @@ -78,8 +77,8 @@
{
"SingularName": "HydraulicHorsepower",
"PluralName": "HydraulicHorsepower",
"FromUnitToBaseFunc": "{x} * 745.69988145m",
"FromBaseToUnitFunc": "{x} / 745.69988145m",
"FromUnitToBaseFunc": "{x} * 745.69988145",
"FromBaseToUnitFunc": "{x} / 745.69988145",
"Localization": [
{
"Culture": "en-US",
Expand All @@ -90,8 +89,8 @@
{
"SingularName": "BritishThermalUnitPerHour",
"PluralName": "BritishThermalUnitsPerHour",
"FromUnitToBaseFunc": "{x} * 0.29307107017m",
"FromBaseToUnitFunc": "{x} / 0.29307107017m",
"FromUnitToBaseFunc": "{x} * 0.29307107017",
"FromBaseToUnitFunc": "{x} / 0.29307107017",
"Prefixes": [ "Kilo", "Mega" ],
"Localization": [
{
Expand All @@ -103,8 +102,8 @@
{
"SingularName": "JoulePerHour",
"PluralName": "JoulesPerHour",
"FromUnitToBaseFunc": "{x} / 3600m",
"FromBaseToUnitFunc": "{x} * 3600m",
"FromUnitToBaseFunc": "{x} / 3600",
"FromBaseToUnitFunc": "{x} * 3600",
"Prefixes": [ "Milli", "Kilo", "Mega", "Giga" ],
"Localization": [
{
Expand Down
56 changes: 28 additions & 28 deletions UnitsNet.NanoFramework/GeneratedCode/Quantities/Power.g.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ public void UnitsNetBaseJsonConverter_ConvertIQuantity_works_with_double_type()
[Fact]
public void UnitsNetBaseJsonConverter_ConvertIQuantity_works_with_decimal_type()
{
var result = _sut.Test_ConvertDecimalIQuantity(Power.FromWatts(10.2365m));
var result = _sut.Test_ConvertDecimalIQuantity(Information.FromBits(64m));

Assert.Equal("PowerUnit.Watt", result.Unit);
Assert.Equal(10.2365m, result.Value);
Assert.Equal("InformationUnit.Bit", result.Unit);
Assert.Equal(64m, result.Value);
}

[Fact]
Expand All @@ -54,8 +54,7 @@ public void UnitsNetBaseJsonConverter_ConvertValueUnit_works_as_expected()

Assert.NotNull(result);
Assert.IsType<Power>(result);
Assert.True(Power.FromWatts(10.2365m).Equals((Power)result, 1E-5m, ComparisonType.Absolute));

Assert.True(Power.FromWatts(10.2365).Equals((Power)result, 1e-5, ComparisonType.Absolute));
angularsen marked this conversation as resolved.
Show resolved Hide resolved
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public void UnitsNetIComparableJsonConverter_ReadJson_works_as_expected()

Assert.NotNull(result);
Assert.IsType<Power>(result);
Assert.Equal(120M, ((Power)result).Watts);
Assert.Equal(120, ((Power)result).Watts);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ public void UnitsNetIQuantityJsonConverter_WriteJson_works_with_decimal_quantity
using (var stringWriter = new StringWriter(result))
using(var writer = new JsonTextWriter(stringWriter))
{
_sut.WriteJson(writer, Power.FromWatts(value), JsonSerializer.CreateDefault());
_sut.WriteJson(writer, Information.FromBits(value), JsonSerializer.CreateDefault());
}

Assert.Equal($"{{\"Unit\":\"PowerUnit.Watt\",\"Value\":{expectedValue},\"ValueString\":\"{expectedValueString}\",\"ValueType\":\"decimal\"}}",
Assert.Equal($"{{\"Unit\":\"InformationUnit.Bit\",\"Value\":{expectedValue},\"ValueString\":\"{expectedValueString}\",\"ValueType\":\"decimal\"}}",
result.ToString());
}

Expand Down Expand Up @@ -135,7 +135,7 @@ public void UnitsNetIQuantityJsonConverter_ReadJson_works_as_expected()

Assert.NotNull(result);
Assert.IsType<Power>(result);
Assert.Equal(10.3654M, ((Power)result).Watts);
Assert.Equal(10.3654, ((Power)result).Watts);
}
}
}
7 changes: 0 additions & 7 deletions UnitsNet.Tests/CustomCode/IQuantityTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,5 @@ public void IQuantityTUnitDecimal_AsEnum_ReturnsDecimal()
IQuantity<InformationUnit, decimal> decimalQuantity = Information.FromKilobytes(1234.5);
Assert.IsType<decimal>(decimalQuantity.As(InformationUnit.Byte));
}

[Fact]
public void IQuantityTUnitDecimal_AsUnitSystem_ReturnsDecimal()
{
IQuantity<PowerUnit, decimal> decimalQuantity = Power.FromMegawatts(1234.5);
Assert.IsType<decimal>(decimalQuantity.As(UnitSystem.SI));
}
}
}
2 changes: 1 addition & 1 deletion UnitsNet.Tests/CustomCode/MassFlowTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public void TimeSpanTimesMassFlowEqualsMass()
public void MassFlowDividedByBrakeSpecificFuelConsumptionEqualsPower()
{
Power power = MassFlow.FromTonnesPerDay(20) / BrakeSpecificFuelConsumption.FromGramsPerKiloWattHour(180.0);
AssertEx.EqualTolerance(20.0m / 24.0m * 1e6m / 180.0m, power.Kilowatts, 1E-11m);
AssertEx.EqualTolerance(20.0 / 24.0 * 1e6 / 180.0, power.Kilowatts, 1e-11);
}

[Fact]
Expand Down
4 changes: 2 additions & 2 deletions UnitsNet.Tests/CustomCode/PowerRatioTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ public void ExpectPowerConvertedCorrectly(double power, double expected)
public void ExpectPowerRatioConvertedCorrectly(double powerRatio, double expected)
{
PowerRatio pr = PowerRatio.FromDecibelWatts(powerRatio);
decimal actual = pr.ToPower().Watts;
Assert.Equal((decimal)expected, actual);
var actual = pr.ToPower().Watts;
Assert.Equal(expected, actual);
}

// http://www.maximintegrated.com/en/app-notes/index.mvp/id/808
Expand Down
53 changes: 27 additions & 26 deletions UnitsNet.Tests/CustomCode/PowerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,57 +9,58 @@ namespace UnitsNet.Tests
public class PowerTests : PowerTestsBase
{
protected override bool SupportsSIUnitSystem => true;
protected override decimal FemtowattsInOneWatt => 1e15m;

protected override decimal GigajoulesPerHourInOneWatt => 3600e-9m;
protected override double FemtowattsInOneWatt => 1e15;

protected override decimal PicowattsInOneWatt => 1e12m;
protected override double GigajoulesPerHourInOneWatt => 3600e-9;

protected override decimal NanowattsInOneWatt => 1e9m;
protected override double PicowattsInOneWatt => 1e12;

protected override decimal MicrowattsInOneWatt => 1e6m;
protected override double NanowattsInOneWatt => 1e9;

protected override decimal MillijoulesPerHourInOneWatt => 3600e3m;
protected override double MicrowattsInOneWatt => 1e6;

protected override decimal MilliwattsInOneWatt => 1e3m;
protected override double MillijoulesPerHourInOneWatt => 3600e3;

protected override decimal DeciwattsInOneWatt => 1e1m;
protected override double MilliwattsInOneWatt => 1e3;

protected override decimal WattsInOneWatt => 1;
protected override double DeciwattsInOneWatt => 1e1;

protected override decimal DecawattsInOneWatt => 1e-1m;
protected override double WattsInOneWatt => 1;

protected override decimal KilojoulesPerHourInOneWatt => 3600e-3m;
protected override double DecawattsInOneWatt => 1e-1;

protected override decimal KilowattsInOneWatt => 1e-3m;
protected override double KilojoulesPerHourInOneWatt => 3600e-3;

protected override decimal MegajoulesPerHourInOneWatt => 3600e-6m;
protected override double KilowattsInOneWatt => 1e-3;

protected override decimal MegawattsInOneWatt => 1e-6m;
protected override double MegajoulesPerHourInOneWatt => 3600e-6;

protected override decimal GigawattsInOneWatt => 1e-9m;
protected override double MegawattsInOneWatt => 1e-6;

protected override decimal TerawattsInOneWatt => 1e-12m;
protected override double GigawattsInOneWatt => 1e-9;

protected override decimal PetawattsInOneWatt => 1e-15m;
protected override double TerawattsInOneWatt => 1e-12;

protected override decimal JoulesPerHourInOneWatt => 3600;
protected override double PetawattsInOneWatt => 1e-15;

protected override decimal KilobritishThermalUnitsPerHourInOneWatt => 3.412141633e-3m;
protected override double JoulesPerHourInOneWatt => 3600;

protected override decimal BoilerHorsepowerInOneWatt => 1.0191082802547770700636942675159e-4m;
protected override double KilobritishThermalUnitsPerHourInOneWatt => 3.412141633e-3;

protected override decimal MegabritishThermalUnitsPerHourInOneWatt => 3.412141633e-6m;
protected override double BoilerHorsepowerInOneWatt => 1.0191082802547770700636942675159e-4;

protected override decimal BritishThermalUnitsPerHourInOneWatt => 3.412141633m;
protected override double MegabritishThermalUnitsPerHourInOneWatt => 3.412141633e-6;

protected override decimal ElectricalHorsepowerInOneWatt => 0.00134048257372654155495978552279m;
protected override double BritishThermalUnitsPerHourInOneWatt => 3.412141633;

protected override decimal HydraulicHorsepowerInOneWatt => 0.00134102207184949258114167291719m;
protected override double ElectricalHorsepowerInOneWatt => 0.00134048257372654155495978552279;

protected override decimal MechanicalHorsepowerInOneWatt => 0.00134103984229371454625916935992m;
protected override double HydraulicHorsepowerInOneWatt => 0.00134102207184949258114167291719;

protected override decimal MetricHorsepowerInOneWatt => 0.00135962161730390432342679032425m;
protected override double MechanicalHorsepowerInOneWatt => 0.00134103984229371454625916935992;

protected override double MetricHorsepowerInOneWatt => 0.00135962161730390432342679032425;

[Fact]
public void DurationTimesPowerEqualsEnergy()
Expand Down
2 changes: 1 addition & 1 deletion UnitsNet.Tests/CustomCode/SpecificEnergyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void DoubleDividedBySpecificEnergyEqualsBrakeSpecificFuelConsumption()
public void SpecificEnergyTimesMassFlowEqualsPower()
{
Power power = SpecificEnergy.FromJoulesPerKilogram(10.0) * MassFlow.FromKilogramsPerSecond(20.0);
Assert.Equal(200m, power.Watts);
Assert.Equal(200, power.Watts);
}

[Fact]
Expand Down
24 changes: 0 additions & 24 deletions UnitsNet.Tests/DecimalOverloadTests.cs

This file was deleted.

Loading