Dlamch.java

package org.mklab.sdpj.gpack.lapackwrap;

import java.util.LinkedList;
import java.util.List;

import org.mklab.nfc.matrix.ComplexNumericalMatrix;
import org.mklab.nfc.matrix.RealNumericalMatrix;
import org.mklab.nfc.scalar.ComplexNumericalScalar;
import org.mklab.nfc.scalar.RealNumericalScalar;
import org.mklab.sdpj.gpack.blaswrap.BLAS;
import org.mklab.sdpj.tool.Tools;


/**
 * @author takafumi
   * @param <RS> type of real scalar
   * @param <RM> type of real matrix
   * @param <CS> type of complex scalar
   * @param <CM> type of complex Matrix
 */
public class Dlamch<RS extends RealNumericalScalar<RS, RM, CS, CM>, RM extends RealNumericalMatrix<RS, RM, CS, CM>, CS extends ComplexNumericalScalar<RS, RM, CS, CM>, CM extends ComplexNumericalMatrix<RS, RM, CS, CM>> {

  /** どのメソッドを通過したか表示するフラグ */
  private boolean showDetailFlag = false;
  /** 一度実行されたらfalse */
  private boolean isFirst = true;
  /** */
  private RS base;
  /** */
  private int beta;
  /** */
  private RS emin;
  /** */
  private RS prec;
  /** */
  private RS emax;
  /** */
  private int imin;
  /** */
  private int imax;
  /** */
  private RS rmin;
  /** */
  private RS rmax;
  /** */
  private RS t;
  /** */
  private RS rmach;
  /** */
  private RS sfmin;
  /** */
  private int it;
  /** */
  private RS rnd;
  /** */
  private RS eps;
  /** */
  private boolean lrnd;
  /** */
  private boolean ieee1;

  /*  -- LAPACK auxiliary routine (version 3.0) --
  Univ. of Tennessee, Univ. of California Berkeley, NAG Ltd.,
  Courant Institute, Argonne National Lab, and Rice University
  October 31, 1992


  Purpose
  =======

  DLAMCH determines double precision machine parameters.

  Arguments
  =========

  CMACH   (input) CHARACTER*1
       Specifies the value to be returned by DLAMCH:
       = 'E' or 'e',   DLAMCH := eps
       = 'S' or 's ,   DLAMCH := sfmin
       = 'B' or 'b',   DLAMCH := base
       = 'P' or 'p',   DLAMCH := eps*base
       = 'N' or 'n',   DLAMCH := t
       = 'R' or 'r',   DLAMCH := rnd
       = 'M' or 'm',   DLAMCH := emin
       = 'U' or 'u',   DLAMCH := rmin
       = 'L' or 'l',   DLAMCH := emax
       = 'O' or 'o',   DLAMCH := rmax

       where

       eps   = relative machine precision
       sfmin = safe minimum, such that 1/sfmin does not overflow
       base  = base of the machine
       prec  = eps*base
       t     = number of (base) digits in the mantissa
       rnd   = 1.0 when rounding occurs in addition, 0.0 otherwise
       emin  = minimum exponent before (gradual) underflow
       rmin  = underflow threshold - base**(emin-1)
       emax  = largest exponent before overflow
       rmax  = overflow threshold  - (base**emax)*(1-eps)

  =====================================================================
  */
  /**
   * @param cmach cmach
   * @param unit RS
   * @return result
   */
  private RS dlamch(String cmach, RS unit) {
    if (this.showDetailFlag) {
      System.out.println("in>>dlamch"); //$NON-NLS-1$
    }

    //final RS unit = Tools.getUnitNumber();

    if (this.isFirst) {
      dlamc2(unit);

      this.base = unit.create(this.beta);
      this.t = unit.create(this.it);

      if (this.lrnd) {
        this.rnd = unit.createUnit();
        this.eps = this.base.power(1 - this.it).divide(2);
      } else {
        this.rnd = unit.createZero();
        this.eps = this.base.power(1 - this.it);
      }

      this.prec = this.eps.multiply(this.base);
      this.emin = unit.create(this.imin);
      this.emax = unit.create(this.imax);
      this.sfmin = this.rmin;
      final RS small = this.rmax.inverse();

      if (small.isGreaterThanOrEquals(this.sfmin)) {
        /* Use SMALL plus a bit, to avoid the possibility of rounding
         * causing overflow when computing  1/sfmin.
         */
        this.sfmin = small.multiply(this.eps.add(1));
      }
      
      this.isFirst = false;
    }

    if (BLAS.lsame(cmach, "E")) { //$NON-NLS-1$
      return this.eps;
    } else if (BLAS.lsame(cmach, "S")) { //$NON-NLS-1$
      return this.sfmin;
    } else if (BLAS.lsame(cmach, "B")) { //$NON-NLS-1$
      return this.base;
    } else if (BLAS.lsame(cmach, "P")) { //$NON-NLS-1$
      return this.prec;
    } else if (BLAS.lsame(cmach, "N")) { //$NON-NLS-1$
      return this.t;
    } else if (BLAS.lsame(cmach, "R")) { //$NON-NLS-1$
      return this.rnd;
    } else if (BLAS.lsame(cmach, "M")) { //$NON-NLS-1$
      return this.emin;
    } else if (BLAS.lsame(cmach, "U")) { //$NON-NLS-1$
      return this.rmin;
    } else if (BLAS.lsame(cmach, "L")) { //$NON-NLS-1$
      return this.emax;
    } else if (BLAS.lsame(cmach, "O")) { //$NON-NLS-1$
      return this.rmax;
    }

    throw new IllegalArgumentException();

    //    final RS ret_val = this.rmach;
    //    if (this.showDetailFlag) {
    //      System.out.println("out>>dlamch"); //$NON-NLS-1$
    //    }
    //    return ret_val;
  }

