<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
   "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>

<meta http-equiv="Content-Type" content="text/html;charset=US-ASCII">

<style type="text/css">

body {
  color: #000000;
  background-color: #FFFFFF;
}

del {
  text-decoration: line-through;
  color: #8B0040;
}
ins {
  text-decoration: underline;
  color: #005100;
}

p.example {
  margin: 2em;
}
pre.example {
  margin: 2em;
}
div.example {
  margin: 2em;
}

code.extract {
  background-color: #F5F6A2;
}
pre.extract {
  margin: 2em;
  background-color: #F5F6A2;
  border: 1px solid #E1E28E;
}

p.function {
}

p.attribute {
  text-indent: 3em;
}

blockquote.std {
  color: #000000;
  background-color: #F1F1F1;
  border: 1px solid #D1D1D1;
  padding: 0.5em;
}

blockquote.stddel {
  text-decoration: line-through;
  color: #000000;
  background-color: #FFEBFF;
  border: 1px solid #ECD7EC;
  padding: 0.5em;
}

blockquote.stdins {
  text-decoration: underline;
  color: #000000;
  background-color: #C8FFC8;
  border: 1px solid #B3EBB3;
  padding: 0.5em;
}

table {
  border: 1px solid black;
  border-spacing: 0px;
  border-collapse: collapse;
  margin-left: auto;
  margin-right: auto;
}
th {
  text-align: left;
  vertical-align: top;
  padding: 0.2em;
  border: 1px solid black;
}
td {
  text-align: left;
  vertical-align: top;
  padding: 0.2em;
  border: 1px solid black;
}

</style>


<title>An Outline of a C++ Numbers Technical Specification</title>
</head>
<body>
<h1>An Outline of a C++ Numbers Technical Specification</h1>

<p>
ISO/IEC JTC1 SC22 WG21 P0101R0 - 2015-09-27
</p>

<p>
Lawrence Crowl, Lawrence@Crowl.org
</p>

<p>
<a href="#Introduction">Introduction</a><br>
&nbsp;&nbsp;&nbsp;&nbsp;<a href="#Principles">Design Principles</a><br>
<a href="#Builtin">Built-in Types</a><br>
&nbsp;&nbsp;&nbsp;&nbsp;<a href="#DecimalFloat">Decimal Floating Point</a><br>
&nbsp;&nbsp;&nbsp;&nbsp;<a href="#Parametric">Parametric Aliases</a><br>
&nbsp;&nbsp;&nbsp;&nbsp;<a href="#BuiltinOther">Other Built-in Types</a><br>
<a href="#Utilities">Utilities</a><br>
&nbsp;&nbsp;&nbsp;&nbsp;<a href="#OverflowArith">Overflow-Detecting Arithmetic</a><br>
&nbsp;&nbsp;&nbsp;&nbsp;<a href="#WideArith">Double-Wide Arithmetic</a><br>
&nbsp;&nbsp;&nbsp;&nbsp;<a href="#Multiprecision">Multiprecision Arithmetic</a><br>
&nbsp;&nbsp;&nbsp;&nbsp;<a href="#UtilityOther">Other Utilities</a><br>
<a href="#RoundingOverflow">Rounding and Overflow</a><br>
<a href="#Unbounded">Unbounded Types</a><br>
&nbsp;&nbsp;&nbsp;&nbsp;<a href="#UnboundInteger">Unbounded Integer</a><br>
&nbsp;&nbsp;&nbsp;&nbsp;<a href="#UnboundRational">Unbounded Rational</a><br>
&nbsp;&nbsp;&nbsp;&nbsp;<a href="#UnBoundOther">Other Unbounded Types</a><br>
<a href="#Bounded">Bounded Types</a><br>
&nbsp;&nbsp;&nbsp;&nbsp;<a href="#BoundInteger">Bounded Integers</a><br>
&nbsp;&nbsp;&nbsp;&nbsp;<a href="#BoundFixed">Bounded Fixed-Point Types</a><br>
&nbsp;&nbsp;&nbsp;&nbsp;<a href="#BoundOther">Other Bounded Types</a><br>
<a href="#Conversion">Conversion</a><br>
</p>


<h2><a name="Introduction">Introduction</a></h2>

<p>
ISO SC22/WG21/SG6 is the numerics study group of the C++ standards committee.
SG6 has been working towards
the definition of a set of number types
and associated functions
that enable
</p>
<ul>

<li><p>
the interchange of additional numeric values
between software components with different authors,
</p></li>

<li><p>
the production of software
that is less vulnerable (if not immune) to overflow and rounding errors and
</p></li>

<li><p>
the efficient and effective implementation of additional numeric types.
</p></li>

</ul>


<h3><a name="Principles">Design Principles</a></h3>

<ul>

<li><p>
Provide a consistent vocabulary.
</p></li>

<li><p>
Expose sound "building-block" abstractions.
</p></li>

<li><p>
Provide a mechanism for conversion
that does not require <var>n</var><sup>2</sup> operations
or coordination between independent developers.
</p></li>

<li><p>
Strive for efficient parameter passing.
Pass types known to be primitive or very small by value.
Pass types known to be indirect or very large by reference.
</p></li>

<li><p>
Match aliasing behavior to the users.
Handle potential parameter aliasing internally
if the type is expected to be widely used.
If the type serves mostly as a narrow-audience implementation tool,
handle aliasing externally.
</p></li>

<li><p>
Prefer run-time efficiency over compile-time efficiency.
</p></li>

<li><p>
Only define functions to be constexpr if we need them constexpr today.
It is easy to add constexpr later, but impossible to remove it.
</p></li>

