The fltpoint package^{∗}

Eckhart Guthöhrlein^{†}

Printed June 11, 2004

Abstract This package provides commands for simple floating point arithmetic with TEX. At the moment, only the four basic operations addition, subtraction, multiplication and division are supported, but future extensions are planned.

1 Introduction

The need for floating-point calculations inside TEX was encountered when working on some macros to convert positions on a linear scale into angle values, since integer values proved not to be sufficiently exact. Although the capabilities of this package are still rather limited at the moment, they may be of some use if you do not need more than the provided functions. Have a look at the rccol or at the plasmid package^{†} for example applications.

2 User interface

The user commands are divided into two categories: the normal and the register commands. Each command is available in those two variants, as decribed below. At first, we have to agree about the syntax for floating point numbers.

2.1 Syntax of floating point numbers

In the syntax descriptions below, ⟨fp number⟩ will be used to denote a number according to the following syntax.

⟨fp number⟩ := ⟨opt signs⟩⟨opt digits⟩⟨opt dot⟩⟨opt digits⟩

⟨opt signs⟩ may be any number of ‘+’ and/or ‘-’ characters, where each ‘-’ toggles the sign of the number. ⟨opt digits⟩ may be any number of characters ‘0’… ‘9’, and ⟨opt dot⟩ is the optional decimal sign. For example, the following inputs for ⟨fp number⟩ are valid, resulting into the specified numbers.

`100' → 100, `010,98700' → 10,987, `-,99' → −0,99, `-+-+0001,' → 1,

∗This file has version number v1.0c dated 2000/09/05.

†Send comments or bug-reports to the author via e-mail <ewg@epost.de>.

†The plasmid package is not yet publically available; a first official release is planned for december. Those among you who know what plasmids are may look forward to a powerful drawing interface.

`' → 0, `---,50' → −0,5. As you can see, leading and trailing zeros are removed as far as possible, and an ‘empty number’ (omitting anything optional) is understood as zero.

There is no syntax checking, so if you do not obey the rules above, you are likely to encounter strange error messages, as well as everything might work properly in some cases. Of course, it is also possible to use a macro as ⟨fp number⟩ if it expands to a string satisfying the syntax rules.

2.2 Standard operations

\fpAdd The standard commands for binary operations have the following common syntax:

\fpSub
\fpMul
\fpDiv

\fp⟨bOp⟩{⟨command sequence⟩}{⟨fp number⟩}{⟨fp number⟩}.

This will perform the operation specified by ⟨bOp⟩ with the two given numbers, saving the result in ⟨command sequence⟩. Possibilities for ⟨bOp⟩ are ‘Add’, ‘Sub’, ‘Mul’ and ‘Div’, specifying addition, subtraction, multiplication, and division. Example:

 \fpAdd{\exmplsum}{100,0}{-99,1}
 \fpMul{\exmplprod}{5}{\exmplsum}

After this, the results of the computations will be stored in the macros \exmplsum and \exmplprod, expanding to 0,9 and 4,5.

\fpNeg Similar to the binary operations, the unary operations share the common syn-

\fpAbs tax

\fp⟨uOp⟩{⟨command sequence⟩}{⟨fp number⟩}.

Possibilities for ⟨uOp⟩ are ‘Abs’ and ‘Neg’, meaning absolute amount and negation.

\fpRound With \fpRound{⟨command sequence⟩}{⟨fp number⟩}{⟨precision⟩}, a number can be rounded to the desired precision (a power of ten). The result is saved in ⟨command sequence⟩ as usual.

2.3 Register operations

You may use register variants of all operations, which means that you perform the operation on a register which contains a number. A register is referred to using its name; the name may contain any characters including digits.

\fpRegSet Registers are initialized by assigning them values, using \fpRegSet. They can \fpRegGet be read out into command sequences using \fpRegGet.

\fpRegSet{⟨reg name⟩}{⟨fp number⟩} \fpRegGet{⟨reg name⟩}{⟨command sequence⟩}

\fpRegAdd The binary operations need two register names. After execution, the first

\fpRegSub
\fpRegMul
\fpRegDiv

register will hold the result of the specified computation, performed with its former value and the value of the second register.

\fp⟨bOp⟩{⟨reg name 1 ⟩}{⟨reg name 2 ⟩}

\fpRegAbs Consequently, the unary operations only need the name of the register.

\fpRegNeg

\fp⟨uOp⟩{⟨reg name⟩} \fpRegRound Rounding of registers is also possible.

\fpRound{⟨reg name⟩}{⟨precision⟩}

\fpRegCopy Furthermore, there is one binary operation only available for registers, this is

\fpRegCopy which assigns the value of ⟨reg name 2 ⟩ to register ⟨reg name 1 ⟩. For example, consider the following statements.

 \fpRegSet{test1}{36}     \fpRegSet{test2}{-3}
 \fpRegDiv{test1}{test2}  \fpRegMul{test1}{test1}
 \fpRegGet{test1}{\fpresult}

After this, test1 will hold the value 144, which \fpresult will expand to.

2.4 Configuration and Parameters

\fpAccuracy The macro \fpAccuracy takes one argument (a number), determining the number of digits after the decimal sign, i. e., the accuracy of the computations. The default value is five. At the moment, the name promises too much. The command only

affects \fpDiv and \fpRegDiv.

\fpDecimalSign With \fpDecimalSign{⟨character⟩} you can chose any character for use as the decimal sign. Normally, this will be either a point or a comma; the default is a comma. You can furthermore use the package options german or comma if you want a comma or the options USenglish, english or point if you want a point to be used.

3 To do

• Extend syntax to support numbers like 1,7E−1 or 2,765 · 10^{5} in input and output (planned for v1.1).

• Formatted, customizable output with thousand separator etc. (planned for v1.1). There is an internal command \fp@regoutput in this version, but you should consider that it is likely to change in the next version. If you want to use it nevertheless, look at the implementation for a syntax description.

• User access to the comparison of registers (v1.1 or v1.2).

• A better concept for chosing the accuracy of the computations (v1.1 or v1.2).

• More operations like e^{x}, \sqrt{x}, sin x, ln x… (planned for v2.0 next year).