⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 float.java

📁 实现了一个基于j2me移动gps定位系统
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
/*
 * $Id: Float.java 261 2008-07-18 03:52:43Z khanh.lnq $
 * $URL: https://jvnmobilegis.googlecode.com/svn/trunk/src/henson/midp/Float.java $
 * $Author: khanh.lnq $
 * $Revision: 261 $
 * $Date: 2008-07-18 10:52:43 +0700 (Fri, 18 Jul 2008) $
 *
 * ====================================================================
 *
 * Copyright (C) 2006-2007 by JVNGIS
 *
 * All copyright notices regarding JVNMobileGIS MUST remain
 * intact in the Java codes and resource files.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Support can be obtained from project homepage at:
 * http://code.google.com/p/jvnmobilegis/
 *
 * Correspondence and Marketing Questions can be sent to:
 * khanh.lnq AT gmail.com
 *
 * @version: 1.0
 * @author: Khanh Le
 * @Date Created: 22 Jun 2007
 */

package henson.midp;

/**
 * <p>
 * Title: Class for float-point calculations in J2ME applications (MIDP 1.0/CLDC
 * 1.0 where float or double types are not available)
 * </p>
 * <p>
 * Description: It makes float-point calculations via integer numbers
 * </p>
 * <p>
 * Copyright: Nikolay Klimchuk Copyright (c) 2002-2005
 * </p>
 * <p>
 * Company: UNTEH
 * </p>
 * <p>
 * License: Free for use only for non-commercial purpose
 * </p>
 * <p>
 * If you want to use all or part of this class for commercial applications then
 * take into account these conditions:
 * </p>
 * <p>
 * 1. I need a one copy of your product which includes my class with license key
 * and so on
 * </p>
 * <p>
 * 2. Please append my copyright information henson.midp.Float (C) by Nikolay
 * Klimchuk on �About� screen of your product
 * </p>
 * <p>
 * 3. If you have web site please append link <a
 * href=�http://henson.newmail.ru�>Nikolay Klimchuk</a> on the page with
 * description of your product
 * </p>
 * <p>
 * That's all, thank you!
 * </p>
 * 
 * @author Nikolay Klimchuk http://henson.newmail.ru
 * @version 1.01
 */

public class Float {

    /**
     * ERROR constant
     */
    final static private Float ERROR = new Float(Long.MAX_VALUE, Long.MAX_VALUE);
    /**
     * Number of itterations in sqrt method, if you want to make calculations
     * more precise set ITNUM=6,7,... or ITMUN=4,3,... to make it faster
     */
    final static private int ITNUM = 5;
    /**
     * Square root from 3
     */
    private final static Float SQRT3 = new Float(1732050807568877294L, -18L);
    /**
     * The Float value that is closer than any other to pi, the ratio of the
     * circumference of a circle to its diameter
     */
    public final static Float PI = new Float(3141592653589793238L, -18L);
    /**
     * Zero constant
     */
    private final static Float ZERO = new Float();
    /**
     * One constant
     */
    private final static Float ONE = new Float(1L);
    /**
     * The Float value that is closer than any other to e, the base of the
     * natural logarithms
     */
    final static public Float E = new Float(271828182845904512L, -17);
    /**
     * Log10 constant
     */
    private final static Float LOG10 = new Float(2302585092994045684L, -18);
    //
    /**
     * Pi/2 constant
     */
    private final static Float PIdiv2 = PI.Div(2L);
    /**
     * Pi/4 constant
     */
    final static public Float PIdiv4 = PIdiv2.Div(2L);
    /**
     * Pi/6 constant
     */
    private final static Float PIdiv6 = PIdiv2.Div(3L);
    /**
     * Pi/12 constant
     */
    private final static Float PIdiv12 = PIdiv6.Div(2L);
    /**
     * Pi*2 constant
     */
    private final static Float PImul2 = PI.Mul(2L);
    /**
     * Pi*4 constant
     */
    final static public Float PImul4 = PI.Mul(4L);
    /**
     * ln(0.5) constant
     */
    private final static Float LOGdiv2 = new Float(-6931471805599453094L, -19);
    /**
     * Mantissa
     */
    private long m_Val;
    /**
     * Exponent
     */
    private long m_E;
    /**
     * Limit of value
     */
    private final long maxLimit = Long.MAX_VALUE / 100;

    //
    /**
     * Create object with zero inside
     */
    private Float() {
        m_Val = m_E = 0;
    }