  /*  -- LAPACK auxiliary routine (version 3.0) --
  Univ. of Tennessee, Univ. of California Berkeley, NAG Ltd.,
  Courant Institute, Argonne National Lab, and Rice University
  October 31, 1992


  Purpose
  =======

  DLAMC1 determines the machine parameters given by BETA, T, RND, and
  IEEE1.

  Arguments
  =========

  BETA    (output) INTEGER
       The base of the machine.

  T       (output) INTEGER
       The number of ( BETA ) digits in the mantissa.

  RND     (output) LOGICAL
       Specifies whether proper rounding  ( RND = .TRUE. )  or
       chopping  ( RND = .FALSE. )  occurs in addition. This may not

       be a reliable guide to the way in which the machine performs

       its arithmetic.

  IEEE1   (output) LOGICAL
       Specifies whether rounding appears to be done in the IEEE
       'round to nearest' style.

  Further Details
  ===============

  The routine is based on the routine  ENVRON  by Malcolm and
  incorporates suggestions by Gentleman and Marovich. See

  Malcolm M. A. (1972) Algorithms to reveal properties of
     floating-point arithmetic. Comms. of the ACM, 15, 949-951.

  Gentleman W. M. and Marovich S. B. (1974) More on algorithms
     that reveal properties of floating point arithmetic units.
     Comms. of the ACM, 17, 276-277.

  =====================================================================
  */
  /**
   * @param beta beta
   * @param t t
   * @param unit unit
   * @return result
   */
  private int[] dlamc1(int beta, int t, RS unit) {
    if (this.showDetailFlag) {
      System.out.println("in>>dlamc1"); //$NON-NLS-1$
    }

    //final RS unit = (E)Tools.getUnitNumber();

    int lbeta = 0;
    int lt = 0;

    boolean first = true;
    if (first) {
      first = false;
      RS one = unit.createUnit();

      /* 
       * LBETA, LIEEE1, LT and LRND are the local values of BETA, IEEE1, T and RND.
       *
       *  Throughout this routine  we use the function  DLAMC3  to ensure
       *  that relevant values are  stored and not held in registers, or
       *  are not affected by optimizers.
       *  Compute  a = 2.0**m  with the  smallest positive integer m such
       *  that fl( a + 1.0 ) = a.
       */
      RS a = unit.createUnit();
      RS c = unit.createUnit();

      while (c.equals(one)) {
        a = a.multiply(2);
        c = dlamc3(a, one);
        c = dlamc3(c, a.unaryMinus());
      }

      /*  Now compute  b = 2.0**m  with the smallest positive integer m such that fl( a + b ) .gt. a. */
      RS b = unit.createUnit();
      c = dlamc3(a, b);

      while (c.equals(a)) {
        b = b.multiply(2);
        c = dlamc3(a, b);
      }

      /* 
       * Now compute the base.  a and c  are neighbouring floating point
       * numbers  in the  interval  ( beta**t, beta**( t + 1 ) )  and so
       * their difference is beta. Adding 0.25 to c is to ensure that it
       * is truncated to beta and not ( beta - 1 ).
       */
      RS qtr = one.divide(4);
      RS savec = c;
      c = dlamc3(c, a.unaryMinus());
      lbeta = Tools.NumericalScalarToInt((c.add(qtr)));

      /* 
       * Now determine whether rounding or chopping occurs,
       * by adding a bit less than beta/2 and a bit more than
       * beta/2 to a.
       */
      b = unit.create(lbeta);
      RS f = dlamc3(b.divide(2), b.unaryMinus().divide(100));
      c = dlamc3(f, a);
      if (c.equals(a)) {
        this.lrnd = true;
      } else {
        this.lrnd = false;
      }

      f = dlamc3(b.divide(2), b.divide(100));
      c = dlamc3(f, a);
      if (this.lrnd && c.equals(a)) {
        this.lrnd = false;
      }

      /*
       * Try and decide whether rounding is done in the  IEEE  'round to
       * nearest' style. B/2 is half a unit in the last place of the two
       * numbers A and SAVEC. Furthermore, A is even, i.e. has lastbit
       * zero, and SAVEC is odd. Thus adding B/2 to A should not change
       * A, but adding B/2 to SAVEC should change SAVEC.
       */
      RS t1 = dlamc3(b.divide(2), a);
      RS t2 = dlamc3(b.divide(2), savec);
      this.ieee1 = t1.equals(a) && t2.isGreaterThan(savec) && this.lrnd;

      /* 
       * Now find  the  mantissa, t.  It should  be the  integer part of
       * log to the base beta of a,  however it is safer to determine t
       * by powering.  So we find t as the smallest positive integer for
       * which fl( beta**t + 1.0 ) = 1.0.
       */
      lt = 0;
      a = unit.createUnit();
      c = unit.createUnit();

      while (c.equals(one)) {
        ++lt;
        a = a.multiply(lbeta);
        c = dlamc3(a, one);
        c = dlamc3(c, a.unaryMinus());
      }
    }

    beta = lbeta;
    t = lt;

    if (this.showDetailFlag) {
      System.out.println("out>>dlamc1"); //$NON-NLS-1$
    }
    return new int[] {beta, t};
  }

