Class

Unit​Converter​Linear

NSUnit​Converter​Linear is an Unit​Converter subclass for converting between units using a linear equation.

Overview

A linear equation for unit conversion takes the form y = mx + b, such that:

  • y is the value in terms of the base unit of the dimension

  • m is the known coefficient used for this unit's conversion

  • x is the value in terms of the unit on which this method is called

  • b is the known constant used for this unit's conversion

The base​Unit​Value​From​Value:​ method performs the conversion in the form of y = mx + b, where x represents the value passed in and y represents the value returned. The value​From​Base​Unit​Value:​ method performs the inverse conversion in the form of x = (y - b) / m, where y represents the value passed in and x represents the value returned.

For example, consider the fahrenheit unit defined by Unit​Temperature: The base​Unit​Value​From​Value:​ method calculates the value in the base unit, kelvin using the formula K = (0.55555555555556) × °F + 255.37222222222427, and the value​From​Base​Unit​Value:​ method calculates the value in fahrenheit using the formula °F = (K — 255.37222222222427) / (0.55555555555556), where the coefficient is (0.55555555555556) and the constant is 255.37222222222427.

let kelvinToFahrenheit = UnitConverterLinear(coefficient: 0.55555555555556, constant: 255.37222222222427)

Units that perform conversion using only a scale factor have a coefficient equal to the scale factor and a constant equal 0. For example, consider the kilometers unit defined by Unit​Length: The base​Unit​Value​From​Value:​ method calculates the value in meters using the formula value​In​Meters = 1000 * value​In​Kilometers + 0, and the value​From​Base​Unit​Value:​ calculates the value in kilometers using the formula value​In​Kilometers = (value​In​Meters - 0) / 1000, where the coefficient is 1000 and the constant is 0.

let kilometersToMeters = UnitConverterLinear(coefficient: 1000.0, constant: 0.0)

Symbols

Accessing Linear Parameters

var coefficient:​ Double

The coefficient used in the linear unit conversion calculation.

var constant:​ Double

The constant used in the linear unit conversion calculation.

Creating Unit Converters

init(coefficient:​ Double)

Initializes the unit converter with the specified coefficient.

Relationships

Inherits From