As a super-class of numbers, Kawa also provides quantities. A quantity is a product of a unit and a pure number. The number part can be an arbitrary complex number. The unit is a product of integer powers of base units, such as meter or second.
Quantity literals have the following syntax:
quantity
::=
optional-sign
decimal
unit-term
[*
unit-term
]... [/
unit-term
]
unit-term
::=
unit-name
[^
digit
+]
unit-name
::=
letter
+
Some examples are 10pt
(10 points), 5s
(5 seconds),
and 4cm^2
(4 square centimeters).
Note the quantity
syntax is not recognized by the reader.
Instead these are read as symbols.
Assuming there is no lexical binding the for the symbol, it will be
rewritten at compile-time into an expression. For example 4cm^2
is transformed into:
(* 4.0 (expt unit:cm 2))
True iff
object
is a quantity. Note that all numbers are quantities, but not the other way round. Currently, there are no quantities that re not numbers. To distinguish a plain unit-less number from a quantity, you can usecomplex?
.
Returns the pure number part of the quantity
q
, relative to primitive (base) units. Ifq
is a number, returnsq
. Ifq
is a unit, yields the magitude ofq
relative to base units.
Returns the unit of the quantity
q
. Ifq
is a number, returns the empty unit.
Procedure: make-quantity
x
unit
Returns the product of
x
(a pure number) andunit
. You can specify a string instead ofunit
, such as"cm"
or"s"
(seconds).
Syntax: define-base-unit
unit-name
dimension
Define
unit-name
as a base (primitive) unit, which is used to measure along the specifieddimension
.(define-base-unit dollar "Money")
Syntax: define-unit
unit-name
expression
Define
unit-name
as a unit (that can be used in literals) equal to the quantityexpression
.(define-unit cent 0.01dollar)The
unit-name
is declared in theunit
namespace, so the above is equivalent to:(define-constant unit:cent (* 0.01 unit:dollar))