  /*  -- LAPACK auxiliary routine (version 3.0) --
  Univ. of Tennessee, Univ. of California Berkeley, NAG Ltd.,
  Courant Institute, Argonne National Lab, and Rice University
  October 31, 1992


  Purpose
  =======

  DLAMC2 determines the machine parameters specified in its argument
  list.

  Arguments
  =========

  BETA    (output) INTEGER
       The base of the machine.

  T       (output) INTEGER
       The number of ( BETA ) digits in the mantissa.

  RND     (output) LOGICAL
       Specifies whether proper rounding  ( RND = .TRUE. )  or
       chopping  ( RND = .FALSE. )  occurs in addition. This may not

       be a reliable guide to the way in which the machine performs

       its arithmetic.

  EPS     (output) DOUBLE PRECISION
       The smallest positive number such that

          fl( 1.0 - EPS ) .LT. 1.0,

       where fl denotes the computed value.

  EMIN    (output) INTEGER
       The minimum exponent before (gradual) underflow occurs.

  RMIN    (output) DOUBLE PRECISION
       The smallest normalized number for the machine, given by
       BASE**( EMIN - 1 ), where  BASE  is the floating point value

       of BETA.

  EMAX    (output) INTEGER
       The maximum exponent before overflow occurs.

  RMAX    (output) DOUBLE PRECISION
       The largest positive number for the machine, given by
       BASE**EMAX * ( 1 - EPS ), where  BASE  is the floating point

       value of BETA.

  Further Details
  ===============

  The computation of  EPS  is based on a routine PARANOIA by
  W. Kahan of the University of California at Berkeley.

  =====================================================================
  */
  /**
   * @param unit unit
   * @return result
   */
  private int dlamc2(RS unit) {
    if (this.showDetailFlag) {
      System.out.println("in>>dlamc2"); //$NON-NLS-1$
    }

    //final RS unit = (E)Tools.getUnitNumber();

    /* Table of constant values */
    int lbeta = 0;
    int lemin = 0;
    int lemax = 0;
    int gnmin = 0;
    int gpmin = 0;
    int lt = 0;

    RS lrmin = unit.createZero();
    RS lrmax = unit.createZero();

    RS leps = null;
    //boolean first = true;

    if (this.isFirst) {
      RS zero = unit.createZero();
      RS one = unit.createUnit();
      RS two = unit.create(2);

      /* LBETA, LT, LRND, LEPS, LEMIN and LRMIN  are the local values of BETA, T, RND, EPS, EMIN and RMIN.
       *
       * Throughout this routine  we use the function  DLAMC3  to ensure
       * that relevant values are stored  and not held in registers, or
       * are not affected by optimizers.
       *  
       * DLAMC1 returns the parameters  LBETA, LT, LRND and LIEEE1.
       */
      int[] tmp = dlamc1(lbeta, lt, unit);
      lbeta = tmp[0];
      lt = tmp[1];

      // Start to find EPS.
      RS b = unit.create(lbeta);
      RS a = b.power(-lt);
      leps = a;

      // Try some tricks to see whether or not this is the correct EPS.
      b = two.divide(3);
      RS half = one.divide(2);
      RS sixth = dlamc3(b, half.unaryMinus());
      RS third = dlamc3(sixth, sixth);
      b = dlamc3(third, half.unaryMinus());
      b = dlamc3(b, sixth);
      b = b.abs();

      if (b.isLessThan(leps)) {
        b = leps;
      }

      leps = unit.createUnit();

      while (leps.isGreaterThan(b) && b.isGreaterThan(zero)) {
        leps = b;
        /* Computing 5th power */
        RS d3 = two.multiply(two);
        RS d2 = two.multiply(d3.multiply(d3)).multiply(leps.multiply(leps));
        RS c = dlamc3(half.multiply(leps), d2);
        c = dlamc3(half, c.unaryMinus());
        b = dlamc3(half, c);
        c = dlamc3(half, b.unaryMinus());
        b = dlamc3(half, c);
      }

      if (a.isLessThan(leps)) {
        leps = a;
      }

      /* 
       * Computation of EPS complete.
       * Now find  EMIN.  Let A = + or - 1, and + or - (1 + BASE**(-3)).
       * Keep dividing  A by BETA until (gradual) underflow occurs. T his
       * is detected when we cannot recover the previous A.
       */

      RS rbase = one.divide(lbeta);
      RS small = one;
      for (int i = 1; i <= 3; ++i) {
        small = dlamc3(small.multiply(rbase), zero);
        /* L20: */
      }
      a = dlamc3(one, small);
      int ngpmin = 0;
      ngpmin = dlamc4(ngpmin, one, lbeta, unit);

      int ngnmin = 0;
      ngnmin = dlamc4(ngnmin, one.unaryMinus(), lbeta, unit);
      gpmin = dlamc4(gpmin, a, lbeta, unit);
      gnmin = dlamc4(gnmin, a.unaryMinus(), lbeta, unit);

      boolean ieee = false;
      boolean iwarn = false;

      if (ngpmin == ngnmin && gpmin == gnmin) {
        if (ngpmin == gpmin) {
          lemin = ngpmin;
          /* ( Non twos-complement machines, no gradual under flow; e.g., VAX) */
        } else if (gpmin - ngpmin == 3) {
          lemin = ngpmin - 1 + lt;
          ieee = true;
          /* ( Non twos-complement machines, with gradual und erflow; e.g., IEEE standard followers ) */
        } else {
          lemin = Math.min(ngpmin, gpmin);
          /* (A guess; no known machine) */
          iwarn = true;
        }
      } else if (ngpmin == gpmin && ngnmin == gnmin) {
        if (Math.abs(ngpmin - ngnmin) == 1) {
          lemin = Math.max(ngpmin, ngnmin);
          /* ( Twos-complement machines, no gradual underflow; e.g., CYBER 205 ) */
        } else {
          lemin = Math.min(ngpmin, ngnmin);
          /*( A guess; no known machine ) */
          iwarn = true;
        }
      } else if (Math.abs(ngpmin - ngnmin) == 1 && gpmin == gnmin) {
        if (gpmin - Math.min(ngpmin, ngnmin) == 3) {
          lemin = Math.max(ngpmin, ngnmin) - 1 + lt;
          /* ( Twos-complement machines with gradual underflow; no known machine ) */
        } else {
          lemin = Math.min(ngpmin, ngnmin);
          /* ( A guess; no known machine ) */
          iwarn = true;
        }
      } else {
        /* Computing MIN */
        lemin = Math.min(Math.min(Math.min(ngpmin, ngnmin), gpmin), gnmin);
        /* ( A guess; no known machine ) */
        iwarn = true;
      }

      /* Comment out this if block if EMIN is ok */
      if (iwarn) {
        this.isFirst = true;
        System.out.printf("\n\n WARNING. The value EMIN may be incorrect:- "); //$NON-NLS-1$
        System.out.printf("EMIN = %8i\n", Integer.valueOf(lemin)); //$NON-NLS-1$
        System.out.printf("If, after inspection, the value EMIN looks acceptable"); //$NON-NLS-1$
        System.out.printf("please comment out \n the IF block as marked within the"); //$NON-NLS-1$
        System.out.printf("code of routine DLAMC2, \n otherwise supply EMIN"); //$NON-NLS-1$
        System.out.printf("explicitly.\n"); //$NON-NLS-1$
      }

      /*
       * Assume IEEE arithmetic if we found denormalised numbers above,
       * or if arithmetic seems to round in the  IEEE style,  determined
       * in routine DLAMC1. A true IEEE machine should have both  things
       * true; however, faulty machines may have one or the other.
       */
      ieee = ieee || this.ieee1;

      /* 
       * Compute  RMIN by successive division by  BETA. We could compute
       * RMIN as BASE**( EMIN - 1 ),  but some machines underflow during
       * this computation.
       */
      lrmin = unit.createUnit();
      for (int i = 1; i <= 1 - lemin; ++i) {
        lrmin = dlamc3(lrmin.multiply(rbase), zero);
        /* L30: */
      }

      /* Finally, call DLAMC5 to compute EMAX and RMAX. */
      //Object[] ans = dlamc5(lbeta, lt, lemin, ieee, unit);
      Dlamc5Result ans = dlamc5(lbeta, lt, lemin, ieee, unit);
      lemax = ans.lemax;
      lrmax = ans.y;
    }

    this.beta = lbeta;
    this.it = lt;
    this.eps = leps;
    this.imin = lemin;
    this.rmin = lrmin;
    this.imax = lemax;
    this.rmax = lrmax;

    if (this.showDetailFlag) {
      System.out.println("out>>dlamc2"); //$NON-NLS-1$
    }
    return 0;
  }

