Classes | Functions
core/vnl/vnl_decnum.h File Reference

Infinite precision numbers with decimal arithmetic. More...

#include <vcl_iostream.h>
#include <vcl_string.h>

Go to the source code of this file.

Classes

class  vnl_decnum

Functions

vcl_ostream & operator<< (vcl_ostream &s, vnl_decnum const &r)
 decimal output.
vcl_istream & operator>> (vcl_istream &s, vnl_decnum &r)
 decimal input.
vnl_decnum ceil (vnl_decnum const &x)
vnl_decnum floor (vnl_decnum const &x)
vnl_decnum pow (vnl_decnum const &x, unsigned long p)
vnl_decnum vnl_math_abs (vnl_decnum const &x)
vnl_decnum vnl_math_sqr (vnl_decnum const &x)
vnl_decnum vnl_math_cube (vnl_decnum const &x)
vnl_decnum vnl_math_squared_magnitude (vnl_decnum const &x)
bool vnl_math_isnan (vnl_decnum const &x)
bool vnl_math_isfinite (vnl_decnum const &x)
vnl_decnum vnl_math_max (vnl_decnum const &x, vnl_decnum const &y)
vnl_decnum vnl_math_min (vnl_decnum const &x, vnl_decnum const &y)
int vnl_math_sgn (vnl_decnum x)
int vnl_math_sgn0 (vnl_decnum x)

Detailed Description

Infinite precision numbers with decimal arithmetic.

The vnl_decnum class implements infinite precision integers and floating point numbers, with all necessary arithmetic, by using a dynamically allocated string of decimals for the mantissa. Implicit conversion to the system defined types short, int, and long is supported by overloaded operator member functions and explicit conversions from these types is available through constructors; additionally, an implicit "double" constructor is available (mainly for use with vnl_decnum_traits), without any guarantees.

Addition and subtraction operators are performed by simple decimal arithmetic with checks for carry flag propagation. The multiplication, division, and remainder operations utilize the naive, commonly known decimal algorithms. Beware that these could be considerably slower than both the operations on built-in integer types and those implemented in the class vnl_bignum. On the other hand, the I/O could be faster since no decimal <==> binary conversion needs to be performed. Rounding errors due to imprecise internal representation can never be made with vnl_decnum, so consider using this class if decimal I/O and precise arithmetic are crucial.

Since the internal representation is decimal, there is no overhead for converting between a vnl_decnum and a decimal character string representation. Hence, there is a constructor from std::string, there are both << and >> operators, and a cast method to std::string, all of them essentially no-ops.

Decimal point notation is only supported on input; internally, only the scientific notation (mantissa and exponent) is used, with integer mantissa (and, on output, without "e exponent" for integers). Beware that, even for non-integers, the division and modulo operators are logically speaking integer operations on the mantissa. E.g., the result of 42e-3 / 30e-4 is 1e1, while 42e-3 / 3e-3 has as outcome 14, even though numerically 30e-4 == 3e-3. But then also 42e-3 % 30e-4 returns 12e-3 while 42e-3 % 3e-3 returns 0. Both results are compatible with (a/b) * b + (ab) = a which is generally true for other integral types as well.

The difference between using e.g. 1000 and 10e2 is essentially similar to the use of the "P" format in Cobol: in the former case, a precision of four digits is kept, while in the latter only two digits are present (with two zeros implicitly there). Arithmetic is done with the available precision only, which explains the outcomes of operator/ and operator%. A similar logic holds for the difference between e.g. 1000e-2 and 10.

When none of the used vnl_decnum constructor values have an "e" (or when all constructors that you use in your program are on integers), no "e" is ever introduced (except when explicitly using the method compactify()). In that case, vnl_decnum really behaves as an integral type, very similar to vnl_bignum, and as an extension of int and long.

In addition to (arbitrary precision) numbers, vnl_decnum also represents +Infinity, -Infinity, and Nan. Their internal representations are: (sign,data,exp) = ('+','Inf',0L), ('-','Inf',0L'), and (' ','NaN',0L). Arithmetic with these entities is as expected: e.g. 1/0=+Inf, 1/Inf=0, -Inf < n < +Inf, 1+Inf=+Inf, 0/0=NaN, NaN op n = NaN.

See also:
vnl_bignum
Author:
Peter Vanroose, ABIS Leuven
Date:
August 2011
    Modifications
     15 Aug. 2011 - Peter Vanroose - added member exp_ (and non-integer arith)
     16 Aug. 2011 - Peter Vanroose - added Inf & NaN and constructor from double
   

Definition in file vnl_decnum.h.


Function Documentation

vnl_decnum ceil ( vnl_decnum const &  x) [inline]

Definition at line 395 of file vnl_decnum.h.

vnl_decnum floor ( vnl_decnum const &  x) [inline]

Definition at line 396 of file vnl_decnum.h.

vcl_ostream & operator<< ( vcl_ostream &  s,
vnl_decnum const &  r 
) [inline]

decimal output.

Definition at line 388 of file vnl_decnum.h.

vcl_istream& operator>> ( vcl_istream &  s,
vnl_decnum r 
)

decimal input.

Definition at line 411 of file vnl_decnum.cxx.

vnl_decnum pow ( vnl_decnum const &  x,
unsigned long  p 
) [inline]

Definition at line 397 of file vnl_decnum.h.

vnl_decnum vnl_math_abs ( vnl_decnum const &  x) [inline]

Definition at line 399 of file vnl_decnum.h.

vnl_decnum vnl_math_cube ( vnl_decnum const &  x) [inline]

Definition at line 401 of file vnl_decnum.h.

bool vnl_math_isfinite ( vnl_decnum const &  x) [inline]

Definition at line 404 of file vnl_decnum.h.

bool vnl_math_isnan ( vnl_decnum const &  x) [inline]

Definition at line 403 of file vnl_decnum.h.

vnl_decnum vnl_math_max ( vnl_decnum const &  x,
vnl_decnum const &  y 
) [inline]

Definition at line 405 of file vnl_decnum.h.

vnl_decnum vnl_math_min ( vnl_decnum const &  x,
vnl_decnum const &  y 
) [inline]

Definition at line 406 of file vnl_decnum.h.

int vnl_math_sgn ( vnl_decnum  x) [inline]

Definition at line 407 of file vnl_decnum.h.

int vnl_math_sgn0 ( vnl_decnum  x) [inline]

Definition at line 408 of file vnl_decnum.h.

vnl_decnum vnl_math_sqr ( vnl_decnum const &  x) [inline]

Definition at line 400 of file vnl_decnum.h.

vnl_decnum vnl_math_squared_magnitude ( vnl_decnum const &  x) [inline]

Definition at line 402 of file vnl_decnum.h.