Algorithmic Solutions > LEDA > LEDA Guide > Number Types > Example Floating Point Filter, Interval, Integer

Example Floating Point Filter, Interval, and Integer Computation

The following program shows the usage of floatf, inteval, and integer. Function main() calls SignExample() for different values of Iter. In SignExample() the same computation is performed with integer, floatf, and interval, and the sign of the result is checked.

The resulting output shows that the sign in the integer computation is -1 for all values of Iter. This is the correct result. The sign is also -1 in the floatf computation for Iter<10 and NO_IDEA for larger values. The sign is -1 in the interval computation for Iter<22, and unknown for the rest.

#include <LEDA/numbers/floatf.h>
#include <LEDA/numbers/integer.h>
#include <LEDA/numbers/interval.h>

using namespace leda;

int SignExample(int Iter)
{
  integer A=1314157,B=3,RES;

  floatf af(A),bf(B),resf;
  interval_round_inside ai(A), bi(B),resi; 

  // integer computation
  int i; 
  for (i=1;i<Iter;i++) A=A*B;  
  B=A+1;
  RES=A-B;

  // floatf computation
  for (i=1;i<Iter;i++) af=af*bf;
  bf=af+floatf(1);
  resf=af-bf;

  // interval computation
  for (i=1;i<Iter;i++) ai=ai*bi;
  bi=ai+interval_round_inside(1);
  resi=ai-bi;

  if (Sign(resf)==NO_IDEA) std::cout<<"?";
  else std::cout<< Sign(resf); 
  cstd::out << "\t";

  if (resi.sign_is_known()) std::cout << resi.sign();
  else std::cout << "?";
  std::cout<<"\t\t";

  std::cout << sign(RES);

  std::cout << std::endl;

  return 0;
}

int main()
{
  std::cout << "Sign-Example" << std::endl;
  std::cout << "------------" << std::endl << std::endl;
  std::cout << "Iterations\tfloatf\tinterval\tinteger" << std::endl;
  for (int i=5; i<=30; i++)
  {
    std::cout << i << "\t\t";
    SignExample(i);
  }
  
  return 0;
}
Thanks for this example to Stefan Funke, MPI Informatik, Saarbrücken, Germany.

See also:

Floating Point Filter

Integers of Arbitrary Length

Interval Arithmetic


Manual Entries:

Manual Page Floating Point Filter

Manual Page Integers of Arbitrary Length

Manual Page Interval Arithmetic




Please send any suggestions, comments or questions to leda@algorithmic-solutions.com
© Copyright 2001-2003, Algorithmic Solutions Software GmbH