Module: UnitMeasurements::Math

Included in:
Measurement
Defined in:
lib/unit_measurements/math.rb

Overview

The UnitMeasurements::Math mixin module provides methods for performing mathematical functions on the measurement.

This module is included in the Measurement class to allow mathematical functions on the measurement.

See Also:

Author:

Since:

  • 1.6.0

Instance Method Summary collapse

Instance Method Details

#absMeasurement

Returns absolute value of the measurement quantity.

Examples:

UnitMeasurements::Length.new(-17.625, "m").abs
=> 17.625 m

Returns:

  • (Measurement)

    A new Measurement instance with the absolute value of the quantity.

Author:

Since:

  • 1.6.0



47
48
49
# File 'lib/unit_measurements/math.rb', line 47

def abs
  self.class.new(quantity.abs, unit)
end

#cbrtMeasurement

Returns cube root of the measurement quantity.

Examples:

UnitMeasurements::Length.new(27, "m").cbrt
=> 3.0 m

Returns:

  • (Measurement)

    A new Measurement instance with cube root of quantity.

Author:

Since:

  • 5.13.0



116
117
118
# File 'lib/unit_measurements/math.rb', line 116

def cbrt
  self.class.new((quantity ** Rational(1, 3)), unit)
end

#ceil(ndigits = 0) ⇒ Measurement

Returns ceiled quantity of the measurement. If ndigits is not specified, quantity is rounded to next higher Integer.

Examples:

UnitMeasurements::Length.new(17.625, "m").ceil
=> 18 m

UnitMeasurements::Length.new(17.625, "m").ceil(2)
=> 17.63 m

Parameters:

  • ndigits (Integer, optional) (defaults to: 0)

    The number of digits to round to.

Returns:

  • (Measurement)

    A new Measurement instance with the ceiled quantity.

Author:

Since:

  • 1.6.0



87
88
89
# File 'lib/unit_measurements/math.rb', line 87

def ceil(ndigits = 0)
  self.class.new(quantity.ceil(ndigits), unit)
end

#floor(ndigits = 0) ⇒ Measurement

Returns floored quantity of the measurement. If ndigits is not specified, quantity is rounded to next lower Integer.

Examples:

UnitMeasurements::Length.new(17.625, "m").floor
=> 17 m

UnitMeasurements::Length.new(17.625, "m").floor(2)
=> 17.62 m

Parameters:

  • ndigits (Integer, optional) (defaults to: 0)

    The number of digits to round to.

Returns:

  • (Measurement)

    A new Measurement instance with the floored quantity.

Author:

Since:

  • 1.6.0



67
68
69
# File 'lib/unit_measurements/math.rb', line 67

def floor(ndigits = 0)
  self.class.new(quantity.floor(ndigits), unit)
end

#round(ndigits = 0) ⇒ Measurement

Rounds quantity of the measurement. If ndigits is not specified, quantity is rounded to Integer.

Examples:

UnitMeasurements::Length.new(17.625, "m").round
=> 18 m

UnitMeasurements::Length.new(17.625, "m").round(2)
=> 17.63 m

Parameters:

  • ndigits (Integer, optional) (defaults to: 0)

    The number of digits to round to.

Returns:

  • (Measurement)

    A new Measurement instance with the rounded quantity.

Author:

Since:

  • 1.6.0



32
33
34
# File 'lib/unit_measurements/math.rb', line 32

def round(ndigits = 0)
  self.class.new(quantity.round(ndigits), unit)
end

#sqrtMeasurement

Returns square root of the measurement quantity.

Examples:

UnitMeasurements::Length.new(9, "m").sqrt
=> 3.0 m

Returns:

  • (Measurement)

    A new Measurement instance with square root of quantity.

Author:

Since:

  • 5.13.0



101
102
103
# File 'lib/unit_measurements/math.rb', line 101

def sqrt
  self.class.new((quantity ** Rational(1, 2)), unit)
end