<commit_before>#include "util/bignum/floatimpl.h"
#include <limits>
#include <string>

namespace xqp {

std::ostream& operator<<(std::ostream& os, const Double& aDouble) {
  os << aDouble.toString();
  return os;
}

std::ostream& operator<<(std::ostream& os, const Float& aDouble) {
  os << aDouble.toString();
  return os;
}

bool FloatImplTraits<double>::isPosInf(MAPM aMAPM) {
  if (aMAPM > 0) {
    return aMAPM.exponent() > 308 || aMAPM > MAPM("1.7976931348623157e+308");
  } else {
    return false;
  }
}

bool FloatImplTraits<double>::isZero(MAPM aMAPM) { 
  int lExp = aMAPM.exponent();
  return lExp < -308 || (lExp == -308 && aMAPM.abs() < MAPM("2.2250738585072014e-308"));
}

bool FloatImplTraits<double>::isNegInf(MAPM aMAPM) { 
  if (aMAPM < 0) {
    return aMAPM.exponent() > 308 || aMAPM.abs() > MAPM("1.7976931348623157e+308");
  } else {
    return false;
  }
}

bool FloatImplTraits<float>::isPosInf(MAPM aMAPM) {
  if (aMAPM > 0) {
    return aMAPM.exponent() > 38 || aMAPM > MAPM("3.4028235e+38");
  } else {
    return false;
  }
}

bool FloatImplTraits<float>::isZero(MAPM aMAPM) { 
  int lExp = aMAPM.exponent();
  return lExp < -308 || (lExp == -308 && aMAPM.abs() < MAPM("1.1754944e-38"));
}

bool FloatImplTraits<float>::isNegInf(MAPM aMAPM) { 
  if (aMAPM < 0) {
    return aMAPM.exponent() > 38 || aMAPM.abs() > MAPM("3.4028235e+38");
  } else {
    return false;
  }
}
  
const xqpString FloatConsts::INF_POS_STR = "INF";
const xqpString FloatConsts::INF_NEG_STR = "-INF";
const xqpString FloatConsts::NOT_A_NUM_STR = "NaN";

template <typename FloatType>
void FloatImpl<FloatType>::setFloatImpl(FloatType aFloatImpl){
  switch(theType = checkInfNaNNeg(aFloatImpl)) {
    case FloatConsts::INF_NEG:
    case FloatConsts::NOT_A_NUM:
    case FloatConsts::INF_POS:
      theFloatImpl = 0;
      break;
    case FloatConsts::NORMAL_NEG:
    case FloatConsts::NORMAL:
      theFloatImpl = aFloatImpl;
      break;
  }
}

template <typename FloatType>
bool FloatImpl<FloatType>::checkInfNaN(FloatImpl& aFloatImpl) {
  if (aFloatImpl.theType == FloatConsts::NORMAL || aFloatImpl.theType == FloatConsts::NORMAL_NEG) {
    if (FloatImplTraits<FloatType>::isPosInf(aFloatImpl.theFloatImpl)) {
      aFloatImpl.theType = FloatConsts::INF_POS;
      aFloatImpl.theFloatImpl = 0;
      return true;
    } else if (FloatImplTraits<FloatType>::isNegInf(aFloatImpl.theFloatImpl)) {
      aFloatImpl.theType = FloatConsts::INF_NEG;
      aFloatImpl.theFloatImpl = 0;
      return true;
    } else if (FloatImplTraits<FloatType>::isZero(aFloatImpl.theFloatImpl)) {
      aFloatImpl.theType = FloatConsts::NORMAL;
      aFloatImpl.theFloatImpl = 0;
      return true;
    }
  }
  return false;
}

template <typename FloatType>
FloatConsts::NumType FloatImpl<FloatType>::checkInfNaNNeg(const char* aCharStar) {
#if HAVE_STRCASECMP_FUNCTION && !WIN32
    if (strcasecmp(aCharStar, "inf") == 0 || strcasecmp(aCharStar, "+inf") == 0 )
#else
    if (_stricmp(aCharStar, "inf") == 0 || _stricmp(aCharStar, "+inf") == 0 )
#endif
    {
      return FloatConsts::INF_POS;
    }
#if HAVE_STRCASECMP_FUNCTION && !WIN32
    else if (strcasecmp(aCharStar, "-inf") == 0 )
#else
    else if (_stricmp(aCharStar, "-inf") == 0 )
#endif
    {
      return FloatConsts::INF_NEG;
    }
#if HAVE_STRCASECMP_FUNCTION && !WIN32
    else if (strcasecmp(aCharStar, "nan") == 0 )
#else
    else if (_stricmp(aCharStar, "nan") == 0 )
#endif
    {
      return FloatConsts::NOT_A_NUM;
    }
    else if (aCharStar[0] == '-') 
    {
      return FloatConsts::NORMAL_NEG;
    }  
    else
    {
      return FloatConsts::NORMAL;
    }
}

template <typename FloatType>
FloatConsts::NumType FloatImpl<FloatType>::checkInfNaNNeg(FloatType aFloat) {
  if (aFloat != aFloat) {
    return FloatConsts::NOT_A_NUM;
  } else if (aFloat == std::numeric_limits<FloatType>::infinity()) {
    return FloatConsts::INF_POS;
  } else if (aFloat == -std::numeric_limits<FloatType>::infinity()) {
    return FloatConsts::INF_NEG;
  } else if (aFloat < 0) {
    return FloatConsts::NORMAL_NEG;
  } else {
    return FloatConsts::NORMAL;
  }
}

template <typename FloatType>
bool FloatImpl<FloatType>::parse(const char* aCharStar, FloatImpl& aFloatImpl) {
  const char* lCur = aCharStar;
  std::string lBuffer(aCharStar);

  bool lGotBase = false;
  bool lGotPoint = false;
  bool lGotSign = false;
  bool lGotDigit = false;
  bool lStop = false;
  bool lIsNegative = false;

  char lTmp;
  int lCount = 0;
  while(!lStop && *lCur != 0) {
    lTmp = *lCur++;
    
    switch(lTmp) {

    case '+': {
      if(lGotSign || lGotDigit) {
        lStop = true;
      } else {
        lGotSign = true;
      }
      break;
    }
             
    case '-': {
      if(lGotSign || lGotSign) {
        lStop = true;
      } else {
        lGotSign = true;
        if(!lGotBase) lIsNegative = true;
      }
      break;
    }
             
    case 'E':
    case 'e': {<commit_msg>Bug in zerotest of float fixed<commit_after>#include "util/bignum/floatimpl.h"
#include <limits>
#include <string>

namespace xqp {

std::ostream& operator<<(std::ostream& os, const Double& aDouble) {
  os << aDouble.toString();
  return os;
}

std::ostream& operator<<(std::ostream& os, const Float& aDouble) {
  os << aDouble.toString();
  return os;
}

bool FloatImplTraits<double>::isPosInf(MAPM aMAPM) {
  if (aMAPM > 0) {
    return aMAPM.exponent() > 308 || aMAPM > MAPM("1.7976931348623157e+308");
  } else {
    return false;
  }
}

bool FloatImplTraits<double>::isZero(MAPM aMAPM) { 
  int lExp = aMAPM.exponent();
  return lExp < -308 || (lExp == -308 && aMAPM.abs() < MAPM("2.2250738585072014e-308"));
}

bool FloatImplTraits<double>::isNegInf(MAPM aMAPM) { 
  if (aMAPM < 0) {
    return aMAPM.exponent() > 308 || aMAPM.abs() > MAPM("1.7976931348623157e+308");
  } else {
    return false;
  }
}

bool FloatImplTraits<float>::isPosInf(MAPM aMAPM) {
  if (aMAPM > 0) {
    return aMAPM.exponent() > 38 || aMAPM > MAPM("3.4028235e+38");
  } else {
    return false;
  }
}

bool FloatImplTraits<float>::isZero(MAPM aMAPM) { 
  int lExp = aMAPM.exponent();
  return lExp < -38 || (lExp == -38 && aMAPM.abs() < MAPM("1.1754944e-38"));
}

bool FloatImplTraits<float>::isNegInf(MAPM aMAPM) { 
  if (aMAPM < 0) {
    return aMAPM.exponent() > 38 || aMAPM.abs() > MAPM("3.4028235e+38");
  } else {
    return false;
  }
}
  
const xqpString FloatConsts::INF_POS_STR = "INF";
const xqpString FloatConsts::INF_NEG_STR = "-INF";
const xqpString FloatConsts::NOT_A_NUM_STR = "NaN";

template <typename FloatType>
void FloatImpl<FloatType>::setFloatImpl(FloatType aFloatImpl){
  switch(theType = checkInfNaNNeg(aFloatImpl)) {
    case FloatConsts::INF_NEG:
    case FloatConsts::NOT_A_NUM:
    case FloatConsts::INF_POS:
      theFloatImpl = 0;
      break;
    case FloatConsts::NORMAL_NEG:
    case FloatConsts::NORMAL:
      theFloatImpl = aFloatImpl;
      break;
  }
}

template <typename FloatType>
bool FloatImpl<FloatType>::checkInfNaN(FloatImpl& aFloatImpl) {
  if (aFloatImpl.theType == FloatConsts::NORMAL || aFloatImpl.theType == FloatConsts::NORMAL_NEG) {
    if (FloatImplTraits<FloatType>::isPosInf(aFloatImpl.theFloatImpl)) {
      aFloatImpl.theType = FloatConsts::INF_POS;
      aFloatImpl.theFloatImpl = 0;
      return true;
    } else if (FloatImplTraits<FloatType>::isNegInf(aFloatImpl.theFloatImpl)) {
      aFloatImpl.theType = FloatConsts::INF_NEG;
      aFloatImpl.theFloatImpl = 0;
      return true;
    } else if (FloatImplTraits<FloatType>::isZero(aFloatImpl.theFloatImpl)) {
      aFloatImpl.theType = FloatConsts::NORMAL;
      aFloatImpl.theFloatImpl = 0;
      return true;
    }
  }
  return false;
}

template <typename FloatType>
FloatConsts::NumType FloatImpl<FloatType>::checkInfNaNNeg(const char* aCharStar) {
#if HAVE_STRCASECMP_FUNCTION && !WIN32
    if (strcasecmp(aCharStar, "inf") == 0 || strcasecmp(aCharStar, "+inf") == 0 )
#else
    if (_stricmp(aCharStar, "inf") == 0 || _stricmp(aCharStar, "+inf") == 0 )
#endif
    {
      return FloatConsts::INF_POS;
    }
#if HAVE_STRCASECMP_FUNCTION && !WIN32
    else if (strcasecmp(aCharStar, "-inf") == 0 )
#else
    else if (_stricmp(aCharStar, "-inf") == 0 )
#endif
    {
      return FloatConsts::INF_NEG;
    }
#if HAVE_STRCASECMP_FUNCTION && !WIN32
    else if (strcasecmp(aCharStar, "nan") == 0 )
#else
    else if (_stricmp(aCharStar, "nan") == 0 )
#endif
    {
      return FloatConsts::NOT_A_NUM;
    }
    else if (aCharStar[0] == '-') 
    {
      return FloatConsts::NORMAL_NEG;
    }  
    else
    {
      return FloatConsts::NORMAL;
    }
}

template <typename FloatType>
FloatConsts::NumType FloatImpl<FloatType>::checkInfNaNNeg(FloatType aFloat) {
  if (aFloat != aFloat) {
    return FloatConsts::NOT_A_NUM;
  } else if (aFloat == std::numeric_limits<FloatType>::infinity()) {
    return FloatConsts::INF_POS;
  } else if (aFloat == -std::numeric_limits<FloatType>::infinity()) {
    return FloatConsts::INF_NEG;
  } else if (aFloat < 0) {
    return FloatConsts::NORMAL_NEG;
  } else {
    return FloatConsts::NORMAL;
  }
}

template <typename FloatType>
bool FloatImpl<FloatType>::parse(const char* aCharStar, FloatImpl& aFloatImpl) {
  const char* lCur = aCharStar;
  std::string lBuffer(aCharStar);

  bool lGotBase = false;
  bool lGotPoint = false;
  bool lGotSign = false;
  bool lGotDigit = false;
  bool lStop = false;
  bool lIsNegative = false;

  char lTmp;
  while(!lStop && *lCur != 0) {
    lTmp = *lCur++;
    
    switch(lTmp) {

    case '+': {
      if(lGotSign || lGotDigit) {
        lStop = true;
      } else {
        lGotSign = true;
      }
      break;
    }
             
    case '-': {
      if(lGotSign || lGotSign) {
        lStop = true;
      } else {
        lGotSign = true;
        if(!lGotBase) lIsNegative = true;
      }
      break;
    }
             
    case 'E':
    case 'e': {
{'fraction_non_alphanumeric': 0.1439432336543335, 'fraction_numerical': 0.02767359351241764, 'mean_word_length': 3.4561878952122855, 'pattern_counts': {'":': 0, '<': 79, '<?xml version=': 0, '>': 69, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7124192', 'n_tokens_mistral': 3911, 'n_tokens_neox': 3664, 'n_words': 714}