Quantities extension for Numbas

This extension wraps the js-quantities library to provide a "quantity with units" data type to Numbas.

It provides a quantity data type, which represents a scalar amount* and a list of *units.

A note about precision: Amounts are represented with JavaScript floating-point numbers, which are only precise to around 30 decimal places. It's our intention to add support for more precise number representations to Numbas; when that happens, this extension could be updated to use that.

JME functions

quantity([number], units) or qty([number], units)

Create a quantity with the given units. If the number is not given, the returned quantity represents 1 of the given units. Use * and / to combine units, and ^ for powers of units. An empty units string will produce a unitless quantity.

Examples:

units_of_kind(kind)

Returns a list of recognised units of the given kind.

Example:

aliases(unit)

Returns a list of accepted names for the given unit.

Example:

compatible(q1, q2)

Are the two given quantities compatible? That is, are they of the same kindm, so one can be converted to the other?

Examples:

kind(quantity)

What kind of unit is quantity measured in? Returns a string code corresponding to the kind. For combinations of units that don't correspond to a built-in kind, an empty string is returned.

Examples:

unitless(quantity)

Does the given quantity have no associated units? Note that a dimensionless* quantity is *not necessarily unitless - for example, a quantity measured in meters per foot has no dimension but is not unitless.

isbase(quantity)

Is the given quantity in base units, as defined in the International System of Units (SI)?

Examples:

tobase(quantity)

Convert the given quantity to base SI units.

Example:

q_from in units or q_from in q_to

Convert quantity q_from to the units specified by the given string, or the same units as q_to. If the desired units are not compatible with q_from, an error is thrown.

Examples:

as_si(quantity)

Convert quantity to SI units.

Example:

inverse(quantity)

Return the reciprocal of the given quantity.

Example:

same(a, b)

Are a and b both exactly the same quantity, measured in the same units?

Examples:

a < b, a <= b, a > b, a >= b

Compare quantities a and b. If their units are not compatible, an error is thrown.

Examples:

a = b

Quantities a and b are equal if their units are compatible and they represent exactly the same amount.

Examples:

a + b, a - b, a * b, a / b

Arithmetic on units is supported. When adding or subtracting units, the result is given in the same units as the left-hand argument, and if the two quantities being combined are in incompatible units an error is thrown. When multiplying or dividing, units are not automatically converted to their common names. For example, The division of a quantity in Newtons by a quantity in m^2 returns a quantity in N/m^2, not in Pa.

Examples:

-q

The negative of the given quantity.

Example:

n * quantity or quantity * n

Multiply a quantity by a scalar.

Example:

round(quantity, precision)

Round quantity to the nearest multiple of precision, given either as a string in the form "amount units", or another quantity. If precision is not given, the quantity is rounded to the nearest whole unit.

Examples:

precround(quantity,dp)

Round quantity to dp decimal places.

Example:

siground(quantity,sf)

Round quantity to sf significant figures.

Example:

abs(quantity)

The absolute value of the quantity.

Example:

scalar(quantity)

The scalar amount of the quantity, as a number - in other words, strip off the units information.

Example:

sign(quantity)

Returns 1 if the quantity is positive, -1 if it's negative, and 0 otherwise.

Examples:

string(quantity, [notation style])

A string representing the given quantity, in the given notational style (plain English is the default) The units are presented using "nice" characters: a dot for multiplication, and exponents are displayed in superscript.

Examples:

plain_string(quantity, [notation style])

A string representing the given quantity, in the given notational style (plain English is the default). The units are given in a form that is easy to type: * is used for multiplication, and ^ marks an exponent.

Examples:

units_numerator(quantity) and units_denominator(quantity)

The units of a quantity can be written as a fraction whose numerator and denominator both consist of a list of units. When a squared or cubed unit is used, the base unit is repeated. These functions return the lists of units in the numerator and denominator, respectively, for a given quantity. Any order-of-magnitude prefixes are included as separate items in the list

Examples:

units(quantity)

Return a quantity representing one unit of the same kind as the given quantity.

Example:

units_string(quantity)

Return a string describing the units of the given quantity, suitable for display. Powers are displayed in superscript, and is used for multiplication. The units are presented using "nice" characters: a dot for multiplication, and exponents are displayed in superscript.

Example:

plain_units_string(quantity)

Return a string describing the units of the given quantity, suitable for display. Powers are displayed in superscript, and is used for multiplication. The units are given in a form that is easy to type: * is used for multiplication, and ^ marks an exponent.

Example:

quantity_kinds

A constant list of all the recognised kinds of units.

JavaScript functions

See the documentation for js-quantities for detail on its JavaScript API.

The Qty object is available globally when this extension is loaded.

In addition, the following functions are defined under the Numbas.extensions.quantities namespace:

precround(q,dp)

Round the quantity q to dp decimal places. See Numbas.math.precround

siground(q,sf)

Round the quantity q to sf significant figures. See Numbas.math.siground

<a name="recognised-units">Recognised units</a>

The following unit names are recognised. Units of the same kind can be converted between each other. The prefixes can be written to the left of any other unit, to change the order of magnitude.

acceleration

activity

angle

angular velocity

area

capacitance

charge

concentration

conductance

counting

currency

current

energy

force

frequency

illuminance

inductance

information

information rate

length

logarithmic

luminosity

luminous power

magnetism

mass

potential

power

<a name="prefixes">prefix</a>

prefix only

pressure

radiation

rate

resistance

resolution

solid angle

speed

substance

temperature

time

typography

viscosity

volume