<li><p>
Prefer general-use types that can detect and handle exceptional conditions.
</p></li>

</ul>


<h2><a name="Builtin">Built-in Types</a></h2>

<p>
We add or reference built-in types when necessary.
</p>


<h3><a name="DecimalFloat">Decimal Floating Point</a></h3>

<p>
Decimal floating-point types already exist.
We anticipate some refinement of them.
See
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3871.html">N3871
Proposal to Add Decimal Floating Point Support to C++</a>.
</p>


<h3><a name="Parametric">Parametric Aliases</a></h3>

<p>
Computing the needed size of a type need support.
See
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/p0102r0.html">P0102R0
C++ Parametric Number Type Aliases</a>.
</p>


<h3><a name="BuiltinOther">Other Built-in Types</a></h3>

<p>
There has been discussion of the following types,
but as yet no papers.
</p>

<ul>

<li><p>
non-modular unsigned integers
</p></li>

<li><p>
binary floating-point intervals
</p></li>

<li><p>
binary floating-point rationals
</p></li>

<li><p>
binary log representation of reals
</p></li>

</ul>


<h2><a name="Utilities">Utilities</a></h2>

<p>
The implementation of numeric types
will often have common implementation components.
It would be nice to share them.
</p>


<h3><a name="OverflowArith">Overflow-Detecting Arithmetic</a></h3>

<p>
Detecting overflow has existing hardware support
that is not available to programmers.
See
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/p0103r0.html">P0103R0
Overflow-Detecting and Double-Wide Arithmetic Operations</a>.
</p>


<h3><a name="WideArith">Double-Wide Arithmetic</a></h3>

<p>
Multi-word operations are most efficient when hardware-supported
double-wide operations are available to programmers.
See <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/p0103r0.html">P0103R0
Overflow-Detecting and Double-Wide Arithmetic Operations</a>.
</p>


<h3><a name="Multiprecision">Multiprecision Arithmetic</a></h3>

<p>
Multi-word operations and types are commonly needed.
See
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/p0104r0.html">P0104R0
Multi-Word Integer Operations and Types</a>.
</p>


<h3><a name="UtilityOther">Other Utilities</a></h3>

<p>
We need some bitwise utilities along the lines of
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n3864.html">N3864
A constexpr bitwise operations library for C++</a>.
</p>

<p>
While not strictly related to numbers,
a library for units of measure would be welcome.
</p>


<h2><a name="RoundingOverflow">Rounding and Overflow</a></h2>

<p>
The primary problem with existing C++ number types
is the very poor control over overflow and rounding.
We need to put such control in the hands of programmers.
See <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/p0105r0.html">P0105R0
Rounding and Overflow in C++</a>.
</p>


<h2><a name="Unbounded">Unbounded Types</a></h2>

<p>
Unbounded types allocate memory as necessary
to maintain a representation of true operation results.
We may need an allocator interface for them.
</p>


<h3><a name="UnboundInteger">Unbounded Integer</a></h3>

<p>
The classic bignum has been too long in coming to C++.
See
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n4038.html">N4038
Proposal for Unbounded-Precision Integer Types</a>.
</p>

<p>
Can we know highest bit set in integers?
</p>


<h3><a name="UnboundRational">Unbounded Rational</a></h3>

<p>
Unbound rational numbers is useful in computational geometry.
See
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3611.html">N3611
A Rational Number Library for C++</a>.
</p>

<p>
Rational numbers can grow pretty quickly without reduction,
but reduction is expensive.
The tradeoff could be managed
either by checking the size
or by adding a count of operations since last reduction.
</p>


<h3><a name="UnBoundOther">Other Unbounded Types</a></h3>

<p>
We do not yet have a paper for an unbound binary-floating point.
It would be useful on its own
and as a generalization of both fixed-bound floating-point
and fixed-bound fixed-point.
Rounding would still take place with unbound binary-floating point,
but only on division and only within normal error bounds.
This generally means a doubling in representation size after division.
An explicit resolution reduction would be desireable.
</p>


<h2><a name="Bounded">Bounded Types</a></h2>

<p>
Bounds are specified in number of data bits, i.e. ignoring sign.
</p>


<h3><a name="BoundInteger">Bounded Integers</a></h3>

<p>
Integers are bound by their range.
They can overflow.
See
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/p0106r0.html">P0106R0
C++ Binary Fixed-Point Arithmetic</a>.
</p>


<h3><a name="BoundFixed">Bounded Fixed-Point Types</a></h3>

<p>
Integers are bound by their range and resolution.
They can both overflow and require rounding.
See
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/p0106r0.html">P0106R0
C++ Binary Fixed-Point Arithmetic</a>.
</p>


<h3><a name="BoundOther">Other Bounded Types</a></h3>

<p>
We have discussed, but do not have papers for:
</p>

<ul>

<li><p>
A binary rational numberis bound by
the range of the numerator and the range of the denominator.
It has both overflow and rounding.
Each operation would be exact until the representation size is reached,
and then would approximate using a near representation.
has overflow and rounding
</p></li>

<li><p>
A bounded decimal fixed-point would be much like a bounded binary fixed-point.
</p></li>

</ul>


<h2><a name="Conversion">Conversion</a></h2>

<p>
Conversion is not a new problem.
See
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1879.htm">N1879
A proposal to add a general purpose ranged-checked numeric_cast&lt;&gt; (Revision 1)</a>.
However,
many new number types requires a practical conversion algorithm.
Using converting through intermedate unbounded types
provides that algorithm.
See
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/p0105r0.html">P0105R0
Rounding and Overflow in C++</a>.
</p>


</body>
</html>
