<commit_before>/**
 ** \file object/float-class.cc
 ** \brief Creation of the URBI object float.
 */

#include <cmath>

#include <boost/format.hpp>

#include <sdk/config.h>
#ifndef HAVE_ROUND
# include <libport/ufloat.h>
#endif
#include <libport/ufloat.hh>

#include <object/float-class.hh>
#include <object/list-class.hh>
#include <object/object.hh>
#include <object/string-class.hh>
#include <object/urbi-exception.hh>

namespace object
{
  rObject float_class;

  Float::Float()
    : value_(0)
  {
    proto_add(float_class);
  }

  Float::Float(value_type value)
    : value_(value)
  {
    proto_add(float_class);
  }

  Float::Float(const rFloat& model)
    : value_(model->value_get())
  {
    proto_add(model);
    proto_remove(float_class);
  }

  const Float::value_type&
  Float::value_get() const
  {
    return value_;
  }

  Float::value_type&
  Float::value_get()
  {
    return value_;
  }

  int
  Float::to_int(const libport::Symbol func) const
  {
    try
    {
      return libport::ufloat_to_int(value_);
    }
    catch (libport::bad_numeric_cast& ue)
    {
      throw BadInteger(value_, func);
      return 0;  // Keep the compiler happy
    }
  }

  unsigned int
  Float::to_unsigned_int(const libport::Symbol func,
			 const std::string fmt) const
  {
    try
    {
      int res = libport::ufloat_to_int(value_);
      if (res < 0)
	throw libport::bad_numeric_cast();
      return res;
    }
    catch (libport::bad_numeric_cast& ue)
    {
      throw BadInteger(value_, func, fmt);
      return 0;  // Keep the compiler happy
    }
  }

  Float::value_type
  Float::inf()
  {
    return std::numeric_limits<libport::ufloat>::infinity();
  }

  Float::value_type
  Float::nan()
  {
    return std::numeric_limits<libport::ufloat>::quiet_NaN();
  }

  std::string
  Float::as_string(const rObject& from)
  {
    if (from.get() == float_class.get())
      return SYMBOL(LT_Float_GT);
    {
      type_check<Float>(from, SYMBOL(asString));
      float fl = from->as<Float>()->value_get();
      // Do not rely on boost::format to print inf and nan since
      // behavior differs under Win32
      if (isinf(fl))
        return fl > 0 ? SYMBOL(inf) : SYMBOL(MINUS_inf);
      if (isnan(fl))
        return SYMBOL(nan);
      static boost::format f("%g");
      return str(f % fl);
    }
  }

#define BOUNCE_OP(Op, ResType, Check)					\
  ResType								\
  Float::operator Op(value_type rhs)					\
  {                                                                     \
    static libport::Symbol op(#Op);                                     \
    WHEN(Check,                                                         \
         if (!rhs)							\
           throw PrimitiveError(op, "division by 0"));                  \
    return value_get() Op rhs;						\
  }

  BOUNCE_OP(*, Float::value_type, false)
  BOUNCE_OP(/, Float::value_type, true)
  BOUNCE_OP(<, bool, false)<commit_msg>Qualify invocations of isinf and isnan.<commit_after>/**
 ** \file object/float-class.cc
 ** \brief Creation of the URBI object float.
 */

#include <boost/format.hpp>

#include <libport/cmath>

#include <sdk/config.h>
#ifndef HAVE_ROUND
# include <libport/ufloat.h>
#endif
#include <libport/ufloat.hh>

#include <object/float-class.hh>
#include <object/list-class.hh>
#include <object/object.hh>
#include <object/string-class.hh>
#include <object/urbi-exception.hh>

namespace object
{
  rObject float_class;

  Float::Float()
    : value_(0)
  {
    proto_add(float_class);
  }

  Float::Float(value_type value)
    : value_(value)
  {
    proto_add(float_class);
  }

  Float::Float(const rFloat& model)
    : value_(model->value_get())
  {
    proto_add(model);
    proto_remove(float_class);
  }

  const Float::value_type&
  Float::value_get() const
  {
    return value_;
  }

  Float::value_type&
  Float::value_get()
  {
    return value_;
  }

  int
  Float::to_int(const libport::Symbol func) const
  {
    try
    {
      return libport::ufloat_to_int(value_);
    }
    catch (libport::bad_numeric_cast& ue)
    {
      throw BadInteger(value_, func);
      return 0;  // Keep the compiler happy
    }
  }

  unsigned int
  Float::to_unsigned_int(const libport::Symbol func,
			 const std::string fmt) const
  {
    try
    {
      int res = libport::ufloat_to_int(value_);
      if (res < 0)
	throw libport::bad_numeric_cast();
      return res;
    }
    catch (libport::bad_numeric_cast& ue)
    {
      throw BadInteger(value_, func, fmt);
      return 0;  // Keep the compiler happy
    }
  }

  Float::value_type
  Float::inf()
  {
    return std::numeric_limits<libport::ufloat>::infinity();
  }

  Float::value_type
  Float::nan()
  {
    return std::numeric_limits<libport::ufloat>::quiet_NaN();
  }

  std::string
  Float::as_string(const rObject& from)
  {
    if (from.get() == float_class.get())
      return SYMBOL(LT_Float_GT);
    {
      type_check<Float>(from, SYMBOL(asString));
      float fl = from->as<Float>()->value_get();
      // Do not rely on boost::format to print inf and nan since
      // behavior differs under Win32
      if (std::isinf(fl))
        return fl > 0 ? SYMBOL(inf) : SYMBOL(MINUS_inf);
      if (std::isnan(fl))
        return SYMBOL(nan);
      static boost::format f("%g");
      return str(f % fl);
    }
  }

#define BOUNCE_OP(Op, ResType, Check)					\
  ResType								\
  Float::operator Op(value_type rhs)					\
  {                                                                     \
    static libport::Symbol op(#Op);                                     \
    WHEN(Check,                                                         \
         if (!rhs)							\
           throw PrimitiveError(op, "division by 0"));                  \
    return value_get() Op rhs;						\
  }

  BOUNCE_OP(*, Float::value_type, false)
  BOUNCE_OP(/, Float::value_type, true)
  BOUNCE_OP(<, bool, false)
{'fraction_non_alphanumeric': 0.1337427499147049, 'fraction_numerical': 0.002729443875810304, 'mean_word_length': 2.2938202247191013, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5970650', 'n_tokens_mistral': 2062, 'n_tokens_neox': 1961, 'n_words': 402}