// file: bw_sub_0.cc
//

// isip include files
//
#include "bw_train.h"
#include "bw_train_constants.h"

// method: log_sub_cc
//
// arguments:
//  float_8 x : (input) variable
//  float_8 y : (input) variable
//
// return: a float_8 value resulting from the subtraction
//
// this method inputs x = log(a), y = log(b) and returns log(x-y)
//
float_8 log_sub_cc(float_8 x_a, float_8 y_a) {

  // local variables
  //
  float_8 diff = (float_8)0.0;
  float_8 z = (float_8)0.0;
  float_8 temp = BW_LOG_ZERO;
  temp = -log(-temp);

  // check if x >= y
  //
  if (x_a < y_a) {
    fprintf(stdout, "Error in log subtraction : x can't be less than y");
    exit(ISIP_PROTO_ERROR);
  }
  else {
    diff = y_a - x_a;
    if (diff < temp) {
      if (x_a < BW_LOG_MIN) {
	z = BW_LOG_ZERO;
      }
      else {
	z = x_a;
      }
    }
    else {
      z = 1.0 - exp(diff);
      if (z < BW_MIN_LARG) {
	z = BW_LOG_ZERO;
      }
      else {
	z = x_a + log(z);
      }
    }
  }

  // exit gracefully
  //
  return z;
}
