Skip to content

Expressions

饺子w (Yumechi) edited this page Aug 23, 2023 · 7 revisions

Expressions that are not preceded by a repl command are interpreted, computed and applied on the current stack just like dc.

In this mode, every token is interpreted as an operation, based on their type:

Quantity

Numbers

The following forms are allowed:

1 1. are equivalent

0.234 .234 are equivalent

1e-2 .01 are equivalent

Additionally, an underscore (_) could be used anywhere in a number literal.

Unit

A unit is defined as a base unit (length, mass, etc.). Derived units such as molarity should be defined as an unit alias.

Units are denoted by an identifier enclosed by parenthesis (()).

The following units are defined automatically:

  • No Unit: 1
  • Mass: g.
  • Volume: l.
  • International Unit (IU): iu.
  • Length: m.
  • Mole: mol.

A unit token in RPN mode set the unit of the quantity on top of stack.

If the top of the stack already has a quantity: unitdc will check if they are equivalent, if so a unit conversion would be performed. Otherwise an error will be thrown and evaluation will stop.

To forcefully interpret an existing quantity with unit as another unit, convert it into no unit((1) operator) and then convert back to the desired unit.

Derived Units

An derived unit follows the same syntax as an unit, but internally handled as a combination of base units.

The following units are defined automatically:

  • Molar: M = 1 (mol) 1 (l) /. mM uM nM pM.
  • Dalton: Da = 1 (g) 1 (mol) /. kDa
  • Mass: mg ug ng pg.
  • Volume: dl ml ul nl.
  • Length: cm mm um nm.
  • Mole: mmol umol nmol pmol.

For a full list of pre-defined units, see here

Operators

Unary operators

The following operators are defined, functionality is equivalent to dc unless otherwise specified. Necessary unit conversions will be performed and if they are impossible, an error will be thrown and evaluation will stop, stack remains unmodified:

c

d

v

p

n

f

Binary operators

The following operators are defined, functionality is equivalent to dc unless otherwise specified. Two quantities are popped from the stack and the result is pushed back onto the stack, necessary unit conversions will be performed and if they are impossible, an error will be thrown and evaluation will stop, stack remains unmodified:

+

-

*

/

r

Special Operators

s: The Unit Solver

U: Outputs a summary of the current unit system.

Macros

There are two macros defined:

  • @base(xxx) defines xxx as a base unit.
  • @derived(xxx) pops two values on the stack, the first one is the scale and the second one is the offset, and defines xxx to be a derived unit equivalent to scale * x + offset.
Clone this wiki locally