/*
 * Jakelib2 - General purpose C++ library
 * Copyright (C) 2001 Florian Wolff  (florian@donuz.de)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * $Id: Double.h,v 1.13 2003/02/11 13:53:41 alfa Exp $
 */

#ifndef INCLUDE_JAKELIB2_LANG_DOUBLE_H
# define INCLUDE_JAKELIB2_LANG_DOUBLE_H
# include "Number.h"
# include <math.h>
/*
# define NEGATIVE_INFINITY LL(0xFFF0000000000000)
# define POSITIVE_INFINITY LL(0x7FF0000000000000)
*/
//extern implemented in lang/dtoa.c
extern "C" char *dtoa(double d, int mode, int ndigits,
			int *decpt, int *sign, char **rve);

namespace jakelib {
  namespace lang {

    /**
     * @brief An instance of this class encapsulates a single \c jdouble value.
     * \c Double objects can be used as hash keys for example.
     * Some class methods provide functions to convert strings to Doubles 
     * and vice versa.
     *
     * @class Double
     * @version $Id: Double.h,v 1.13 2003/02/11 13:53:41 alfa Exp $
     * @author Florian 'Overflo' Wolff (florian@donuz.de)
     * @todo \c decode is not yet implemented
     */
    class Double : public Number, Comparable
      {
      private:
	/** @brief The actual \c jdouble value */
	jdouble value;

        static const unsigned int* firstNanWord;
        static const unsigned int* secondNanWord;

      public:
	/**
	 * @brief Creates a new \c Double object with the given \c jdouble value.
	 */
	Double(jdouble value);

	/**
	 * @brief Creates a new \c Double object. The given \c String is
	 * parsed and converted to \c jdouble.
	 *
	 * @throws NumberFormatException If the \c String cannot be parsed
	 */
	Double(String* s);
  
	/**
	 * @brief Returns a hashcode for the Double value. 
	 *
	 * @return hash code
	 */
	jint hashCode();
  
	/**
	 * @brief Returns the string representation of the Double value.
	 *
	 * @return String representation
	 */
	String* toString();
  
	/**
	 * @brief Returns the string representation of the given Double value.
	 *
	 * @param value Double value.
         * @param ndigits
	 * @return String representation
	 */
	static String* toString(jdouble value, jint ndigits = 0);
  
	/**
	 * @brief Compares this object to the specified object.
	 *
	 * The result is <code>true</code> if and only if the argument is not
	 * <code>null</code> and is an <code>Double</code> object that contains
	 * the same <code>jdouble</code> value as this object.
	 *
	 * @param   obj   the object to compare with.
	 * @return  <code>true</code> if the objects are the same;
	 *          <code>false</code> otherwise.
	 */
	jboolean equals(Object* obj);
    
	/**
	 * @brief Returns the number represented by this object converted to <tt>jbyte</tt>.
	 */
	jbyte byteValue();

	/**
	 * @brief Returns the number represented by this object converted to <tt>jdouble</tt>.
	 */
	jdouble doubleValue();

	/**
	 * @brief Returns the number represented by this object converted to <tt>jfloat</tt>.
	 */
	jfloat floatValue();

	/**
	 * @brief Returns the number represented by this object converted to <tt>jint</tt>.
	 */
	jint intValue();

	/**
	 * @brief Returns the number represented by this object converted to <tt>jlong</tt>.
	 */
	jlong longValue();

	/**
	 * @brief Returns the number represented by this object converted to <tt>jshort</tt>.
	 */
	jshort shortValue();

	/**
	 *
	 * @throws NumberFormatException If the \c String cannot be parsed
	 */
	static Double* valueOf(String* str);

	/**
	 *
	 * @throws NumberFormatException If the \c String cannot be parsed
	 */
	static jdouble parseDouble(String* str);

	/**
	 *
	 * @throws NumberFormatException If the string cannot be parsed
	 */
	static jdouble parseDouble(const char* str);
  
	int compareTo(Double* obj);
	int compareTo(Object* obj);

        /**
         * Return <code>true</code> if the <code>double</code> has the same
         * value as <code>NaN</code>, otherwise return <code>false</code>.
         *
         * @param v the <code>double</code> to compare
         * @return whether the argument is <code>NaN</code>.
         */
        static jboolean isNaN(jdouble v);

        /**
         * Return <code>true</code> if the <code>double</code> has a value
         * equal to either <code>NEGATIVE_INFINITY</code> or
         * <code>POSITIVE_INFINITY</code>, otherwise return <code>false</code>.
         *
         * @param v the <code>double</code> to compare
         * @return whether the argument is (-/+) infinity.
         */
        static jboolean isInfinite(jdouble v);
        static jboolean isNegativeInfinite(jdouble v);
        static jboolean isPositiveInfinite(jdouble v);

	static const jdouble MAX_VALUE;
	static const jdouble MIN_VALUE;
    static const jdouble NEGATIVE_INFINITY;
    static const jdouble POSITIVE_INFINITY;
    static const jdouble ZERO;
        /*
          static const jdouble NEGATIVE_ZERO;
          static const jdouble POSITIVE_ZERO;
          
        */
        static const jdouble NaN;
        static const jint DEFAULT_DIGITS;//digits after decimal point

        /**
         * Convert the double to the IEEE 754 floating-point "double format" bit
         * layout. Bit 63 (the most significant) is the sign bit, bits 62-52
         * (masked by 0x7ff0000000000000L) represent the exponent, and bits 51-0
         * (masked by 0x000fffffffffffffL) are the mantissa. This function
         * collapses all versions of NaN to 0x7ff8000000000000L. The result of this
         * function can be used as the argument to
         * <code>Double.longBitsToDouble(long)</code> to obtain the original
         * <code>double</code> value.
         *
         * @param value the <code>double</code> to convert
         * @return the bits of the <code>double</code>
         * @see #longBitsToDouble(long)
         */
        static jlong doubleToLongBits(jdouble value);

        /**
         * Convert the double to the IEEE 754 floating-point "double format" bit
         * layout. Bit 63 (the most significant) is the sign bit, bits 62-52
         * (masked by 0x7ff0000000000000L) represent the exponent, and bits 51-0
         * (masked by 0x000fffffffffffffL) are the mantissa. This function
         * leaves NaN alone, rather than collapsing to a canonical value. The
         * result of this function can be used as the argument to
         * <code>Double.longBitsToDouble(long)</code> to obtain the original
         * <code>double</code> value.
         *
         * @param value the <code>double</code> to convert
         * @return the bits of the <code>double</code>
         * @see #longBitsToDouble(long)
         */

        static jlong doubleToRawLongBits(jdouble value);
        /**
         * Convert the argument in IEEE 754 floating-point "double format" bit
         * layout to the corresponding float. Bit 63 (the most significant) is the
         * sign bit, bits 62-52 (masked by 0x7ff0000000000000L) represent the
         * exponent, and bits 51-0 (masked by 0x000fffffffffffffL) are the mantissa.
         * This function leaves NaN alone, so that you can recover the bit pattern
         * with <code>Double.doubleToRawLongBits(double)</code>.
         *
         * @param bits the bits to convert
         * @return the <code>double</code> represented by the bits
         * @see #doubleToLongBits(double)
         * @see #doubleToRawLongBits(double)
         */
        static jdouble longBitsToDouble(jlong bits);

	JAKELIB_DECLARE_CLASS(Double);
      };

  }
}

#endif

