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
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 7 additions & 5 deletions src/Quantity.elm
Original file line number Diff line number Diff line change
Expand Up @@ -516,22 +516,24 @@ sqrt (Quantity value) =


{-| Cube a quantity with some `units`, resulting in a new quantity in
`Cubed units.
`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.
quantity in plain `units`.
-}
cbrt : Quantity Float (Cubed units) -> Quantity Float units
cbrt (Quantity value) =
if value >=0 then
Quantity (value ^ (1 /3))
if value >= 0 then
Quantity (value ^ (1 / 3))

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



---------- INT/FLOAT CONVERSIONS ----------
Expand Down
66 changes: 54 additions & 12 deletions src/Volume.elm
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
module Volume exposing
( Volume, CubicMeters
, cubicMeters, inCubicMeters
, cubicInches, inCubicInches, cubicFeet, inCubicFeet, cubicYards, inCubicYards
, milliliters, inMilliliters, liters, inLiters
, cubicInches, inCubicInches, cubicFeet, inCubicFeet, cubicYards, inCubicYards
, usLiquidGallons, inUsLiquidGallons, usDryGallons, inUsDryGallons, imperialGallons, inImperialGallons
--, usLiquidQuarts, usDryQuarts, imperialQuarts
, usLiquidQuarts, inUsLiquidQuarts, usDryQuarts, inUsDryQuarts, imperialQuarts, inImperialQuarts
--, usLiquidPints, usDryPints, imperialPints
--, usFluidOunces, imperialFluidOunces
)
Expand All @@ -18,21 +18,21 @@ US liquid gallons, imperial fluid ounces etc. It is stored as a number of cubic
## Metric

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


## Imperial

@docs cubicInches, inCubicInches, cubicFeet, inCubicFeet, cubicYards, inCubicYards
@docs usLiquidGallons, inUsLiquidGallons, usDryGallons, inUsDryGallons, imperialGallons, inImperialGallons
--@docs usLiquidQuarts, usDryQuarts, imperialQuarts
--@docs usLiquidPints, usDryPints, imperialPints
--@docs usFluidOunces, imperialFluidOunces
@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 (Quantity(..), Cubed)
import Quantity exposing (Cubed, Quantity(..))


{-| -}
Expand Down Expand Up @@ -133,14 +133,14 @@ inLiters volume =
-}
usLiquidGallons : Float -> Volume
usLiquidGallons numUsLiquidGallons =
cubicMeters ( numUsLiquidGallons / 264.17220000000003 )
cubicMeters (numUsLiquidGallons / 264.17220000000003)


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


{-| Construct a volume from a number of usDryGallons.
Expand All @@ -154,18 +154,60 @@ usDryGallons numUsDryGallons =
-}
inUsDryGallons : Volume -> Float
inUsDryGallons volume =
227.0208 * inCubicMeters volume
227.0208 * inCubicMeters volume


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


{-| Convert a volume to a number of imperialGallons.
-}
inImperialGallons : Volume -> Float
inImperialGallons volume =
219.9688 * inCubicMeters 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
24 changes: 24 additions & 0 deletions tests/Tests.elm
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module Tests exposing
, speeds
, temperatureDeltas
, temperatures
, volumes
)

import Acceleration exposing (..)
Expand Down Expand Up @@ -186,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" <|
Expand Down Expand Up @@ -313,5 +334,8 @@ conversionsToQuantityAndBack =
, 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
]
]