/* Collection of code snippets by Arne Vajhøj */
/* (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) */
#ifndef FP_H
#define FP_H

#include <iostream>

using namespace std;

#include "int.h"

typedef int BOOL;

//**********************************************************************
//
//  Class definitions:
//
//    BigFP   : general big floating point
//    BigFP16 : 16 byte floating point (=128 bit)
//    BigFP32 : 32 byte floating point (=256 bit)
//
//**********************************************************************

// class for general big floating point
class BigFP {
   private:
      // data
      int Size;
      BOOL Sign;
      class BigInt Mantissa;
      long int Exponent;
      // normalize
      void Normalize();
      // dump
      void Dump(ostream& s) const;
   public:
      // constructor
      BigFP(int siz,int val = 0);
      BigFP(int siz,double val);
      BigFP(int siz,char *val);
      BigFP(const class BigFP& z);
      void operator=(const class BigFP& z);
      void operator=(const double z);
      // destructor
      ~BigFP();
      // operators
      friend class BigFP operator+(const class BigFP& z1,const class BigFP& z2);
      friend class BigFP operator-(const class BigFP& z1,const class BigFP& z2);
      friend class BigFP operator*(const class BigFP& z1,const class BigFP& z2);
      friend class BigFP operator/(const class BigFP& z1,const class BigFP& z2);
      friend BOOL operator==(const class BigFP& z1,const class BigFP& z2);
      friend BOOL operator!=(const class BigFP& z1,const class BigFP& z2);
      friend BOOL operator>(const class BigFP& z1,const class BigFP& z2);
      friend BOOL operator<(const class BigFP& z1,const class BigFP& z2);
      friend BOOL operator>=(const class BigFP& z1,const class BigFP& z2);
      friend BOOL operator<=(const class BigFP& z1,const class BigFP& z2);
      friend class BigFP operator+(const double z1,const class BigFP& z2);
      friend class BigFP operator-(const double z1,const class BigFP& z2);
      friend class BigFP operator*(const double z1,const class BigFP& z2);
      friend class BigFP operator/(const double z1,const class BigFP& z2);
      friend BOOL operator==(const double z1,const class BigFP& z2);
      friend BOOL operator!=(const double z1,const class BigFP& z2);
      friend BOOL operator>(const double z1,const class BigFP& z2);
      friend BOOL operator<(const double z1,const class BigFP& z2);
      friend BOOL operator>=(const double z1,const class BigFP& z2);
      friend BOOL operator<=(const double z1,const class BigFP& z2);
      friend class BigFP operator+(const class BigFP& z1,const double z2);
      friend class BigFP operator-(const class BigFP& z1,const double z2);
      friend class BigFP operator*(const class BigFP& z1,const double z2);
      friend class BigFP operator/(const class BigFP& z1,const double z2);
      friend BOOL operator==(const class BigFP& z1,const double z2);
      friend BOOL operator!=(const class BigFP& z1,const double z2);
      friend BOOL operator>(const class BigFP& z1,const double z2);
      friend BOOL operator<(const class BigFP& z1,const double z2);
      friend BOOL operator>=(const class BigFP& z1,const double z2);
      friend BOOL operator<=(const class BigFP& z1,const double z2);
      friend istream& operator>>(istream& s,class BigFP& z);
      friend ostream& operator<<(ostream& s,const class BigFP& z);
      // functions
      friend int trunc(const class BigFP& z);
      friend class BigFP inv(const class BigFP& z);
      friend class BigFP sqrt(const class BigFP& z);
      friend class BigFP pow(const class BigFP& z,int p);
      friend class BigFP pow(const class BigFP& z,double p);
      friend class BigFP exp_help(const class BigFP& z);
      friend class BigFP exp(const class BigFP& z);
      friend class BigFP log_help(const class BigFP& z);
      friend class BigFP log(const class BigFP& z,const class BigFP& logtwo);
      friend class BigFP log(const class BigFP& z);
      friend class BigFP pi_help(int n);
      friend class BigFP cos_help(const class BigFP& z);
      friend class BigFP cos(const class BigFP& z);
      friend class BigFP sin_help(const class BigFP& z);
      friend class BigFP sin(const class BigFP& z);
      friend class BigFP tan(const class BigFP& z);
   };

// class for 16 byte floating point (=128 bit)
class BigFP16 : public BigFP {
   public:
      // constructor
      BigFP16(double val = 0);
      void operator=(const class BigFP& z);
      // destructor
      ~BigFP16();
   };

// class for 32 byte floating point (=256 bit)
class BigFP32 : public BigFP {
   public:
      // constructor
      BigFP32(double val = 0);
      void operator=(const class BigFP& z);
      // destructor
      ~BigFP32();
   };

#endif // FP_H