    /**
     * Create object and makes initialization with value
     * 
     * @param value
     *            long - value
     */
    public Float(long value) {
        m_Val = value;
        m_E = 0;
    }

    /**
     * Create object and makes initialization both mantissa and exponent
     * 
     * @param value
     *            long - mantissa
     * @param e
     *            long - exponent
     */
    private Float(long value, long e) {
        m_Val = value;
        if (m_Val == 0)
            m_E = 0;
        else
            m_E = e;
    }

    /**
     * Create object and makes initialization by other Float object
     * 
     * @param value
     *            Float - source object
     */
    public Float(Float value) {
        m_Val = value.m_Val;
        if (m_Val == 0)
            m_E = 0;
        else
            m_E = value.m_E;
    }

    /**
     * Convert Float object to long number
     * 
     * @return long - number
     */
    public long toLong() {
        long tmpE = m_E;
        long tmpVal = m_Val;
        //
        while (tmpE != 0) {
            if (tmpE < 0) {
                tmpVal /= 10;
                tmpE++;
            } else {
                tmpVal *= 10;
                tmpE--;
            }
        }
        return tmpVal;
    }

    /**
     * Convert Float object to string without exponent
     * 
     * @return String - string
     */
    public String toShortString() {
        if (isError())
            return "NaN";
        //
        StringBuffer sb = new StringBuffer();
        sb.append(m_Val);
        int len = (int) m_E;
        if (len > 0) {
            for (int k = 0; k < len; k++)
                sb.append("0");
            len = 0;
        }
        //
        String str = sb.toString();
        len += str.length();
        //
        if (m_Val < 0L) {
            if (len > 1)
                return str.substring(0, len);
        } else {
            if (len > 0)
                return str.substring(0, len);
        }
        //
        return "0";
    }

    /**
     * Check value of current Float object is NaN
     * 
     * @return boolean - true-if NaN, false-if not
     */
    private boolean isError() {
        return (this.m_Val == ERROR.m_Val && this.m_E == ERROR.m_E);
    }

    /**
     * Convert Float object to string
     */
    public String toString() {
        if (isError())
            return "NaN";
        //
        RemoveZero();
        //
        Long l = new Long(m_Val);
        String str = l.toString();
        int len = str.length();
        boolean neg = false;
        if (m_Val < 0L) {
            neg = true;
            str = str.substring(1, len);
            len--;
        }
        //
        StringBuffer sb = new StringBuffer();
        //
        if (m_E < 0L) {
            int absE = (int) Math.abs(m_E);
            if (absE < len) {
                sb.append(str.substring(0, len - absE));
                sb.append(".");
                sb.append(str.substring(len - absE));
            } else {
                sb.append(str);
                for (int i = 0; i < (absE - len); i++)
                    sb.insert(0, "0");
                sb.insert(0, "0.");
            }
        } else {
            if (len + m_E > 6) {
                sb.append(str.charAt(0));
                if (str.length() > 1) {
                    sb.append(".");
                    sb.append(str.substring(1));
                } else
                    sb.append(".0");
                sb.append("E").append(len - 1 + m_E);
            } else {
                sb.append(str);
                for (int i = 0; i < m_E; i++)
                    sb.append("0");
            }
        }
        //
        str = sb.toString();
        sb = null;
        if (neg)
            str = "-" + str;
        //
        return str;
    }

    /**
     * Append value of argument to value of current Float object and return as
     * new Float object
     * 
     * @param value
     *            Float - argument
     * @return Float - current+value
     */
    public Float Add(Float value) {
        if (value.Equal(ZERO))
            return new Float(this);
        //
        long e1 = m_E;
        long e2 = value.m_E;
        long v1 = m_Val;
        long v2 = value.m_Val;
        // E must be equal in both operators
        while (e1 != e2) {
            if (e1 > e2) {
                if (Math.abs(v1) < maxLimit) {
                    v1 *= 10;
                    e1--;
                } else {
                    v2 /= 10;
                    e2++;
                }
            } else if (e1 < e2) {
                if (Math.abs(v2) < maxLimit) {
                    v2 *= 10;
                    e2--;
                } else {
                    v1 /= 10;
                    e1++;
                }
            }
        }
        //
        if ((v1 > 0 && v2 > Long.MAX_VALUE - v1)
                || (v1 < 0 && v2 < Long.MIN_VALUE - v1)) {
            v1 /= 10;
            e1++;
            v2 /= 10;
            e2++;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -