aboutsummaryrefslogtreecommitdiffstats
path: root/pressure
diff options
context:
space:
mode:
authorSam Anthony <sam@samanthony.xyz>2022-05-15 16:51:41 -0230
committerSam Anthony <sam@samanthony.xyz>2022-05-15 16:51:41 -0230
commit585fbf852c1e76470df42ebe99ede62440ce19d9 (patch)
tree4bb5406eb0e1d1a978e21de1e6c4291c22510d39 /pressure
parent98a5ded8daccbb576b2928359112c6454fd0c5b3 (diff)
downloadvolute-585fbf852c1e76470df42ebe99ede62440ce19d9.zip
simplify unit conversion
Diffstat (limited to 'pressure')
-rw-r--r--pressure/pressure.go30
1 files changed, 9 insertions, 21 deletions
diff --git a/pressure/pressure.go b/pressure/pressure.go
index b3063cc..da1ab1e 100644
--- a/pressure/pressure.go
+++ b/pressure/pressure.go
@@ -5,13 +5,13 @@ import (
"fmt"
)
-type unit float32
+type Pressure float32
const (
- Pascal unit = 1
- Kilopascal unit = 1_000
- Bar unit = 100_000
- PoundsPerSquareInch unit = 6_894.757
+ Pascal Pressure = 1
+ Kilopascal Pressure = 1_000
+ Bar Pressure = 100_000
+ PoundsPerSquareInch Pressure = 6_894.757
)
// UnitStrings returns a slice of strings, each representing a
@@ -22,12 +22,12 @@ func UnitStrings() []string {
}
const (
- DefaultUnit unit = Kilopascal
+ DefaultUnit Pressure = Kilopascal
// DefaultUnitIndex is used to index UnitStrings().
DefaultUnitIndex int32 = 1 // kPa
)
-func UnitFromString(s string) (unit, error) {
+func UnitFromString(s string) (Pressure, error) {
// Each case corresponds to a value in UnitStrings().
switch s {
case "Pa":
@@ -39,22 +39,10 @@ func UnitFromString(s string) (unit, error) {
case "psi":
return PoundsPerSquareInch, nil
default:
- return *new(unit), errors.New(fmt.Sprintf("invalid unit: '%s'", s))
+ return *new(Pressure), errors.New(fmt.Sprintf("invalid unit: '%s'", s))
}
}
-type Pressure struct {
- val float32
-}
-
-func New(i float32, u unit) Pressure {
- return Pressure{i * float32(u)}
-}
-
-func (p Pressure) AsUnit(u unit) float32 {
- return p.val / float32(u)
-}
-
func Atmospheric() Pressure {
- return Pressure{float32(1 * Bar)}
+ return 1 * Bar
}