This section describes briefly what interval arithmetic is, its implementation in Cgal, and its possible use by geometric programs. The main reason for having interval arithmetic in Cgal is its integration into the filtered robust and fast predicates scheme, but we also provide a number type so that you can use it separately if you find any use for it, such as interval analysis, or to represent data with tolerance...
The purpose of interval arithmetic is to provide an efficient way to bound the roundoff errors made by floating point computations. You can choose the behavior of your program depending on these errors. You can find more theoretical information on this topic in [BBP01].
Interval arithmetic is a large concept and we will only consider here a simple arithmetic based on intervals whose bounds are doubles. So each variable is an interval representing any value inside the interval. All arithmetic operations (+, , *, /, √, square(), min(), max() and abs()) on intervals preserve the inclusion. This property can be expressed by the following formula (x and y are reals, X and Y are intervals, OP is an arithmetic operation):
∀ x ∈ X, ∀ y ∈ Y, (x OP y) ∈ (X OP Y) 
For example, if the final result of a sequence of arithmetic operations is an interval that does not contain zero, then you can safely determine its sign.
#include <CGAL/Interval_nt.h>
The template parameter Protected is a Boolean parameter, which defaults to true. It provides a way to select faster computations by avoiding rounding mode switches, at the expense of more care to be taken by the user (see below). The default value, true, is the safe way, and takes care of proper rounding mode changes. When specifying false, the user has to take care about setting the rounding mode towards plus infinity before doing any computations with the interval class. He can do so using the Protect_FPU_rounding class for example.

 The type of the bounds of the interval. 
 
 The type of the exceptions raised when uncertain comparisons are performed. 
 
A type whose default constructor and destructor allow
to protect a block of code from FPU rounding modes necessary for the
computations with Interval_nt<false>. It does nothing for
Interval_nt<true>. It is implemented as Protect_FPU_rounding<!Protected>.

 
introduces the interval [i;i].
 
 
introduces the interval [d;d].
 
 
introduces the interval [i;s].
 
 
introduces the interval [p.first;p.second].

All functions required by a class to be considered as a Cgal number type (see 4) are present, as well as the utility functions, sometimes with a particular semantic which is described below. There are also a few additional functions.

 returns [∞;+∞] when the denominator contains 0. 

 returns [0;√upper_bound(I)] when only the lower bound is negative (expectable case with roundoff errors), and is unspecified when the upper bound also is negative (unexpected case). 

 returns the middle of the interval, as a double approximation of the interval. 

 returns the lower bound of the interval. 

 returns the upper bound of the interval. 

 returns whether both bounds are equal. 

 returns whether both intervals have the same bounds. 

 returns whether both intervals have a non empty intersection. 
The comparison operators (<, >, <=, >=, ==, !=, sign() and compare()) have the following semantic: it is the intuitive one when for all couples of values in both intervals, the comparison is identical (case of nonoverlapping intervals). This can be expressed by the following formula (x and y are reals, X and Y are intervals, OP is a comparison operator):
( ∀ x ∈ X, ∀ y ∈ Y, (x OP y) = true) ⇒ (X OP Y) = true 
( ∀ x ∈ X, ∀ y ∈ Y, (x OP y) = false) ⇒ (X OP Y) =false 
Otherwise, the comparison is not safe, and we specify this by returning a type encoding this uncertainty, namely using Uncertain<bool> or Uncertain<Sign>, which can be probed for uncertainty explicitely, and which has a conversion to the normal type (e.g. bool) which throws an exception when the conversion is not certain. Note that each failed conversion increments a profiling counter (see CGAL_PROFILE), and then throws the exception of type unsafe_comparison.



















 




 This typedef (at namespace CGAL scope) exists for backward compatibility, as well as removing the need to remember the Boolean value for the template parameter. 
The operations on Interval_nt with the default parameter true, are automatically protected against rounding modes, and are thus slower than those on Interval_nt_advanced, but easier to use. Users that need performance are encouraged to use Interval_nt_advanced instead.
Changing the rounding mode affects all floating point computations, and might cause problems with parts of your code, or external libraries (even Cgal), that expect the rounding mode to be the default (round to the nearest).
We provide two interfaces to change the rounding mode. The first one is to use a protector object whose default constructor and destructor will take care of changing the rounding mode. The protector is implemented using Protect_FPU_rounding.
The second one is the following detailed set of functions :

 The type used by the following functions to deal with rounding modes. This is usually an int. 

 sets the rounding mode to R. 

 returns the current rounding mode. 

 sets the rounding mode to R and returns the old one. 
The macros CGAL_FE_TONEAREST, CGAL_FE_TOWARDZERO, CGAL_FE_UPWARD and CGAL_FE_DOWNWARD are the values corresponding to the rounding modes.
Protecting an area of code that uses operations on the class Interval_nt_advanced can be done in the following way:
{ Interval_nt_advanced::Protector P; ... // The code to be protected. }
The basic idea is to use the directed rounding modes specified by the IEEE 754 standard, which are implemented by almost all processors nowadays. It states that you have the possibility, concerning the basic floating point operations (+,,*,/,√) to specify the rounding mode of each operation instead of using the default, which is set to 'round to the nearest'. This feature allows us to compute easily on intervals. For example, to add the two intervals [a.i;a.s] and [b.i;b.s], compute c.i=a.i+b.i rounded towards minus infinity, and c.s=a.s+b.s rounded towards plus infinity, and the result is the interval [c.i;c.s]. This method can be extended easily to the other operations.
The problem is that we have to change the rounding mode very often, and the functions of the C library doing this operation are slow and not portable. That's why assembly versions are used as often as possible. Another trick is to store the opposite of the lower bound, instead of the lower bound itself, which allows us to never change the rounding mode inside simple operations. Therefore, all basic operations, which are in the class Interval_nt_advanced assume that the rounding mode is set to 'round to infinity', and everything works with this correctly set.
So, if the user needs the speed of Interval_nt_advanced, he must take care of setting the rounding mode to 'round to infinity' before each block of operations on this number type. And if other operations might be affected by this, he must take care to reset it to 'round to the nearest' before they are executed.
Notes: