KNumber Class Reference

#include <knumber.h>

List of all members.


Detailed Description

Class that provides arbitrary precision numbers.

KNumber provides access to arbitrary precision numbers from within KDE.

KNumber is based on the GMP (GNU Multiprecision) library and provides transparent support to integer, fractional and floating point number. It contains rudimentary error handling, and also includes methods for converting the numbers to QStrings for output, and to read QStrings to obtain a KNumber.

The different types of numbers that can be represented by objects of this class will be described below:

  • NumType::SpecialType - This type represents an error that has occurred, e.g. trying to divide 1 by 0 gives an object that represents infinity.
  • NumType::IntegerType - The number is an integer. It can be arbitrarily large (restricted by the memory of the system).
  • NumType::FractionType - A fraction is a number of the form denominator divided by nominator, where both denominator and nominator are integers of arbitrary size.
  • NumType::FloatType - The number is of floating point type. These numbers are usually rounded, so that they do not represent precise values.
Author:
Klaus Niederkrueger <kniederk@math.uni-koeln.de>

Definition at line 65 of file knumber.h.


Public Types

 SpecialType
 IntegerType
 FractionType
 FloatType
 UndefinedNumber
 Infinity
 MinusInfinity
enum  NumType { SpecialType, IntegerType, FractionType, FloatType }
enum  ErrorType { UndefinedNumber, Infinity, MinusInfinity }

Public Member Functions

 KNumber (signed int num=0)
 KNumber (unsigned int num)
 KNumber (signed long int num)
 KNumber (unsigned long int num)
 KNumber (unsigned long long int num)
 KNumber (double num)
 KNumber (KNumber const &num)
 KNumber (QString const &num)
 ~KNumber ()
KNumber const & operator= (KNumber const &num)
NumType type (void) const
QString const toQString (int width=-1, int prec=-1) const
KNumber const abs (void) const
KNumber const sqrt (void) const
KNumber const cbrt (void) const
KNumber const integerPart (void) const
KNumber const power (KNumber const &exp) const
KNumber const operator+ (KNumber const &arg2) const
KNumber const operator- (void) const
KNumber const operator- (KNumber const &arg2) const
KNumber const operator * (KNumber const &arg2) const
KNumber const operator/ (KNumber const &arg2) const
KNumber const operator% (KNumber const &arg2) const
KNumber const operator & (KNumber const &arg2) const
KNumber const operator| (KNumber const &arg2) const
KNumber const operator<< (KNumber const &arg2) const
KNumber const operator>> (KNumber const &arg2) const
 operator bool (void) const
 operator signed long int (void) const
 operator unsigned long int (void) const
 operator unsigned long long int (void) const
 operator double (void) const
bool const operator== (KNumber const &arg2) const
bool const operator!= (KNumber const &arg2) const
bool const operator> (KNumber const &arg2) const
bool const operator< (KNumber const &arg2) const
bool const operator>= (KNumber const &arg2) const
bool const operator<= (KNumber const &arg2) const
KNumberoperator+= (KNumber const &arg)
KNumberoperator-= (KNumber const &arg)

Static Public Member Functions

static void setDefaultFloatOutput (bool flag)
static void setDefaultFractionalInput (bool flag)
static void setDefaultFloatPrecision (unsigned int prec)
static void setSplitoffIntegerForFractionOutput (bool flag)

Static Public Attributes

static KNumber const Zero
static KNumber const One
static KNumber const MinusOne
static KNumber const Pi
static KNumber const Euler

Member Enumeration Documentation

A KNumber that represents an error, i.e.

that is of type NumType::SpecialType can further distinguished:

  • ErrorType::UndefinedNumber - This is e.g. the result of taking the square root of a negative number or computing $ \infty - \infty $.
  • ErrorType::Infinity - Such a number can be e.g. obtained by dividing 1 by 0. Some further calculations are still allowed, e.g. $ \infty + 5 $ still gives $\infty$.
  • ErrorType::MinusInfinity - MinusInfinity behaves similarly to infinity above. It can be obtained by changing the sign of infinity.

