From 0d05cb75ee3c3d85481fe87ea287c97a439fe47a Mon Sep 17 00:00:00 2001 From: Sam Anthony Date: Sat, 1 Mar 2025 15:11:35 -0500 Subject: temperature conversion functions --- Makefile | 2 +- test.c | 9 +++++++++ test.h | 9 +++++++++ test_temperature.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ unit.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ unit.h | 14 ++++++++++++++ 6 files changed, 125 insertions(+), 1 deletion(-) create mode 100644 test_temperature.c diff --git a/Makefile b/Makefile index c2a54c5..3aa9131 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ SRC = main.c microui.c renderer.c widget.c ui.c unit.c engine.c OBJ = ${SRC:.c=.o} HDR = microui.h renderer.h widget.h ui.h unit.h engine.h -TEST_SRC = test.c test_angular_speed.c test_pressure.c test_volume.c test_volume_flow_rate.c test_fraction.c unit.c +TEST_SRC = test.c test_angular_speed.c test_pressure.c test_temperature.c test_volume.c test_volume_flow_rate.c test_fraction.c unit.c TEST_OBJ = ${TEST_SRC:.c=.o} volute: ${OBJ} diff --git a/test.c b/test.c index 5421adf..48df56e 100644 --- a/test.c +++ b/test.c @@ -23,6 +23,15 @@ main(void) { test_as_bar(); test_as_psi(); + test_kelvin(); + test_celsius(); + test_fahrenheit(); + test_rankine(); + test_as_kelvin(); + test_as_celsius(); + test_as_fahrenheit(); + test_as_rankine(); + test_cubic_centimetre(); test_litre(); test_cubic_metre(); diff --git a/test.h b/test.h index 150443c..4504b00 100644 --- a/test.h +++ b/test.h @@ -29,6 +29,15 @@ void test_as_kilopascal(void); void test_as_bar(void); void test_as_psi(void); +void test_kelvin(void); +void test_celsius(void); +void test_fahrenheit(void); +void test_rankine(void); +void test_as_kelvin(void); +void test_as_celsius(void); +void test_as_fahrenheit(void); +void test_as_rankine(void); + void test_cubic_centimetre(void); void test_litre(void); void test_cubic_metre(void); diff --git a/test_temperature.c b/test_temperature.c new file mode 100644 index 0000000..e7a6e17 --- /dev/null +++ b/test_temperature.c @@ -0,0 +1,45 @@ +#include +#include + +#include "test.h" +#include "unit.h" + +void +test_kelvin(void) { + test(kelvin(123.456), 123.456); +} + +void +test_celsius(void) { + test(celsius(123.456), 396.606); +} + +void +test_fahrenheit(void) { + test(fahrenheit(123.456), 323.9588889); +} + +void +test_rankine(void) { + test(rankine(123.456), 68.5866667); +} + +void +test_as_kelvin(void) { + test(as_kelvin(kelvin(123.456)), 123.456); +} + +void +test_as_celsius(void) { + test(as_celsius(celsius(123.456)), 123.456); +} + +void +test_as_fahrenheit(void) { + test(as_fahrenheit(fahrenheit(123.456)), 123.456); +} + +void +test_as_rankine(void) { + test(as_rankine(rankine(123.456)), 123.456); +} diff --git a/unit.c b/unit.c index de24dc3..d1e487d 100644 --- a/unit.c +++ b/unit.c @@ -18,6 +18,9 @@ /* Seconds per minute. */ #define SEC_PER_MIN 60.0 +/* Zero Celsius in Kelvin. */ +#define ZERO_C 273.15 + AngularSpeed rad_per_sec(double x) { @@ -108,6 +111,50 @@ as_psi(Pressure x) { } +Temperature +kelvin(double x) { + return x; +} + +Temperature +celsius(double x) { + return x + ZERO_C; +} + +Temperature +fahrenheit(double x) { + double c; + + c = (x - 32.0) * 5.0 / 9.0; + return c + ZERO_C; +} + +Temperature +rankine(double x) { + return x * 5.0 / 9.0; +} + +double +as_kelvin(Temperature t) { + return t; +} + +double +as_celsius(Temperature t) { + return t - ZERO_C; +} + +double +as_fahrenheit(Temperature t) { + return as_celsius(t) * 9.0 / 5.0 + 32.0; +} + +double +as_rankine(Temperature t) { + return t * 9.0 / 5.0; +} + + Volume cubic_centimetre(double x) { return x * 1e-6; diff --git a/unit.h b/unit.h index 5febcd1..4e22a35 100644 --- a/unit.h +++ b/unit.h @@ -34,6 +34,20 @@ double as_bar(Pressure x); double as_psi(Pressure x); +typedef double Temperature; +typedef Temperature (*TemperatureMaker)(double); +typedef double (*TemperatureReader)(Temperature); + +Temperature kelvin(double x); +Temperature celsius(double x); +Temperature fahrenheit(double x); +Temperature rankine(double x); +double as_kelvin(Temperature t); +double as_celsius(Temperature t); +double as_fahrenheit(Temperature t); +double as_rankine(Temperature t); + + typedef double Volume; typedef Volume (*VolumeMaker)(double); typedef double (*VolumeReader)(Volume); -- cgit v1.2.3