aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--test_pressure.c21
-rw-r--r--unit.c69
2 files changed, 70 insertions, 20 deletions
diff --git a/test_pressure.c b/test_pressure.c
index a64bac9..f04a1bc 100644
--- a/test_pressure.c
+++ b/test_pressure.c
@@ -2,53 +2,54 @@
#include <stdio.h>
#include "test.h"
+#include "unit.h"
void
test_pascal(void) {
- assert(0);
+ test(pascal(123.456), 123.456);
}
void
test_millibar(void) {
- assert(0);
+ test(millibar(123.456), 12345.6);
}
void
test_kilopascal(void) {
- assert(0);
+ test(kilopascal(123.456), 123456.0);
}
void
test_bar(void) {
- assert(0);
+ test(bar(123.456), 12345600.0);
}
void
test_psi(void) {
- assert(0);
+ test(psi(123.456), 851199.15638539323);
}
void
test_as_pascal(void) {
- assert(0);
+ test(as_pascal(pascal(123.456)), 123.456);
}
void
test_as_millibar(void) {
- assert(0);
+ test(as_millibar(millibar(123.456)), 123.456);
}
void
test_as_kilopascal(void) {
- assert(0);
+ test(as_kilopascal(kilopascal(123.456)), 123.456);
}
void
test_as_bar(void) {
- assert(0);
+ test(as_bar(bar(123.456)), 123.456);
}
void
test_as_psi(void) {
-
+ test(as_psi(psi(123.456)), 123.456);
}
diff --git a/unit.c b/unit.c
index fde768f..316a39a 100644
--- a/unit.c
+++ b/unit.c
@@ -3,6 +3,16 @@
#include "unit.h"
+/* Kilograms per pound. */
+#define KG_PER_LB 0.45359237
+
+/* Acceleration of gravity [m/(s*s)]. */
+#define G 9.80665
+
+/* Metres per inch. */
+#define M_PER_IN 0.0254
+
+
AngularSpeed
rad_per_sec(double x) {
return x;
@@ -33,16 +43,55 @@ Fraction percent(double x);
double as_percent(Fraction x);
-Pressure pascal(double x);
-Pressure millibar(double x);
-Pressure kilopascal(double x);
-Pressure bar(double x);
-Pressure psi(double x);
-double as_pascal(Pressure x);
-double as_millibar(Pressure x);
-double as_kilopascal(Pressure x);
-double as_bar(Pressure x);
-double as_psi(Pressure x);
+Pressure
+pascal(double x) {
+ return x;
+}
+
+Pressure
+millibar(double x) {
+ return x * 100.0;
+}
+
+Pressure
+kilopascal(double x) {
+ return x * 1e3;
+}
+
+Pressure
+bar(double x) {
+ return x * 1e5;
+}
+
+Pressure
+psi(double x) {
+ return x * KG_PER_LB * G / pow(M_PER_IN, 2);
+}
+
+double
+as_pascal(Pressure x) {
+ return x;
+}
+
+double
+as_millibar(Pressure x) {
+ return x / 100.0;
+}
+
+double
+as_kilopascal(Pressure x) {
+ return x * 1e-3;
+}
+
+double
+as_bar(Pressure x) {
+ return x * 1e-5;
+}
+
+double
+as_psi(Pressure x) {
+ return x * pow(M_PER_IN, 2) / (KG_PER_LB * G);
+}
Volume