aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSam Anthony <sam@samanthony.xyz>2025-02-28 21:01:26 -0500
committerSam Anthony <sam@samanthony.xyz>2025-02-28 21:01:26 -0500
commit942a6711361c371da0736cc62f0e3e2ed13f341b (patch)
tree607afae0b1c3446bbdc76da4b59d24ca24f0d753
parentfaf14d71cfbac1f21500f4b997441fa03731bf0e (diff)
downloadvolute-942a6711361c371da0736cc62f0e3e2ed13f341b.zip
fraction/percent conversion functions
-rw-r--r--Makefile2
-rw-r--r--test.c3
-rw-r--r--test.h3
-rw-r--r--test_fraction.c15
-rw-r--r--unit.c12
5 files changed, 32 insertions, 3 deletions
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 <assert.h>
+#include <stdio.h>
+
+#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