From 942a6711361c371da0736cc62f0e3e2ed13f341b Mon Sep 17 00:00:00 2001 From: Sam Anthony Date: Fri, 28 Feb 2025 21:01:26 -0500 Subject: fraction/percent conversion functions --- Makefile | 2 +- test.c | 3 +++ test.h | 3 +++ test_fraction.c | 15 +++++++++++++++ unit.c | 12 ++++++++++-- 5 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 test_fraction.c diff --git a/Makefile b/Makefile index 8f0bbe1..c2a54c5 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 unit.c +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_OBJ = ${TEST_SRC:.c=.o} volute: ${OBJ} diff --git a/test.c b/test.c index 440ab40..5421adf 100644 --- a/test.c +++ b/test.c @@ -9,6 +9,9 @@ main(void) { test_as_deg_per_sec(); test_as_rpm(); + test_percent(); + test_as_percent(); + test_pascal(); test_millibar(); test_kilopascal(); diff --git a/test.h b/test.h index cc91c7e..ef3e9cf 100644 --- a/test.h +++ b/test.h @@ -15,6 +15,9 @@ void test_as_rad_per_sec(void); void test_as_deg_per_sec(void); void test_as_rpm(void); +void test_percent(void); +void test_as_percent(void); + void test_pascal(void); void test_millibar(void); void test_kilopascal(void); diff --git a/test_fraction.c b/test_fraction.c new file mode 100644 index 0000000..726b2fd --- /dev/null +++ b/test_fraction.c @@ -0,0 +1,15 @@ +#include +#include + +#include "test.h" +#include "unit.h" + +void +test_percent(void) { + test(percent(12.345), 0.12345); +} + +void +test_as_percent(void) { + test(as_percent(percent(12.345)), 12.345); +} diff --git a/unit.c b/unit.c index 9e03abf..de24dc3 100644 --- a/unit.c +++ b/unit.c @@ -45,8 +45,16 @@ double as_rpm(AngularSpeed x) { return x * 60.0 / (2.0 * M_PI); } -Fraction percent(double x); -double as_percent(Fraction x); + +Fraction +percent(double x) { + return x / 100.0; +} + +double +as_percent(Fraction x) { + return x * 100.0; +} Pressure -- cgit v1.2.3