  /*  -- LAPACK auxiliary routine (version 3.0) --
  Univ. of Tennessee, Univ. of California Berkeley, NAG Ltd.,
  Courant Institute, Argonne National Lab, and Rice University
  October 31, 1992


  Purpose
  =======

  DLAMC3  is intended to force  A  and  B  to be stored prior to doing

  the addition of  A  and  B ,  for use in situations where optimizers

  might hold one of these in a register.

  Arguments
  =========

  A, B    (input) DOUBLE PRECISION
       The values A and B.

  =====================================================================
  */
  /**
   * @param a a
   * @param b b
   * @return result
   */
  private RS dlamc3(RS a, RS b) {
    RS ret_val = a.add(b);
    return ret_val;
  }

  /*  -- LAPACK auxiliary routine (version 2.0) --
  Univ. of Tennessee, Univ. of California Berkeley, NAG Ltd.,
  Courant Institute, Argonne National Lab, and Rice University
  October 31, 1992


  Purpose
  =======

  DLAMC4 is a service routine for DLAMC2.

  Arguments
  =========

  EMIN    (output) EMIN
       The minimum exponent before (gradual) underflow, computed by

       setting A = START and dividing by BASE until the previous A
       can not be recovered.

  START   (input) DOUBLE PRECISION
       The starting point for determining EMIN.

  BASE    (input) INTEGER
       The base of the machine.

  =====================================================================
  */
  /**
   * @param emino output
   * @param start input
   * @param base input
   * @param unit unit
   * @return result
   */
  private int dlamc4(int emino, RS start, int base, RS unit) {
    if (this.showDetailFlag) {
      System.out.println("in>>dlamc4"); //$NON-NLS-1$
    }
    //final RS unit = (E)Tools.getUnitNumber();

    RS a = start;
    RS one = unit.createUnit();
    RS rbase = one.divide(base);
    RS zero = unit.createZero();
    emino = 1;
    RS b1 = dlamc3(a.multiply(rbase), zero);
    RS c1 = a;
    RS c2 = a;
    RS d1 = a;
    RS d2 = a;

    long testCode1 = 0;
    while (c1.equals(a) && c2.equals(a) && d1.equals(a) && d2.equals(a)) {
      testCode1++;
      if (testCode1 % (1000 * 1000) == 0) {
        System.out.println(testCode1);
      }

      emino = emino - 1;
      a = b1;
      b1 = dlamc3(a.divide(base), zero);
      c1 = dlamc3(b1.multiply(base), zero);
      d1 = zero;
      for (int i = 1; i <= base; ++i) {
        d1 = d1.add(b1);
        /* L20: */
      }
      RS b2 = dlamc3(a.multiply(rbase), zero);
      c2 = dlamc3(b2.divide(rbase), zero);
      d2 = zero;
      for (int i = 1; i <= base; ++i) {
        d2 = d2.add(b2);
        /* L30: */
      }
    }

    if (this.showDetailFlag) {
      System.out.println("out>>dlamc4"); //$NON-NLS-1$
    }
    return emino;
  }

