Chemical Calculator

From Fōrmulæ wiki
Revision as of 10:40, 22 March 2019 by Admin (Talk | contribs) (Preamble)

Jump to: navigation, search

This page is the answer to the task Chemical Calculator in the Rosetta Code.

Description (from Rosetta Code)

Given a molecule's chemical formula, calculate the molar mass.


  • A molecule consists of atoms. E.g. water, H2O, has two hydrogen atoms and one oxygen atom
  • The mass of H2O is 1.008 * 2 + 15.999 = 18.015
  • An atom name consists of one upper-case letter followed by zero, one or two lower-case letters.
    • H (Hydrogen)
    • He (Helium)
    • Uue (Ununennium)
  • The number of atoms is stated behind the atom or atom group
  • An atom group is specified using parenthesis. E.g. Butyric acid, (CH3)2CHCOOH, has two CH3 groups
  • A group may contain other groups, e.g. COOH(C(CH3)2)3CH3


Molecular mass



There is a Fōrmulæ package for chemistry being developed at the moment of writing this article, but it has enough features to accomplish this task.

Chemical elements

There is a Fōrmulæ expression for each chemical element, as follows:

Atomic number Element expression
1 Chemistry.Element.Hydrogen
2 Chemistry.Element.Helium
3 Chemistry.Element.Lithium
 :  :

They are shown as their chemical symbol, it is H for Hydrogen, He for Helium, etc.

Chemical compounds

There are two Fōrmulæ expression to make chemical formulae:

The Chemistry.Compund expression represents a composition between two or more chemical elements, but it can also contain either Chemistry.Compond or Chemistry.Group (see below) expressions. For example, the composition of the chemical elements C and O results in the compund CO.

This expression is shown as every subexpression set one next the other, so the

The Chemistry.Group represents an expression that is present several times. It contains exactly two subexpressions, the first one is the chemical element, Chemistry.Compound or Chemistry.Group expression, and the second one is the number of times. For example, grouping the O chemical element two times results in O2

The following are examples of chemical formulae created with the previous expressions:

ChemicalCalculatorPreamble1.png ChemicalCalculatorPreamble2.png ChemicalCalculatorPreamble3.png

Let us see the structures of the given formulae, by the use of the ToTree expression:

ChemicalCalculatorPreamble4.png ChemicalCalculatorPreamble5.png ChemicalCalculatorPreamble6.png




Use cases


Other examples