Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#6 add volume module #9

Merged
merged 18 commits into from
Oct 7, 2018
Merged
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
@@ -4,3 +4,4 @@ add your name to the end of this list!

Ian Mackenzie <[email protected]>
Matthias Devlamynck <[email protected]>
Katja Mordaunt <[email protected]>
3 changes: 2 additions & 1 deletion elm.json
Original file line number Diff line number Diff line change
@@ -22,7 +22,8 @@
"Resistance",
"Speed",
"Temperature",
"Voltage"
"Voltage",
"Volume"
],
"elm-version": "0.19.0 <= v < 0.20.0",
"dependencies": {
41 changes: 36 additions & 5 deletions src/Quantity.elm
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
module Quantity exposing
( Quantity(..)
, Squared, Rate
, Squared, Cubed, Rate
, zero, infinity, positiveInfinity, negativeInfinity
, lessThan, greaterThan, compare, equalWithin, max, min, isNaN, isInfinite
, negate, plus, minus, product, ratio, scaleBy, divideBy, abs, clamp, squared, sqrt
, negate, plus, minus, product, ratio, scaleBy, divideBy, abs, clamp, squared, sqrt, cubed, cbrt
, round, floor, ceiling, truncate, toFloatQuantity
, sum, minimum, maximum, sort
, per, times, at, at_, inverse
@@ -17,10 +17,10 @@ module Quantity exposing

# Unit types

The `Squared` and `Rate` units types allow you to build up and work with
The `Squared`, `Cubed` and `Rate` units types allow you to build up and work with
composite units in a fairly flexible way.

@docs Squared, Rate
@docs Squared, Cubed, Rate


# Constants
@@ -35,7 +35,7 @@ composite units in a fairly flexible way.

# Arithmetic

@docs negate, plus, minus, product, ratio, scaleBy, divideBy, abs, clamp, squared, sqrt
@docs negate, plus, minus, product, ratio, scaleBy, divideBy, abs, clamp, squared, sqrt, cubed, cbrt


# `Int`/`Float` conversion
@@ -111,6 +111,17 @@ type Squared units
= Squared units


{-| Represents a units type that is the cube of some other units type; for
example, `Meters` is one units type (the units type of a `Length`) and `Cubed
Meters` is another (the units type of an `Volume`). This is useful because some
functions in this module (specifically [`cubed`](Quantity#cubed),
and [`cbrt`](Quantity#cbrt)) "know" about the
`Cubed` type and how to work with it.
-}
type Cubed units
= Cubed units


{-| Represents the units type of a rate or quotient such as a speed (`Rate
Meters Seconds`) or a pressure (`Rate Newtons SquareMeters`). As with `Squared`,
there are several functions that "know" about the `Rate` units type and how to
@@ -504,6 +515,26 @@ sqrt (Quantity value) =
Quantity (Basics.sqrt value)


{-| Cube a quantity with some `units`, resulting in a new quantity in
`Cubed units`.
ianmackenzie marked this conversation as resolved.
Show resolved Hide resolved
-}
cubed : Quantity number units -> Quantity number (Cubed units)
cubed (Quantity value) =
Quantity (value * value * value)


{-| Take a quantity in `Cubed units` and return the cube root of that
quantity in plain `units`.
-}
cbrt : Quantity Float (Cubed units) -> Quantity Float units
cbrt (Quantity value) =
if value >= 0 then
Quantity (value ^ (1 / 3))

else
Quantity -(-value ^ (1 / 3))



---------- INT/FLOAT CONVERSIONS ----------

213 changes: 213 additions & 0 deletions src/Volume.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
module Volume exposing
( Volume, CubicMeters
, cubicMeters, inCubicMeters
, milliliters, inMilliliters, liters, inLiters
, cubicInches, inCubicInches, cubicFeet, inCubicFeet, cubicYards, inCubicYards
, usLiquidGallons, inUsLiquidGallons, usDryGallons, inUsDryGallons, imperialGallons, inImperialGallons
, usLiquidQuarts, inUsLiquidQuarts, usDryQuarts, inUsDryQuarts, imperialQuarts, inImperialQuarts
--, usLiquidPints, usDryPints, imperialPints
--, usFluidOunces, imperialFluidOunces
)

{-| A `Volume` represents a volume in cubic meters, cubic feet, liters,
US liquid gallons, imperial fluid ounces etc. It is stored as a number of cubic meters.

@docs Volume, CubicMeters


## Metric

@docs cubicMeters, inCubicMeters
@docs milliliters, inMilliliters, liters, inLiters


## Imperial

@docs cubicInches, inCubicInches, cubicFeet, inCubicFeet, cubicYards, inCubicYards
@docs usLiquidGallons, inUsLiquidGallons, usDryGallons, inUsDryGallons, imperialGallons, inImperialGallons
@docs usLiquidQuarts, inUsLiquidQuarts, usDryQuarts, inUsDryQuarts, imperialQuarts, inImperialQuarts
@docs --@docs usLiquidPints, usDryPints, imperialPints
@docs --@docs usFluidOunces, imperialFluidOunces

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@docs appears twice here, once before and once after the comment.


-}

import Length exposing (Meters)
import Quantity exposing (Cubed, Quantity(..))


{-| -}
type alias CubicMeters =
Cubed Meters


{-| -}
type alias Volume =
Quantity Float CubicMeters


{-| Construct a volume from a number of cubic meters.
-}
cubicMeters : Float -> Volume
cubicMeters numCubicMeters =
Quantity numCubicMeters


{-| Convert a volume to a number of cubic meters.
-}
inCubicMeters : Volume -> Float
inCubicMeters (Quantity numCubicMeters) =
numCubicMeters


{-| Construct a volume from a number of cubic inches.
-}
cubicInches : Float -> Volume
cubicInches numCubicInches =
cubicMeters (0.0254 * 0.0254 * 0.0254 * numCubicInches)


{-| Convert a volume to a number of cubic inches.
-}
inCubicInches : Volume -> Float
inCubicInches volume =
inCubicMeters volume / (0.0254 * 0.0254 * 0.0254)


{-| Construct a volume from a number of cubic feet.
-}
cubicFeet : Float -> Volume
cubicFeet numCubicFeet =
cubicMeters (0.3048 * 0.3048 * 0.3048 * numCubicFeet)


{-| Convert a volume to a number of cubic feet.
-}
inCubicFeet : Volume -> Float
inCubicFeet volume =
inCubicMeters volume / (0.3048 * 0.3048 * 0.3048)


{-| Construct a volume from a number of cubic yards.
-}
cubicYards : Float -> Volume
cubicYards numCubicYards =
cubicMeters (0.9144 * 0.9144 * 0.9144 * numCubicYards)


{-| Convert a volume to a number of cubic yards.
-}
inCubicYards : Volume -> Float
inCubicYards volume =
inCubicMeters volume / (0.9144 * 0.9144 * 0.9144)


{-| Construct a volume from a number of milliliters.
-}
milliliters : Float -> Volume
milliliters numMilliliters =
cubicMeters (1.0e-6 * numMilliliters)


{-| Convert a volume to a number of milliliters.
-}
inMilliliters : Volume -> Float
inMilliliters volume =
1.0e6 * inCubicMeters volume


{-| Construct a volume from a number of liters.
-}
liters : Float -> Volume
liters numLiters =
cubicMeters (0.001 * numLiters)


{-| Convert a volume to a number of liters.
-}
inLiters : Volume -> Float
inLiters volume =
1000 * inCubicMeters volume


{-| Construct a volume from a number of usLiquidGallons.
-}
usLiquidGallons : Float -> Volume
usLiquidGallons numUsLiquidGallons =
cubicMeters (numUsLiquidGallons / 264.17220000000003)


{-| Convert a volume to a number of usLiquidGallons.
-}
inUsLiquidGallons : Volume -> Float
inUsLiquidGallons volume =
264.17220000000003 * inCubicMeters volume


{-| Construct a volume from a number of usDryGallons.
-}
usDryGallons : Float -> Volume
usDryGallons numUsDryGallons =
cubicMeters (numUsDryGallons / 227.0208)


{-| Convert a volume to a number of usDryGallons.
-}
inUsDryGallons : Volume -> Float
inUsDryGallons volume =
227.0208 * inCubicMeters volume


{-| Construct a volume from a number of imperialGallons.
-}
imperialGallons : Float -> Volume
imperialGallons numImperialGallons =
cubicMeters (numImperialGallons / 219.969157)


{-| Convert a volume to a number of imperialGallons.
-}
inImperialGallons : Volume -> Float
inImperialGallons volume =
219.969157 * inCubicMeters volume


{-| Construct a volume from a number of usLiquidQuarts.
-}
usLiquidQuarts : Float -> Volume
usLiquidQuarts numUsLiquidQuarts =
cubicMeters ((numUsLiquidQuarts / 4) / 264.17220000000003)


{-| Convert a volume to a number of usLiquidQuarts.
-}
inUsLiquidQuarts : Volume -> Float
inUsLiquidQuarts volume =
4 * 264.17220000000003 * inCubicMeters volume


{-| Construct a volume from a number of usDryQuarts.
-}
usDryQuarts : Float -> Volume
usDryQuarts numUsDryQuarts =
cubicMeters ((numUsDryQuarts / 4) / 227.0208)


{-| Convert a volume to a number of usDryQuarts.
-}
inUsDryQuarts : Volume -> Float
inUsDryQuarts volume =
4 * 227.0208 * inCubicMeters volume


{-| Construct a volume from a number of imperialQuarts.
-}
imperialQuarts : Float -> Volume
imperialQuarts numImperialQuarts =
cubicMeters ((numImperialQuarts / 4) / 219.969157)


{-| Convert a volume to a number of imperialQuarts.
-}
inImperialQuarts : Volume -> Float
inImperialQuarts volume =
4 * 219.969157 * inCubicMeters volume
36 changes: 36 additions & 0 deletions tests/Tests.elm
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@ module Tests exposing
, speeds
, temperatureDeltas
, temperatures
, volumes
)

import Acceleration exposing (..)
@@ -30,6 +31,7 @@ import Speed exposing (..)
import Temperature exposing (Temperature)
import Test exposing (Test)
import Voltage exposing (..)
import Volume exposing (..)


equalityTest : String -> String -> ( Quantity Float units, Quantity Float units ) -> Test
@@ -185,6 +187,26 @@ temperatureDeltas =
]


volumes : Test
volumes =
equalPairs
"Volumes"
"m^3"
[ ( cubicInches (36 * 36 * 36)
, cubicYards 1
)
, ( usLiquidGallons 1
, usLiquidQuarts 4
)
, ( usDryGallons 1
, usDryQuarts 4
)
, ( imperialGallons 1
, imperialQuarts 4
)
]


conversionsToQuantityAndBack : Test
conversionsToQuantityAndBack =
Test.describe "Conversion to Quantity and back is (almost) identity" <|
@@ -302,4 +324,18 @@ conversionsToQuantityAndBack =
, Test.describe "Voltage" <|
[ fuzzFloatToQuantityAndBack "volts" Voltage.volts Voltage.inVolts
]
, Test.describe "Volume" <|
[ fuzzFloatToQuantityAndBack "cubicMeters" Volume.cubicMeters Volume.inCubicMeters
, fuzzFloatToQuantityAndBack "cubicInches" Volume.cubicInches Volume.inCubicInches
, fuzzFloatToQuantityAndBack "cubicFeet" Volume.cubicFeet Volume.inCubicFeet
, fuzzFloatToQuantityAndBack "cubicYards" Volume.cubicYards Volume.inCubicYards
, fuzzFloatToQuantityAndBack "milliliters" Volume.milliliters Volume.inMilliliters
, fuzzFloatToQuantityAndBack "liters" Volume.liters Volume.inLiters
, fuzzFloatToQuantityAndBack "usLiquidGallons" Volume.usLiquidGallons Volume.inUsLiquidGallons
, fuzzFloatToQuantityAndBack "usDryGallons" Volume.usDryGallons Volume.inUsDryGallons
, fuzzFloatToQuantityAndBack "imperialGallons" Volume.imperialGallons Volume.inImperialGallons
, fuzzFloatToQuantityAndBack "usLiquidQuarts" Volume.usLiquidQuarts Volume.inUsLiquidQuarts
, fuzzFloatToQuantityAndBack "usDryQuarts" Volume.usDryQuarts Volume.inUsDryQuarts
, fuzzFloatToQuantityAndBack "imperialQuarts" Volume.imperialQuarts Volume.inImperialQuarts
]
]