  /*  -- LAPACK auxiliary routine (version 3.0) --
  Univ. of Tennessee, Univ. of California Berkeley, NAG Ltd.,
  Courant Institute, Argonne National Lab, and Rice University
  October 31, 1992


  Purpose
  =======

  DLAMC5 attempts to compute RMAX, the largest machine floating-point
  number, without overflow.  It assumes that EMAX + abs(EMIN) sum
  approximately to a power of 2.  It will fail on machines where this
  assumption does not hold, for example, the Cyber 205 (EMIN = -28625,

  EMAX = 28718).  It will also fail if the value supplied for EMIN is
  too large (i.e. too close to zero), probably with overflow.

  Arguments
  =========

  BETA    (input) INTEGER
       The base of floating-point arithmetic.

  P       (input) INTEGER
       The number of base BETA digits in the mantissa of a
       floating-point value.

  EMIN    (input) INTEGER
       The minimum exponent before (gradual) underflow.

  IEEE    (input) LOGICAL
       A logical flag specifying whether or not the arithmetic
       system is thought to comply with the IEEE standard.

  EMAX    (output) INTEGER
       The largest exponent before overflow

  RMAX    (output) DOUBLE PRECISION
       The largest machine floating-point number.

  =====================================================================



  First compute LEXP and UEXP, two powers of 2 that bound
  abs(EMIN). We then assume that EMAX + abs(EMIN) will sum
  approximately to the bound that is closest to abs(EMIN).
  (EMAX is the exponent of the required number RMAX). */
  /**
   * @param lbeta lbeta
   * @param lt lt
   * @param emini emini
   * @param ieee ieee
   * @param unit unit
   * @return result
   */
  private Dlamc5Result dlamc5(int lbeta, int lt, int emini, boolean ieee, RS unit) {
    //final RS unit = (E)Tools.getUnitNumber();

    if (this.showDetailFlag) {
      System.out.println("in>>dlamc5"); //$NON-NLS-1$
    }

    /* Table of constant values */
    RS c_b5 = unit.createZero();

    RS oldy = null;
    int lexp = 1;
    int exbits = 1;

    int try__ = lexp << 1;
    while (try__ <= -emini) {
      lexp = try__;
      ++exbits;
      try__ = lexp << 1;
    }

    int uexp;
    if (lexp == -emini) {
      uexp = lexp;
    } else {
      uexp = try__;
      ++exbits;
    }

    /* 
     * Now -LEXP is less than or equal to EMIN, and -UEXP is greater
     * than or equal to EMIN. EXBITS is the number of bits needed to
     * store the exponent. 
     */
    int expsum;
    if (uexp + emini > -lexp - emini) {
      expsum = lexp << 1;
    } else {
      expsum = uexp << 1;
    }

    /* EXPSUM is the exponent range, approximately equal to EMAX - EMIN + 1 . */

    int lemax = expsum + emini - 1;
    int nbits = exbits + 1 + lt;

    /* NBITS is the total number of bits needed to store a floating-point number. */

    if (nbits % 2 == 1 && lbeta == 2) {
      /* Either there are an odd number of bits used to store a
         floating-point number, which is unlikely, or some bits are

         not used in the representation of numbers, which is possible,
         (e.g. Cray machines) or the mantissa has an implicit bit,
         (e.g. IEEE machines, Dec Vax machines), which is perhaps the

         most likely. We have to assume the last alternative.
         If this is true, then we need to reduce EMAX by one because

         there must be some way of representing zero in an implicit-b it
         system. On machines like Cray, we are reducing EMAX by one unnecessarily.
        */

      lemax = lemax - 1;
    }

    if (ieee) {
      /* Assume we are on an IEEE machine which reserves one exponent for infinity and NaN. */
      lemax = lemax - 1;
    }

    /* Now create RMAX, the largest machine number, which should
       be equal to (1.0 - BETA**(-P)) * BETA**EMAX .

       First compute 1.0 - BETA**(-P), being careful that the result is less than 1.0 . */

    RS recbas = unit.createUnit().divide(lbeta);
    RS z = unit.create(lbeta).subtract(1);
    RS y = unit.createZero();

    for (int i = 1; i <= lt; ++i) {
      z = z.multiply(recbas);
      if (y.isLessThan(1)) {
        oldy = y;
      }
      y = dlamc3(y, z);
      /* L20: */
    }
    if (y.isGreaterThanOrEquals(1)) {
      y = oldy;
    }

    /* Now multiply by BETA**EMAX to get RMAX. */
    for (int i = 1; i <= lemax; ++i) {
      y = dlamc3(y.multiply(lbeta), c_b5);
      /* L30: */
    }

    if (this.showDetailFlag) {
      System.out.println("out>>dlamc5"); //$NON-NLS-1$
    }

    Dlamc5Result result = new Dlamc5Result();
    result.lemax = lemax;
    result.y = y;
    
    return result;
    
    //return new Object[] {Integer.valueOf(lemax), y};
  }
  
