30 lines
849 B
Elixir
Raw Normal View History

defmodule Growth.Calc.BMI do
@moduledoc """
Calculate body mass index
"""
@inch_to_centimeter 2.54
@pound_to_kilogram 0.453592
@doc """
Calculate the body mass index for a given weight and height.
Measurements taken in english unit (pounds and inches) are converted to their metric equivalents (kilograms and centimeters).
"""
@spec calculate(:english | :metric, number, number) :: number
def calculate(:english, weight, height) do
metric_weight = pound_to_kilogram(weight)
metric_height = inches_to_centimeters(height)
calculate(:metric, metric_weight, metric_height)
end
def calculate(:metric, weight, height) do
weight / :math.pow(height / 100.0, 2)
end
defp inches_to_centimeters(height), do: height * @inch_to_centimeter
defp pound_to_kilogram(weight), do: weight * @pound_to_kilogram
end