use of android.icu.math.BigDecimal in project j2objc by google.
the class DiagBigDecimalTest method diagpow.
/* ----------------------------------------------------------------- */
/**
* Test the {@link android.icu.math.BigDecimal#pow} method.
*/
@Test
public void diagpow() {
boolean flag;
android.icu.math.BigDecimal x;
android.icu.math.BigDecimal temp;
int n = 0;
android.icu.math.BigDecimal vx;
android.icu.math.BigDecimal vn;
java.lang.ArithmeticException ae = null;
flag = true;
TestFmwk.assertTrue("pow001", "1".equals((new android.icu.math.BigDecimal("0.3")).pow(new android.icu.math.BigDecimal("0"), mcdef).toString()));
TestFmwk.assertTrue("pow002", "0.3".equals((new android.icu.math.BigDecimal("0.3")).pow(new android.icu.math.BigDecimal("1"), mcdef).toString()));
TestFmwk.assertTrue("pow003", "0.3".equals((new android.icu.math.BigDecimal("0.3")).pow(new android.icu.math.BigDecimal("1.00"), mcdef).toString()));
TestFmwk.assertTrue("pow004", "0.09".equals((new android.icu.math.BigDecimal("0.3")).pow(new android.icu.math.BigDecimal("2.00"), mcdef).toString()));
TestFmwk.assertTrue("pow005", "0.09".equals((new android.icu.math.BigDecimal("0.3")).pow(new android.icu.math.BigDecimal("2.000000000"), mcdef).toString()));
TestFmwk.assertTrue("pow006", ("1E-8").equals((new android.icu.math.BigDecimal("10")).pow(new android.icu.math.BigDecimal("-8"), mcdef).toString()));
TestFmwk.assertTrue("pow007", ("1E-7").equals((new android.icu.math.BigDecimal("10")).pow(new android.icu.math.BigDecimal("-7"), mcdef).toString()));
TestFmwk.assertTrue("pow008", "0.000001".equals((new android.icu.math.BigDecimal("10")).pow(new android.icu.math.BigDecimal("-6"), mcdef).toString()));
TestFmwk.assertTrue("pow009", "0.00001".equals((new android.icu.math.BigDecimal("10")).pow(new android.icu.math.BigDecimal("-5"), mcdef).toString()));
TestFmwk.assertTrue("pow010", "0.0001".equals((new android.icu.math.BigDecimal("10")).pow(new android.icu.math.BigDecimal("-4"), mcdef).toString()));
TestFmwk.assertTrue("pow011", "0.001".equals((new android.icu.math.BigDecimal("10")).pow(new android.icu.math.BigDecimal("-3"), mcdef).toString()));
TestFmwk.assertTrue("pow012", "0.01".equals((new android.icu.math.BigDecimal("10")).pow(new android.icu.math.BigDecimal("-2"), mcdef).toString()));
TestFmwk.assertTrue("pow013", "0.1".equals((new android.icu.math.BigDecimal("10")).pow(new android.icu.math.BigDecimal("-1"), mcdef).toString()));
TestFmwk.assertTrue("pow014", "1".equals((new android.icu.math.BigDecimal("10")).pow(new android.icu.math.BigDecimal("0"), mcdef).toString()));
TestFmwk.assertTrue("pow015", "10".equals((new android.icu.math.BigDecimal("10")).pow(new android.icu.math.BigDecimal("1"), mcdef).toString()));
TestFmwk.assertTrue("pow016", "100000000".equals((new android.icu.math.BigDecimal("10")).pow(new android.icu.math.BigDecimal("8"), mcdef).toString()));
TestFmwk.assertTrue("pow017", ("1E+9").equals((new android.icu.math.BigDecimal("10")).pow(new android.icu.math.BigDecimal("9"), mcdef).toString()));
TestFmwk.assertTrue("pow018", ("1E+99").equals((new android.icu.math.BigDecimal("10")).pow(new android.icu.math.BigDecimal("99"), mcdef).toString()));
TestFmwk.assertTrue("pow019", ("1E+999999999").equals((new android.icu.math.BigDecimal("10")).pow(new android.icu.math.BigDecimal("999999999"), mcdef).toString()));
TestFmwk.assertTrue("pow020", ("1E+999999998").equals((new android.icu.math.BigDecimal("10")).pow(new android.icu.math.BigDecimal("999999998"), mcdef).toString()));
TestFmwk.assertTrue("pow021", ("1E+999999997").equals((new android.icu.math.BigDecimal("10")).pow(new android.icu.math.BigDecimal("999999997"), mcdef).toString()));
TestFmwk.assertTrue("pow022", ("1E+333333333").equals((new android.icu.math.BigDecimal("10")).pow(new android.icu.math.BigDecimal("333333333"), mcdef).toString()));
TestFmwk.assertTrue("pow023", ("1E-333333333").equals((new android.icu.math.BigDecimal("10")).pow(new android.icu.math.BigDecimal("-333333333"), mcdef).toString()));
TestFmwk.assertTrue("pow024", ("1E-999999998").equals((new android.icu.math.BigDecimal("10")).pow(new android.icu.math.BigDecimal("-999999998"), mcdef).toString()));
TestFmwk.assertTrue("pow025", ("1E-999999999").equals((new android.icu.math.BigDecimal("10")).pow(new android.icu.math.BigDecimal("-999999999"), mcdef).toString()));
TestFmwk.assertTrue("pow026", "0.5".equals((new android.icu.math.BigDecimal("2")).pow(new android.icu.math.BigDecimal("-1"), mcdef).toString()));
TestFmwk.assertTrue("pow027", "0.25".equals((new android.icu.math.BigDecimal("2")).pow(new android.icu.math.BigDecimal("-2"), mcdef).toString()));
TestFmwk.assertTrue("pow028", "0.0625".equals((new android.icu.math.BigDecimal("2")).pow(new android.icu.math.BigDecimal("-4"), mcdef).toString()));
TestFmwk.assertTrue("pow050", ((new android.icu.math.BigDecimal("0")).pow(new android.icu.math.BigDecimal("0"), mcdef).toString()).equals("1"));
TestFmwk.assertTrue("pow051", ((new android.icu.math.BigDecimal("0")).pow(new android.icu.math.BigDecimal("1"), mcdef).toString()).equals("0"));
TestFmwk.assertTrue("pow052", ((new android.icu.math.BigDecimal("0")).pow(new android.icu.math.BigDecimal("2"), mcdef).toString()).equals("0"));
TestFmwk.assertTrue("pow053", ((new android.icu.math.BigDecimal("1")).pow(new android.icu.math.BigDecimal("0"), mcdef).toString()).equals("1"));
TestFmwk.assertTrue("pow054", ((new android.icu.math.BigDecimal("1")).pow(new android.icu.math.BigDecimal("1"), mcdef).toString()).equals("1"));
TestFmwk.assertTrue("pow055", ((new android.icu.math.BigDecimal("1")).pow(new android.icu.math.BigDecimal("2"), mcdef).toString()).equals("1"));
TestFmwk.assertTrue("pow056", ((new android.icu.math.BigDecimal("0.3")).pow(new android.icu.math.BigDecimal("0"), mcdef).toString()).equals("1"));
TestFmwk.assertTrue("pow057", ((new android.icu.math.BigDecimal("10")).pow(new android.icu.math.BigDecimal("999999999"), mcdef).toString()).equals("1E+999999999"));
TestFmwk.assertTrue("pow058", ((new android.icu.math.BigDecimal("10")).pow(new android.icu.math.BigDecimal("999999998"), mcdef).toString()).equals("1E+999999998"));
TestFmwk.assertTrue("pow059", ((new android.icu.math.BigDecimal("10")).pow(new android.icu.math.BigDecimal("999999997"), mcdef).toString()).equals("1E+999999997"));
TestFmwk.assertTrue("pow060", ((new android.icu.math.BigDecimal("10")).pow(new android.icu.math.BigDecimal("333333333"), mcdef).toString()).equals("1E+333333333"));
TestFmwk.assertTrue("pow061", ((new android.icu.math.BigDecimal("10")).pow(new android.icu.math.BigDecimal("77"), mcdef).toString()).equals("1E+77"));
TestFmwk.assertTrue("pow062", ((new android.icu.math.BigDecimal("10")).pow(new android.icu.math.BigDecimal("22"), mcdef).toString()).equals("1E+22"));
TestFmwk.assertTrue("pow063", ((new android.icu.math.BigDecimal("10")).pow(new android.icu.math.BigDecimal("-77"), mcdef).toString()).equals("1E-77"));
TestFmwk.assertTrue("pow064", ((new android.icu.math.BigDecimal("10")).pow(new android.icu.math.BigDecimal("-22"), mcdef).toString()).equals("1E-22"));
TestFmwk.assertTrue("pow065", ((new android.icu.math.BigDecimal("2")).pow(new android.icu.math.BigDecimal("-1"), mcdef).toString()).equals("0.5"));
TestFmwk.assertTrue("pow066", ((new android.icu.math.BigDecimal("2")).pow(new android.icu.math.BigDecimal("-2"), mcdef).toString()).equals("0.25"));
TestFmwk.assertTrue("pow067", ((new android.icu.math.BigDecimal("2")).pow(new android.icu.math.BigDecimal("-4"), mcdef).toString()).equals("0.0625"));
TestFmwk.assertTrue("pow068", ((new android.icu.math.BigDecimal("6.0")).pow(new android.icu.math.BigDecimal("2"), mcdef).toString()).equals("36"));
TestFmwk.assertTrue("pow069", ((new android.icu.math.BigDecimal("-3")).pow(new android.icu.math.BigDecimal("2"), mcdef).toString()).equals("9"));
/* from book */
TestFmwk.assertTrue("pow070", ((new android.icu.math.BigDecimal("2")).pow(new android.icu.math.BigDecimal("2"), mcdef).pow(new android.icu.math.BigDecimal("3"), mcdef).toString()).equals("64"));
/* from book */
// 1998.12.14 Next test removed as pow() no longer rounds RHS [as per ANSI]
// Test('pow071').ok=BigDecimal('2').pow(BigDecimal('2.000000001'),mcdef).toString == '4'/* check input rounding */
/* General tests from original Rexx diagnostics */
x = new android.icu.math.BigDecimal("0.5");
temp = android.icu.math.BigDecimal.ONE;
flag = true;
{
n = 1;
for (; n <= 10; n++) {
temp = temp.multiply(x).divide(android.icu.math.BigDecimal.ONE);
flag = flag & (x.pow(new android.icu.math.BigDecimal(n), mcdef).toString()).equals(temp.toString());
}
}
/* n */
TestFmwk.assertTrue("pow080", flag);
x = new android.icu.math.BigDecimal("2");
temp = android.icu.math.BigDecimal.ONE;
flag = true;
{
n = 1;
for (; n <= 29; n++) {
temp = temp.multiply(x).divide(android.icu.math.BigDecimal.ONE);
flag = flag & (x.pow(new android.icu.math.BigDecimal(n), mcdef).toString()).equals(temp.toString());
flag = flag & (x.pow(new android.icu.math.BigDecimal(-n), mcdef).toString()).equals(android.icu.math.BigDecimal.ONE.divide(temp, mcdef).toString());
/* Note that rounding errors are possible for larger "n" */
/* due to the information content of the exponent */
}
}
/* n */
TestFmwk.assertTrue("pow081", flag);
/* The Vienna case. Checks both setup and 1/acc working precision */
// Modified 1998.12.14 as RHS no longer rounded before use (must fit)
// Modified 1990.02.04 as LHS is now rounded (instead of truncated to guard)
// lhs .. rounded to 1.23E+18
vx = new android.icu.math.BigDecimal("123456789E+10");
// rhs .. [was: -1.23455e+2, rounds to -123]
vn = new android.icu.math.BigDecimal("-1.23000e+2");
TestFmwk.assertTrue("pow090", (vx.pow(vn, mc3).toString()).equals("8.74E-2226"));
// - fixed point versions ---
TestFmwk.assertTrue("pow101", "1".equals((new android.icu.math.BigDecimal("0.3")).pow(new android.icu.math.BigDecimal("0")).toString()));
TestFmwk.assertTrue("pow102", "0.3".equals((new android.icu.math.BigDecimal("0.3")).pow(new android.icu.math.BigDecimal("1")).toString()));
TestFmwk.assertTrue("pow103", "0.3".equals((new android.icu.math.BigDecimal("0.3")).pow(new android.icu.math.BigDecimal("1.00")).toString()));
TestFmwk.assertTrue("pow104", "0.09".equals((new android.icu.math.BigDecimal("0.3")).pow(new android.icu.math.BigDecimal("2")).toString()));
TestFmwk.assertTrue("pow105", "0.09".equals((new android.icu.math.BigDecimal("0.3")).pow(new android.icu.math.BigDecimal("2.00")).toString()));
TestFmwk.assertTrue("pow106", "10".equals((new android.icu.math.BigDecimal("10")).pow(new android.icu.math.BigDecimal("1")).toString()));
TestFmwk.assertTrue("pow107", "100000000".equals((new android.icu.math.BigDecimal("10")).pow(new android.icu.math.BigDecimal("8")).toString()));
TestFmwk.assertTrue("pow108", "1000000000".equals((new android.icu.math.BigDecimal("10")).pow(new android.icu.math.BigDecimal("9")).toString()));
TestFmwk.assertTrue("pow109", "10000000000".equals((new android.icu.math.BigDecimal("10")).pow(new android.icu.math.BigDecimal("10")).toString()));
TestFmwk.assertTrue("pow110", "1".equals((new android.icu.math.BigDecimal("2")).pow(new android.icu.math.BigDecimal("0")).toString()));
TestFmwk.assertTrue("pow111", "16".equals((new android.icu.math.BigDecimal("2")).pow(new android.icu.math.BigDecimal("4")).toString()));
TestFmwk.assertTrue("pow112", "256".equals((new android.icu.math.BigDecimal("2")).pow(new android.icu.math.BigDecimal("8")).toString()));
TestFmwk.assertTrue("pow113", "1024".equals((new android.icu.math.BigDecimal("2")).pow(new android.icu.math.BigDecimal("10")).toString()));
TestFmwk.assertTrue("pow114", "1.0510100501".equals((new android.icu.math.BigDecimal("1.01")).pow(new android.icu.math.BigDecimal("5")).toString()));
TestFmwk.assertTrue("pow120", ((new android.icu.math.BigDecimal("0")).pow(new android.icu.math.BigDecimal("0")).toString()).equals("1"));
TestFmwk.assertTrue("pow121", ((new android.icu.math.BigDecimal("0")).pow(new android.icu.math.BigDecimal("1")).toString()).equals("0"));
TestFmwk.assertTrue("pow122", ((new android.icu.math.BigDecimal("0")).pow(new android.icu.math.BigDecimal("2")).toString()).equals("0"));
TestFmwk.assertTrue("pow123", ((new android.icu.math.BigDecimal("1")).pow(new android.icu.math.BigDecimal("0")).toString()).equals("1"));
TestFmwk.assertTrue("pow144", ((new android.icu.math.BigDecimal("1")).pow(new android.icu.math.BigDecimal("1")).toString()).equals("1"));
TestFmwk.assertTrue("pow125", ((new android.icu.math.BigDecimal("1")).pow(new android.icu.math.BigDecimal("2")).toString()).equals("1"));
TestFmwk.assertTrue("pow126", ((new android.icu.math.BigDecimal("0.3")).pow(new android.icu.math.BigDecimal("0")).toString()).equals("1"));
TestFmwk.assertTrue("pow127", ((new android.icu.math.BigDecimal("10")).pow(new android.icu.math.BigDecimal("7")).toString()).equals("10000000"));
TestFmwk.assertTrue("pow128", ((new android.icu.math.BigDecimal("6.0")).pow(new android.icu.math.BigDecimal("2")).toString()).equals("36.00"));
TestFmwk.assertTrue("pow129", ((new android.icu.math.BigDecimal("6.00")).pow(new android.icu.math.BigDecimal("2")).toString()).equals("36.0000"));
TestFmwk.assertTrue("pow130", ((new android.icu.math.BigDecimal("6.000")).pow(new android.icu.math.BigDecimal("2")).toString()).equals("36.000000"));
TestFmwk.assertTrue("pow131", ((new android.icu.math.BigDecimal("-3")).pow(new android.icu.math.BigDecimal("2")).toString()).equals("9"));
TestFmwk.assertTrue("pow132", ((new android.icu.math.BigDecimal("2")).pow(new android.icu.math.BigDecimal("2")).pow(new android.icu.math.BigDecimal("3")).toString()).equals("64"));
/* errors */
try {
ten.pow((android.icu.math.BigDecimal) null);
flag = false;
} catch (java.lang.NullPointerException $67) {
flag = true;
}
/* checknull */
TestFmwk.assertTrue("pow150", flag);
try {
ten.pow(ten, (android.icu.math.MathContext) null);
flag = false;
} catch (java.lang.NullPointerException $68) {
flag = true;
}
/* checknull2 */
TestFmwk.assertTrue("pow151", flag);
flag = true;
try {
tenlong.pow(android.icu.math.BigDecimal.ONE, mcld);
flag = false;
} catch (java.lang.ArithmeticException $69) {
ae = $69;
flag = (ae.getMessage()).equals("Too many digits:" + " " + tenlong.toString());
}
/* checkdigits */
TestFmwk.assertTrue("pow152", flag);
try {
android.icu.math.BigDecimal.ONE.pow(tenlong, mcld);
flag = false;
} catch (java.lang.ArithmeticException $70) {
ae = $70;
flag = (ae.getMessage()).equals("Too many digits:" + " " + tenlong.toString());
}
/* checkdigits */
TestFmwk.assertTrue("pow153", flag);
try {
android.icu.math.BigDecimal.ONE.pow(new android.icu.math.BigDecimal("-71"));
flag = false;
} catch (java.lang.ArithmeticException $71) {
ae = $71;
flag = (ae.getMessage()).equals("Negative power: -71");
}
/* checkpos */
TestFmwk.assertTrue("pow154", flag);
try {
android.icu.math.BigDecimal.ONE.pow(new android.icu.math.BigDecimal("1234"), mc3);
flag = false;
} catch (java.lang.ArithmeticException $72) {
ae = $72;
flag = (ae.getMessage()).equals("Too many digits: 1234");
}
/* checkwhole */
TestFmwk.assertTrue("pow155", flag);
try {
android.icu.math.BigDecimal.ONE.pow(new android.icu.math.BigDecimal("12.34e+2"), mc3);
flag = false;
} catch (java.lang.ArithmeticException $73) {
ae = $73;
flag = (ae.getMessage()).equals("Too many digits: 1.234E+3");
}
/* checkwhole1 */
TestFmwk.assertTrue("pow156", flag);
try {
android.icu.math.BigDecimal.ONE.pow(new android.icu.math.BigDecimal("12.4"), mcdef);
flag = false;
} catch (java.lang.ArithmeticException $74) {
ae = $74;
flag = (ae.getMessage()).equals("Decimal part non-zero: 12.4");
}
/* checkwhole2 */
TestFmwk.assertTrue("pow157", flag);
try {
android.icu.math.BigDecimal.ONE.pow(new android.icu.math.BigDecimal("1.01"), mcdef);
flag = false;
} catch (java.lang.ArithmeticException $75) {
ae = $75;
flag = (ae.getMessage()).equals("Decimal part non-zero: 1.01");
}
/* checkwhole3 */
TestFmwk.assertTrue("pow158", flag);
try {
android.icu.math.BigDecimal.ONE.pow(new android.icu.math.BigDecimal("1.000000001"), mcdef);
flag = false;
} catch (java.lang.ArithmeticException $76) {
ae = $76;
flag = (ae.getMessage()).equals("Decimal part non-zero: 1.000000001");
}
/* checkwhole4 */
TestFmwk.assertTrue("pow159", flag);
try {
android.icu.math.BigDecimal.ONE.pow(new android.icu.math.BigDecimal("1.000000001"), mc3);
flag = false;
} catch (java.lang.ArithmeticException $77) {
ae = $77;
flag = (ae.getMessage()).equals("Decimal part non-zero: 1.000000001");
}
/* checkwhole5 */
TestFmwk.assertTrue("pow160", flag);
try {
android.icu.math.BigDecimal.ONE.pow(new android.icu.math.BigDecimal("5.67E-987654321"), mc3);
flag = false;
} catch (java.lang.ArithmeticException $78) {
ae = $78;
flag = (ae.getMessage()).equals("Decimal part non-zero: 5.67E-987654321");
}
/* checkwhole6 */
TestFmwk.assertTrue("pow161", flag);
}
use of android.icu.math.BigDecimal in project j2objc by google.
the class UniversalTimeScale method toBigDecimal.
/**
* Convert a datetime from the universal time scale to a <code>BigDecimal</code> in the given time scale.
*
* @param universalTime The datetime in the universal time scale
* @param timeScale The time scale to convert to
*
* @return The datetime converted to the given time scale
*/
public static BigDecimal toBigDecimal(BigDecimal universalTime, int timeScale) {
TimeScaleData data = getTimeScaleData(timeScale);
BigDecimal units = new BigDecimal(data.units);
BigDecimal epochOffset = new BigDecimal(data.epochOffset);
return universalTime.divide(units, BigDecimal.ROUND_HALF_UP).subtract(epochOffset);
}
use of android.icu.math.BigDecimal in project j2objc by google.
the class UniversalTimeScale method bigDecimalFrom.
/**
* Convert a <code>double</code> datetime from the given time scale to the universal time scale.
* All calculations are done using <code>BigDecimal</code> to guarantee that the value
* does not go out of range.
*
* @param otherTime The <code>double</code> datetime
* @param timeScale The time scale to convert from
*
* @return The datetime converted to the universal time scale
*/
public static BigDecimal bigDecimalFrom(double otherTime, int timeScale) {
TimeScaleData data = getTimeScaleData(timeScale);
BigDecimal other = new BigDecimal(String.valueOf(otherTime));
BigDecimal units = new BigDecimal(data.units);
BigDecimal epochOffset = new BigDecimal(data.epochOffset);
return other.add(epochOffset).multiply(units);
}
use of android.icu.math.BigDecimal in project j2objc by google.
the class UniversalTimeScale method bigDecimalFrom.
/**
* Convert a <code>BigDecimal</code> datetime from the given time scale to the universal time scale.
* All calculations are done using <code>BigDecimal</code> to guarantee that the value
* does not go out of range.
*
* @param otherTime The <code>BigDecimal</code> datetime
* @param timeScale The time scale to convert from
*
* @return The datetime converted to the universal time scale
*/
public static BigDecimal bigDecimalFrom(BigDecimal otherTime, int timeScale) {
TimeScaleData data = getTimeScaleData(timeScale);
BigDecimal units = new BigDecimal(data.units);
BigDecimal epochOffset = new BigDecimal(data.epochOffset);
return otherTime.add(epochOffset).multiply(units);
}
use of android.icu.math.BigDecimal in project j2objc by google.
the class TimeScaleAPITest method TestToBigDecimalFromBigDecimal.
@Test
public void TestToBigDecimalFromBigDecimal() {
BigDecimal bigZero = new BigDecimal(0);
try {
UniversalTimeScale.toBigDecimal(bigZero, -1);
errln("toBigDecimal(bigZero, -1) did not throw IllegalArgumentException.");
} catch (IllegalArgumentException iae) {
logln("PASS: UniversalTimeScale.toBigDecimal failed as expected");
}
for (int scale = 0; scale < UniversalTimeScale.MAX_SCALE; scale += 1) {
try {
UniversalTimeScale.toBigDecimal(bigZero, scale);
} catch (IllegalArgumentException iae) {
errln("toBigDecimal(bigZero, " + scale + ") threw IllegalArgumentException.");
}
}
try {
UniversalTimeScale.toBigDecimal(bigZero, UniversalTimeScale.MAX_SCALE);
errln("toBigDecimal(bigZero, MAX_SCALE) did not throw IllegalArgumetException.");
} catch (IllegalArgumentException iae) {
logln("PASS: UniversalTimeScale.toBigDecimal failed as expected");
}
}
Aggregations