📄 float.java
字号:
* @return Float
*/
static private Float _log(Float x) {
if (!x.Great(ZERO))
return new Float(ERROR);
//
Float f = new Float(ZERO);
// Make x to close at 1
int appendix = 0;
while (x.Great(ZERO) && x.Less(ONE)) {
x = x.Mul(2);
appendix++;
}
//
x = x.Div(2);
appendix--;
//
Float y1 = x.Sub(ONE);
Float y2 = x.Add(ONE);
Float y = y1.Div(y2);
//
Float k = new Float(y);
y2 = k.Mul(y);
//
for (long i = 1; i < 50; i += 2) {
f = f.Add(k.Div(i));
k = k.Mul(y2);
}
//
f = f.Mul(2);
for (int i = 0; i < appendix; i++)
f = f.Add(LOGdiv2);
//
return f;
}
/**
* Returns the natural logarithm (base e) of a double value. Special cases:
* If the argument is NaN or less than zero, then the result is NaN. If the
* argument is positive infinity, then the result is positive infinity. If
* the argument is positive zero or negative zero, then the result is
* negative infinity. A result must be within 1 ulp of the correctly rounded
* result. Results must be semi-monotonic
*
* @param x
* Float - a number greater than 0.0
* @return Float - the value ln(x), the natural logarithm of x
*/
private static Float log(Float x) {
if (!x.Great(ZERO))
return new Float(ERROR);
//
if (x.Equal(ONE))
return new Float(ZERO);
//
if (x.Great(Float.ONE)) {
x = ONE.Div(x);
return _log(x).Neg();
}
return _log(x);
}
static public Float log10(Float x) {
if (!x.Great(ZERO))
return new Float(ERROR);
//
if (x.Equal(ONE))
return new Float(ZERO);
//
Float f = log(x);
if (f.isError())
return f;
return f.Div(LOG10);
}
/*
* static public Float log10(Float x) { if(!x.Great(ZERO)) return new
* Float(ERROR); // boolean neg=false; if(x.m_Val<0) { neg=true;
* x.m_Val=-x.m_Val; } // int index=0; if(x.Great(Float.ONE)) { // ������ 1
* while(x.Great(Float.ONE)) { x=x.Div(10); index++; } } else { // ������
* ��� ����� 1 while(x.Less(Float.ONE)) { x=x.Mul(10); index--; } } // Float
* res=new Float(index); if(!x.Equal(ONE)) res=res.Add(log(x).Div(LOG10));
* // if(neg) return Float.ONE.Div(res); else return res; }
*/
// precise y=3.5
// x=15 diff=0.06%
// x=20 diff=0.40%
// x=25 diff=1.31%
// x=30 diff=2.95%
// if x negative y must be integer value
/**
* Returns the value of the first argument raised to the power of the second
* argument. Special cases: If the second argument is positive or negative
* zero, then the result is 1.0. If the second argument is 1.0, then the
* result is the same as the first argument. If the second argument is NaN,
* then the result is NaN. If the first argument is NaN and the second
* argument is nonzero, then the result is NaN. If the absolute value of the
* first argument is greater than 1 and the second argument is positive
* infinity, or the absolute value of the first argument is less than 1 and
* the second argument is negative infinity, then the result is positive
* infinity. If the absolute value of the first argument is greater than 1
* and the second argument is negative infinity, or the absolute value of
* the first argument is less than 1 and the second argument is positive
* infinity, then the result is positive zero. If the absolute value of the
* first argument equals 1 and the second argument is infinite, then the
* result is NaN. If the first argument is positive zero and the second
* argument is greater than zero, or the first argument is positive infinity
* and the second argument is less than zero, then the result is positive
* zero. If the first argument is positive zero and the second argument is
* less than zero, or the first argument is positive infinity and the second
* argument is greater than zero, then the result is positive infinity. If
* the first argument is negative zero and the second argument is greater
* than zero but not a finite odd integer, or the first argument is negative
* infinity and the second argument is less than zero but not a finite odd
* integer, then the result is positive zero. If the first argument is
* negative zero and the second argument is a positive finite odd integer,
* or the first argument is negative infinity and the second argument is a
* negative finite odd integer, then the result is negative zero. If the
* first argument is negative zero and the second argument is less than zero
* but not a finite odd integer, or the first argument is negative infinity
* and the second argument is greater than zero but not a finite odd
* integer, then the result is positive infinity. If the first argument is
* negative zero and the second argument is a negative finite odd integer,
* or the first argument is negative infinity and the second argument is a
* positive finite odd integer, then the result is negative infinity. If the
* first argument is finite and less than zero if the second argument is a
* finite even integer, the result is equal to the result of raising the
* absolute value of the first argument to the power of the second argument
* if the second argument is a finite odd integer, the result is equal to
* the negative of the result of raising the absolute value of the first
* argument to the power of the second argument if the second argument is
* finite and not an integer, then the result is NaN. If both arguments are
* integers, then the result is exactly equal to the mathematical result of
* raising the first argument to the power of the second argument if that
* result can in fact be represented exactly as a double value. (In the
* foregoing descriptions, a floating-point value is considered to be an
* integer if and only if it is finite and a fixed point of the method ceil
* or, equivalently, a fixed point of the method floor. A value is a fixed
* point of a one-argument method if and only if the result of applying the
* method to the value is equal to the value.) A result must be within 1 ulp
* of the correctly rounded result. Results must be semi-monotonic.
*
* @param x
* Float - the base
* @param y
* Float - the exponent
* @return Float - the value a^b
*/
static public Float pow(Float x, Float y) {
if (x.Equal(ZERO))
return new Float(ZERO);
if (x.Equal(ONE))
return new Float(ONE);
if (y.Equal(ZERO))
return new Float(ONE);
if (y.Equal(ONE))
return new Float(x);
//
long l = y.toLong();
boolean integerValue = y.Equal(new Float(l));
//
if (integerValue) {
boolean neg = false;
if (y.Less(0))
neg = true;
//
Float result = new Float(x);
for (long i = 1; i < (neg ? -l : l); i++)
result = result.Mul(x);
//
if (neg)
return ONE.Div(result);
else
return result;
} else {
if (x.Great(ZERO))
return exp(y.Mul(log(x)));
else
return new Float(ERROR);
}
}
/**
* Returns the smallest (closest to negative infinity) double value that is
* not less than the argument and is equal to a mathematical integer.
* Special cases: If the argument value is already equal to a mathematical
* integer, then the result is the same as the argument. If the argument is
* NaN or an infinity or positive zero or negative zero, then the result is
* the same as the argument. If the argument value is less than zero but
* greater than -1.0, then the result is negative zero
*
* @param x
* Float - a value
* @return Float - the smallest (closest to negative infinity)
* floating-point value that is not less than the argument and is
* equal to a mathematical integer
*/
static public Float ceil(Float x) {
long tmpVal = x.m_Val;
//
if (x.m_E < 0) {
long coeff = 1;
//
if (x.m_E > -19) {
for (long i = 0; i < -x.m_E; i++)
coeff *= 10;
tmpVal /= coeff;
tmpVal *= coeff;
if (x.m_Val - tmpVal > 0)
tmpVal += coeff;
} else if (tmpVal > 0)
return ONE;
else
return ZERO;
}
//
return new Float(tmpVal, x.m_E);
}
/**
* Returns the largest (closest to positive infinity) double value that is
* not greater than the argument and is equal to a mathematical integer.
* Special cases: If the argument value is already equal to a mathematical
* integer, then the result is the same as the argument. If the argument is
* NaN or an infinity or positive zero or negative zero, then the result is
* the same as the argument
*
* @param x
* Float - a value
* @return Float - the largest (closest to positive infinity) floating-point
* value that is not greater than the argument and is equal to a
* mathematical integer
*/
static public Float floor(Float x) {
long tmpVal = x.m_Val;
//
if (x.m_E < 0) {
long coeff = 1;
//
if (x.m_E > -19) {
for (long i = 0; i < -x.m_E; i++)
coeff *= 10;
tmpVal /= coeff;
tmpVal *= coeff;
if (x.m_Val - tmpVal < 0)
tmpVal -= coeff;
} else if (tmpVal < 0)
return ONE.Neg();
else
return ZERO;
}
//
return new Float(tmpVal, x.m_E);
}
/**
* @param val
* Float - The value to round with the optional number of decimal
* digits to round to is set default to 0
* @return Float - The rounded value
*/
public static Float round(Float val) {
return Float.round(val, 0);
}
/**
* @param val
* Float - The value to round
* @param precision
* int - The optional number of decimal digits to round to,
* defaults to 0
* @return Float - The rounded value
*/
public static Float round(Float val, int precision) {
if (precision != 0) {
val = val.Mul(Float.pow(new Float(10), new Float(precision)));
val = val.Add(new Float(5, -1));
val = Float.floor(val);
val = val.Div(Float.pow(new Float(10), new Float(precision)));
} else {
val = val.Add(new Float(5, -1));
val = Float.floor(val);
}
return val;
}
/**
* Returns the absolute value of a Float object. If the argument is not
* negative, the argument is returned. If the argument is negative, the
* negation of the argument is returned. Special cases: If the argument is
* positive zero or negative zero, the result is positive zero. If the
* argument is infinite, the result is positive infinity. If the argument is
* NaN, the result is NaN
*
* @param x
* Float - the argument whose absolute value is to be determined
* @return Float - the absolute value of the argument
*/
static public Float abs(Float x) {
if (x.m_Val < 0)
return x.Neg();
return new Float(x);
}
/**
* Integer part of Float object
*
* @param x
* Float - source Float object
* @return Float - result Float object
*/
private static Float Int(Float x) {
long tmpVal = x.m_Val;
//
if (x.m_E < 0) {
long coeff = 1;
//
if (x.m_E > -19) {
for (long i = 0; i < -x.m_E; i++)
coeff *= 10;
tmpVal /= coeff;
tmpVal *= coeff;
} else
return Float.ZERO;
}
//
return new Float(tmpVal, x.m_E);
}
/**
* Fractional part of Float object
*
* @param x
* Float - source Float object
* @return Float - result Float object
*/
static public Float Frac(Float x) {
return x.Sub(Int(x));
}
/**
* Converts an angle measured in degrees to an approximately equivalent
* angle measured in radians. The conversion from degrees to radians is
* generally inexact
*
* @param x
* Float - an angle, in degrees
* @return Float - the measurement of the angle x in radians
*/
static public Float toRadians(Float x) {
return x.Mul(PI).Div(180L);
}
/**
* Converts an angle measured in radians to an approximately equivalent
* angle measured in degrees. The conversion from radians to degrees is
* generally inexact; users should not expect cos(toRadians(90.0)) to
* exactly equal 0.0
*
* @param x
* Float - an angle, in radians
* @return Float - the measurement of the angle angrad in degrees
*/
static public Float toDegrees(Float x) {
return x.Mul(180L).Div(PI);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -