diff options
| author | Sam Anthony <sam@samanthony.xyz> | 2025-03-01 15:11:35 -0500 |
|---|---|---|
| committer | Sam Anthony <sam@samanthony.xyz> | 2025-03-01 15:11:35 -0500 |
| commit | 0d05cb75ee3c3d85481fe87ea287c97a439fe47a (patch) | |
| tree | 8603e4ac9d3a58715690643862d27ed769347b8d | |
| parent | 47874c2ec00339314e43d282ab2bc906e9c9ac9b (diff) | |
| download | volute-0d05cb75ee3c3d85481fe87ea287c97a439fe47a.zip | |
temperature conversion functions
| -rw-r--r-- | Makefile | 2 | ||||
| -rw-r--r-- | test.c | 9 | ||||
| -rw-r--r-- | test.h | 9 | ||||
| -rw-r--r-- | test_temperature.c | 45 | ||||
| -rw-r--r-- | unit.c | 47 | ||||
| -rw-r--r-- | unit.h | 14 |
6 files changed, 125 insertions, 1 deletions
@@ -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} @@ -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(); @@ -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 <assert.h> +#include <stdio.h> + +#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); +} @@ -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; @@ -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); |