Definition at line 107 of file knumber.h.

KNumber tries to provide transparent access to the following type of numbers:.

  • NumType::SpecialType - Some type of error has occurred, further inspection with KNumber::ErrorType
  • NumType::IntegerType - the number is an integer
  • NumType::FractionType - the number is a fraction
  • NumType::FloatType - the number is of floating point type

Definition at line 88 of file knumber.h.


Member Function Documentation

KNumber const KNumber::abs ( void   )  const

Compute the absolute value, i.e.

x.abs() returns the value

\[ \left\{\begin{array}{cl} x, & x \ge 0 \\ -x, & x < 0\end{array}\right.\]

This method works for $ x = \infty $ and $ x = -\infty $.

Definition at line 436 of file knumber.cpp.

KNumber const KNumber::cbrt ( void   )  const

Compute the cube root.

If x is an integer or a fraction, then x.cbrt() tries to compute the exact cube root. If the cube root is not a fraction, then a float is returned, but

WARNING: A float cube root is computed as a standard double that is later transformed back into a KNumber.

This method works for $ x = \infty $ giving $ \infty $, and for $ x = -\infty $ giving $ -\infty $.

Definition at line 446 of file knumber.cpp.

KNumber const KNumber::integerPart ( void   )  const

Truncates a KNumber to its integer type returning a number of type NumType::IntegerType.

If $ x = \pm\infty $, integerPart leaves the value unchanged, i.e. it returns $ \pm\infty $.

Definition at line 466 of file knumber.cpp.

void KNumber::setDefaultFloatOutput ( bool  flag  )  [static]

Set whether the output of numbers (with KNumber::toQString) should happen as floating point numbers or not.

This method has in fact only an effect on numbers of type NumType::FractionType, which can be either displayed as fractions or in decimal notation.

The default behavior is not to display fractions in floating point notation.

Definition at line 412 of file knumber.cpp.

void KNumber::setDefaultFloatPrecision ( unsigned int  prec  )  [static]

Set the default precision to be *at least* prec (decimal) digits.

All subsequent initialized floats will use at least this precision, but previously initialized variables are unaffected.

Definition at line 427 of file knumber.cpp.

void KNumber::setDefaultFractionalInput ( bool  flag  )  [static]

Set whether a number constructed from a QString should be initialized as a fraction or as a float, e.g.

"1.01" would be treated as 101/100, if this flag is set to true.

The default setting is false.

Definition at line 417 of file knumber.cpp.

void KNumber::setSplitoffIntegerForFractionOutput ( bool  flag  )  [static]

What a terrible method name!! When displaying a fraction, the default mode gives "nomin/denom".

With this method one can choose to display a fraction as "integer nomin/denom".

Examples: Default representation mode is 47/17, but if flag is true, then the result is 2 13/17.

Definition at line 422 of file knumber.cpp.

KNumber const KNumber::sqrt ( void   )  const

Compute the square root.

If $ x < 0 $ (including $ x=-\infty $), then x.sqrt() returns ErrorType::UndefinedNumber.

If x is an integer or a fraction, then x.sqrt() tries to compute the exact square root. If the square root is not a fraction, then a float with the default precision is returned.

This method works for $ x = \infty $ giving $ \infty $.

Definition at line 456 of file knumber.cpp.

QString const KNumber::toQString ( int  width = -1,
int  prec = -1 
) const

Return a QString representing the KNumber.

Parameters:
width This number specifies the maximal length of the output, before the method switches to exponential notation and does rounding. For negative numbers, this option is ignored.
prec This parameter controls the number of digits following the decimal point. For negative numbers, this option is ignored.

Definition at line 350 of file knumber.cpp.

KNumber::NumType KNumber::type ( void   )  const

Returns the type of the number, as explained in KNumber::NumType.

Definition at line 115 of file knumber.cpp.


The documentation for this class was generated from the following files:
KDE Home | KDE Accessibility Home | Description of Access Keys