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

📄 float.java

📁 J2me应用开发经典例子
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package com.ultrapower.math;

import java.lang.*;
/**
 * <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 慉bout?screen of your product</p>
 * <p>3. If you have web site please append link <a href=攈ttp://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 */
  final static public 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 */
  final static public Float PI=new Float(3141592653589793238L, -18L);
  /** Zero constant */
  final static public Float ZERO=new Float();
  /** One constant */
  final static public 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 */
  final static public Float LOG10 = new Float(2302585092994045684L, -18);
  //
  /** Pi/2 constant */
  final static public Float PIdiv2=PI.Div(2L);
  /** Pi/4 constant */
  final static public Float PIdiv4=PIdiv2.Div(2L);
  /** Pi/6 constant */
  final static public Float PIdiv6=PIdiv2.Div(3L);
  /** Pi/12 constant */
  final static public Float PIdiv12=PIdiv6.Div(2L);
  /** Pi*2 constant */
  final static public Float PImul2=PI.Mul(2L);
  /** Pi*4 constant */
  final static public Float PImul4=PI.Mul(4L);
  /** ln(0.5) constant */
  final static public Float LOGdiv2 = new Float(-6931471805599453094L, -19);
  /** Mantissa */
  public long m_Val;
  /** Exponent */
  public long m_E;
  /** Limit of value */
  private long maxLimit=Long.MAX_VALUE/100;
  //
  /**
   * Create object with zero inside
   */
  public 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
   */
  public 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
   */
  public 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"+(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++;
    }
    //
    if(v1>0 && v2>Long.MAX_VALUE-v1)
      return new Float(ERROR);
    else
    if(v1<0 && v2<Long.MIN_VALUE-v1)
      return new Float(ERROR);
    //
    return new Float(v1+v2, e1);
  }
  /**
   * Subtract value of argument from value of current Float object and return as new Float object
   * @param value Float - argument
   * @return Float - current-value
   */
  public Float Sub(Float value)
  {
    if(value.Equal(ZERO))
      return new Float(m_Val, m_E);
    return Add(new Float(-value.m_Val, value.m_E));
  }
  /**
   * Divide value of current Float object on argument and return as new Float object
   * @param value Float - argument
   * @return Float - current/value
   */
  public Float Mul(long value)
  {
    return Mul(new Float(value, 0));
  }
  /**
   * Multiply value of current Float object on argument and return as new Float object
   * @param value Float - argument
   * @return Float - current*value
   */
  public Float Mul(Float value)
  {
    if(value.Equal(ZERO) || this.Equal(ZERO))
      return new Float(ZERO);
    if(value.Equal(ONE))
      return new Float(this);
    //
    boolean negative1=(m_Val<0);
    if(negative1) m_Val=-m_Val;
    boolean negative2=(value.m_Val<0);
    if(negative2) value.m_Val=-value.m_Val;
    // Check overflow and underflow
    do
    {
      if(value.m_Val>m_Val)
      {
        if(Long.MAX_VALUE/m_Val<value.m_Val)
        {
          value.m_Val/=10;
          value.m_E++;
        }
        else
          break;
      }
      else
      {
        if(Long.MAX_VALUE/value.m_Val<m_Val)
        {
          m_Val/=10;
          m_E++;
        }
        else
          break;
      }
    } while(true);
    //
    if(negative1) m_Val=-m_Val;
    if(negative2) value.m_Val=-value.m_Val;
    //
    long e=m_E+value.m_E;
    long v=m_Val*value.m_Val;
    return new Float(v, e);
  }
  /**
   * Divide value of current Float object on argument and return as new Float object
   * @param value Float - argument
   * @return Float - current/value
   */
  public Float Div(long value)
  {
    return Div(new Float(value, 0));
  }
  /**
   * Divide value of current Float object on argument and return as new Float object
   * @param value Float - argument
   * @return Float - current/value
   */
  public Float Div(Float value)
  {
    if(value.Equal(ONE))
      return new Float(this);
    //
    long e1=m_E;
    long e2=value.m_E;
    long v2=value.m_Val;
    if(v2==0L)
      return new Float(ERROR);
    long v1=m_Val;
    if(v1==0L)
      return new Float(ZERO);
    //
    long val=0L;
    while(true)
    {
      val+=(v1/v2);
      v1%=v2;
      if(v1==0L || Math.abs(val)>(Long.MAX_VALUE/10L))
        break;
      if(Math.abs(v1)>(Long.MAX_VALUE/10L))
      {
        v2/=10L;
        e2++;
      }
      else
      {
        v1*=10L;
        e1--;
      }
      val*=10L;
    }
    //
    Float f=new Float(val, e1-e2);
    f.RemoveZero();
    return f;
  }
  public void RemoveZero()
  {
    if(m_Val==0)
      return;
    while ( m_Val%10 == 0 )
    {
     m_Val/=10;
     m_E++;
    }
  }
  /**
   * Is value of current Float object greater?
   * @param x Float - argument
   * @return boolean - true-if current value is greater x, false-if not
   */
  public boolean Great(Float x)
  {
    long e1=m_E;
    long e2=x.m_E;
    long v1=m_Val;
    long v2=x.m_Val;
    //
    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++;
        }
      }
    }
    //
    return v1>v2;
  }
  /**
   * Is value of current Float object less?
   * @param x Float - argument
   * @return boolean - true-if current value is less x, false-if not
   */
  public boolean Less(long x)
  {
    return Less(new Float(x, 0));
  }
  /**
   * Is value of current Float object less?
   * @param x Float - argument
   * @return boolean - true-if current value is less x, false-if not
   */
  public boolean Less(Float x)
  {
    long e1=m_E;
    long e2=x.m_E;
    long v1=m_Val;
    long v2=x.m_Val;
    //
    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++;
      }
    }
  }
  //
  return v1<v2;
  }
  /**
   * Equal with current Float object?
   * @param x Float - argument
   * @return boolean - true-if equal, false-if not
   */
  public boolean Equal(Float x)
  {

⌨️ 快捷键说明

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