  class Dlamc5Result {
    int lemax;
    RS y;
  }

  /**
   * @param cmach cmach
   * @param unit RS
   * @return result
   */
  public RS execute(String cmach, RS unit) {
    return dlamch(cmach, unit);
  }

  /**
   * 
   */
  private void printAll() {
    System.out.println("isFirst:" + this.isFirst); //$NON-NLS-1$
    System.out.println("base:" + this.base.toString()); //$NON-NLS-1$
    System.out.println("beta:" + this.beta); //$NON-NLS-1$
    System.out.println("emin:" + this.emin.toString()); //$NON-NLS-1$
    System.out.println("prec:" + this.prec.toString()); //$NON-NLS-1$
    System.out.println("emax:" + this.emax.toString()); //$NON-NLS-1$
    System.out.println("imin:" + this.imin); //$NON-NLS-1$
    System.out.println("imax:" + this.imax); //$NON-NLS-1$
    System.out.println("rmin:" + this.rmin.toString()); //$NON-NLS-1$
    System.out.println("rmax:" + this.rmax.toString()); //$NON-NLS-1$
    System.out.println("t:" + this.t.toString()); //$NON-NLS-1$
    //System.out.println("rmach:" + this.rmach.toString()); //$NON-NLS-1$
    System.out.println("sfmin:" + this.sfmin.toString()); //$NON-NLS-1$
    System.out.println("it:" + this.it); //$NON-NLS-1$
    System.out.println("rnd:" + this.rnd.toString()); //$NON-NLS-1$
    System.out.println("eps:" + this.eps.toString()); //$NON-NLS-1$
    System.out.println("lrnd:" + this.lrnd); //$NON-NLS-1$
    System.out.println("ieee1:" + this.ieee1); //$NON-NLS-1$
  }

