Skip to content

Commit

Permalink
fix bugs in temperature conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
iTitus committed Oct 18, 2021
1 parent 5ed2ad2 commit 2fed7c8
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.ititus.si.dimension;

import io.github.ititus.data.Lazy;
import io.github.ititus.internal.Config;

import java.util.Collections;
import java.util.EnumMap;
Expand All @@ -12,7 +13,7 @@ public enum BaseDimension implements Dimension {
MASS('M'),
TIME('T'),
ELECTRIC_CURRENT('I'),
THERMODYNAMIC_TEMPERATURE('\u0398'),
TEMPERATURE(Config.FORCE_ASCII ? 'O' : '\u0398'),
SUBSTANCE_AMOUNT('N'),
LUMINOUS_INTENSITY('J');

Expand Down
14 changes: 14 additions & 0 deletions src/main/java/io/github/ititus/si/quantity/type/Temperature.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.github.ititus.si.quantity.type;

import io.github.ititus.si.dimension.BaseDimension;

import static io.github.ititus.si.unit.Units.KELVIN;

public final class Temperature extends AbstractQuantityType<Temperature> {

public static final Temperature TEMPERATURE = new Temperature();

private Temperature() {
super(BaseDimension.TEMPERATURE, () -> KELVIN);
}
}

This file was deleted.

10 changes: 5 additions & 5 deletions src/main/java/io/github/ititus/si/unit/Units.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
import static io.github.ititus.si.quantity.type.SolidAngle.SOLID_ANGLE;
import static io.github.ititus.si.quantity.type.Speed.SPEED;
import static io.github.ititus.si.quantity.type.SubstanceAmount.SUBSTANCE_AMOUNT;
import static io.github.ititus.si.quantity.type.ThermodynamicTemperature.THERMODYNAMIC_TEMPERATURE;
import static io.github.ititus.si.quantity.type.Temperature.TEMPERATURE;
import static io.github.ititus.si.quantity.type.Time.TIME;
import static io.github.ititus.si.quantity.type.Volume.VOLUME;

Expand All @@ -59,7 +59,7 @@ public final class Units {
public static final Unit<Mass> GRAM = new BaseUnit<>(MASS, "g");
public static final Unit<Time> SECOND = new BaseUnit<>(TIME, "s");
public static final Unit<ElectricCurrent> AMPERE = new BaseUnit<>(ELECTRIC_CURRENT, "A");
public static final Unit<ThermodynamicTemperature> KELVIN = new BaseUnit<>(THERMODYNAMIC_TEMPERATURE, "K");
public static final Unit<Temperature> KELVIN = new BaseUnit<>(TEMPERATURE, "K");
public static final Unit<SubstanceAmount> MOLE = new BaseUnit<>(SUBSTANCE_AMOUNT, "mol");
public static final Unit<LuminousIntensity> CANDELA = new BaseUnit<>(LUMINOUS_INTENSITY, "cd");

Expand Down Expand Up @@ -103,9 +103,9 @@ public final class Units {
// Electric Current

// Thermodynamic Temperature
public static final Unit<ThermodynamicTemperature> CELSIUS = KELVIN.shift(ofExp(-23715, -2)).alternate("°C");
public static final Unit<ThermodynamicTemperature> RANKINE = KELVIN.multiply(of(9, 5)).alternate("°R");
public static final Unit<ThermodynamicTemperature> FAHRENHEIT = RANKINE.shift(ofExp(-45967, -2)).alternate("°F");
public static final Unit<Temperature> CELSIUS = KELVIN.shift(ofExp(27315, -2)).alternate("°C");
public static final Unit<Temperature> RANKINE = KELVIN.multiply(of(5, 9)).alternate("°R");
public static final Unit<Temperature> FAHRENHEIT = RANKINE.shift(ofExp(45967, -2)).alternate("°F");

// Amount of Substance

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public UnitConverter concat(UnitConverter converter) {
return this;
} else if (converter instanceof LinearConverter) {
LinearConverter that = (LinearConverter) converter;
return of(factor.multiply(that.factor), that.factor.multiply(shift).add(this.shift));
return of(factor.multiply(that.factor), that.factor.multiply(shift).add(that.shift));
}

return UnitConverter.compound(List.of(this, converter));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,56 @@

import static io.github.ititus.assertions.Assertions.assertThat;
import static io.github.ititus.si.quantity.value.QuantityValue.of;
import static io.github.ititus.si.unit.Units.CELSIUS;
import static io.github.ititus.si.unit.Units.KELVIN;
import static io.github.ititus.si.unit.Units.*;
import static org.assertj.core.data.Percentage.withPercentage;

class TemperatureTest {

@Test
void kelvinToCelsius() {
Quantity<ThermodynamicTemperature> kelvin = KELVIN.get(0);
Quantity<ThermodynamicTemperature> celsius = kelvin.convertTo(CELSIUS);
void fromKelvin() {
Quantity<Temperature> kelvin = KELVIN.get(42);
Quantity<Temperature> celsius = kelvin.convertTo(CELSIUS);
Quantity<Temperature> rankine = kelvin.convertTo(RANKINE);
Quantity<Temperature> fahrenheit = kelvin.convertTo(FAHRENHEIT);

assertThat(celsius.getValue()).isCloseTo(of(-273.15), withPercentage(1.0));
assertThat(celsius.getValue()).isCloseTo(of(-231.15), withPercentage(1.0));
assertThat(rankine.getValue()).isCloseTo(of(75.6), withPercentage(1.0));
assertThat(fahrenheit.getValue()).isCloseTo(of(-384.07), withPercentage(1.0));
}

@Test
void celsiusToKelvin() {
Quantity<ThermodynamicTemperature> celsius = CELSIUS.get(0);
Quantity<ThermodynamicTemperature> kelvin = celsius.convertTo(KELVIN);
void fromCelsius() {
Quantity<Temperature> celsius = CELSIUS.get(42);
Quantity<Temperature> kelvin = celsius.convertTo(KELVIN);
Quantity<Temperature> rankine = celsius.convertTo(RANKINE);
Quantity<Temperature> fahrenheit = celsius.convertTo(FAHRENHEIT);

assertThat(kelvin.getValue()).isCloseTo(of(273.15), withPercentage(1.0));
assertThat(kelvin.getValue()).isCloseTo(of(315.15), withPercentage(1.0));
assertThat(rankine.getValue()).isCloseTo(of(567.27), withPercentage(1.0));
assertThat(fahrenheit.getValue()).isCloseTo(of(107.6), withPercentage(1.0));
}

@Test
void fromRankine() {
Quantity<Temperature> rankine = RANKINE.get(42);
Quantity<Temperature> kelvin = rankine.convertTo(KELVIN);
Quantity<Temperature> celsius = rankine.convertTo(CELSIUS);
Quantity<Temperature> fahrenheit = rankine.convertTo(FAHRENHEIT);

assertThat(kelvin.getValue()).isCloseTo(of(23.33), withPercentage(1.0));
assertThat(celsius.getValue()).isCloseTo(of(-249.82), withPercentage(1.0));
assertThat(fahrenheit.getValue()).isCloseTo(of(-417.67), withPercentage(1.0));
}

@Test
void fromFahrenheit() {
Quantity<Temperature> fahrenheit = FAHRENHEIT.get(42);
Quantity<Temperature> kelvin = fahrenheit.convertTo(KELVIN);
Quantity<Temperature> celsius = fahrenheit.convertTo(CELSIUS);
Quantity<Temperature> rankine = fahrenheit.convertTo(RANKINE);

assertThat(kelvin.getValue()).isCloseTo(of(278.71), withPercentage(1.0));
assertThat(celsius.getValue()).isCloseTo(of(5.56), withPercentage(1.0));
assertThat(rankine.getValue()).isCloseTo(of(501.67), withPercentage(1.0));
}
}
31 changes: 31 additions & 0 deletions src/test/java/io/github/ititus/si/quantity/type/TimeTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package io.github.ititus.si.quantity.type;

import io.github.ititus.si.quantity.Quantity;
import org.junit.jupiter.api.Test;

import static io.github.ititus.assertions.Assertions.assertThat;
import static io.github.ititus.math.number.BigRational.ofExp;
import static io.github.ititus.si.prefix.MetricPrefix.NANO;
import static io.github.ititus.si.quantity.value.QuantityValue.of;
import static io.github.ititus.si.unit.Units.*;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.data.Percentage.withPercentage;

class TimeTest {

@Test
void secondsToMinutes() {
Quantity<Time> seconds = SECOND.get(42);
Quantity<Time> minutes = seconds.convertTo(MINUTE);

assertThat(minutes.getValue()).isCloseTo(of(0.7), withPercentage(1));
}

@Test
void minutesToSeconds() {
Quantity<Time> minutes = MINUTE.get(42);
Quantity<Time> seconds = minutes.convertTo(SECOND);

assertThat(seconds.getValue()).isCloseTo(of(2520), withPercentage(1));
}
}

0 comments on commit 2fed7c8

Please sign in to comment.