  /**
   * @return result
   */
  List<String> getKeyList() {
    List<String> key = new LinkedList<>();
    key.add("E"); //$NON-NLS-1$
    key.add("S"); //$NON-NLS-1$
    key.add("B"); //$NON-NLS-1$
    key.add("P"); //$NON-NLS-1$
    key.add("N"); //$NON-NLS-1$
    key.add("R"); //$NON-NLS-1$
    key.add("M"); //$NON-NLS-1$
    key.add("U"); //$NON-NLS-1$
    key.add("L"); //$NON-NLS-1$
    key.add("O"); //$NON-NLS-1$
    return key;
  }

  // 
  // = 'E' or 'e',   DLAMCH := eps
  // = 'S' or 's ,   DLAMCH := sfmin
  // = 'B' or 'b',   DLAMCH := base
  // = 'P' or 'p',   DLAMCH := eps*base
  // = 'N' or 'n',   DLAMCH := t
  // = 'R' or 'r',   DLAMCH := rnd
  // = 'M' or 'm',   DLAMCH := emin
  // = 'U' or 'u',   DLAMCH := rmin
  // = 'L' or 'l',   DLAMCH := emax
  // = 'O' or 'o',   DLAMCH := rmax
  /**
   * @see java.lang.Object#toString()
   */
  @Override
  public String toString() {
    String message = ""; //$NON-NLS-1$
    message = message + "eps:" + this.eps.toString("e") + "\n" + "sfmin:" + this.sfmin.toString("e") + "\n" + "base:" + this.base.toString("e") + "\n" + "p:" + this.prec.toString("e") + "\n"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$ //$NON-NLS-10$ //$NON-NLS-11$ //$NON-NLS-12$
    return message;
  }
}