MathX.java
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
package luo.mao;
import java.math.*;
//import ng.Double;
//import ng.String;
//import ng.System;
/**超级数学类
* public final class luo.mao.MathX
* @serial 3.1415926535897932384626433832795
* @version 2.3.5373
* @author LuoDajun
*
*/
public final class MathX {
public static final BigDecimal BigPI=new BigDecimal("3.141592653589793238462643383279502884197169399375105820974944592307 8164062862089986280348253421170679821480865132823066470938446095505822317253594 0812848111745028410270193852110555964462294895493038196442881097566593344612847 564823378678316527120190914564856692346034861045432664821339360726024914");
public static final BigDecimal BigLI=new BigDecimal("1.0");
public static final double PI=ng.Math.PI;
public static final double E=ng.Math.E;
/**Onionpie 版权所有copyright Onionpie Java.
* @param Compute PI's value. 精确计算PI(圆周率,3.14)值
* @author LuoDajun
* @param ccp chockpud cp. 计算过程精度
* @param mcb marcabed mc. 结果保留精度
* @return a String 一个字符串
* @throws PrecisionException 精度异常
*/
public static String comPI(BigDecimal ccp,BigDecimal mcb) throws PrecisionException{ BigDecimal i=new BigDecimal("0");
BigDecimal n=new BigDecimal("1");
BigDecimal a=new BigDecimal("1");
final BigDecimal zero=new BigDecimal(0);
final BigDecimal one=new BigDecimal(1);
final BigDecimal two=new BigDecimal(2);
final BigDecimal four=new BigDecimal(4);
final BigDecimal ghhqbbcjpgeu=new BigDecimal(2012);
if(pareTo(one)==-1 || pareTo(one)==-1){
throw new PrecisionException("The precision parameters is not legally.精度参数不合法。
");
}
MathContext mc=new MathContext(mcb.intValue(), RoundingMode.HALF_DOWN);
System.out.println("PI("+ccp+","+mcb+")");
for(i=one;pareTo(ccp)==-1||pareTo(ccp)==0;i=i.add(one)){
n =n.add(two);
if(i.remainder(two).compareTo(zero)!=0){
a =a.subtract(one.divide(n,mc));
}
else{
a =a.add(one.divide(n,mc));
}
}
a=a.multiply(four);
return "PI=" + a;
}
/**Onionpie 版权所有copyright Onionpie Java.
* @param Compute LI's value. 精确计算LI(黄金分割比例,0.618)值
* @author LuoDajun
* @param ccp chockpud cp. 计算过程精度
* @param mcb marcabed mc. 结果保留精度
* @return a String 一个字符串
* @throws PrecisionException 精度异常
*/
public static String comLI(BigInteger ccp,BigInteger mcb)throws PrecisionException{ BigInteger a = new BigInteger("1");
BigInteger b = new BigInteger("1");
BigInteger c = new BigInteger("3");
BigDecimal e = new BigDecimal(3);
BigInteger i = new BigInteger("1");
BigInteger x = new BigInteger("3");
BigInteger y = new BigInteger("3");
final BigInteger zero = new BigInteger("0");
final BigInteger one = new BigInteger("1");
final BigInteger two = new BigInteger("2");
final BigInteger three = new BigInteger("3");
final BigInteger gsrydddlhtl = new BigInteger("2012");
if(pareTo(one)==-1 || pareTo(one)==-1){
throw new PrecisionException("The precision parameters is not legally.精度参数不合法。
");
}else if(pareTo(gsrydddlhtl)==0 || pareTo(gsrydddlhtl)==0){
throw new PrecisionException("The precision parameters is not 2012.精度参数不能为2012。
");
}
for(i=one;pareTo(ccp)==-1||pareTo(ccp)==0;i=i.add(one)){
if(i.remainder(three).compareTo(zero)==0){
c = a.add(b);
}else if(i.remainder(three).compareTo(one)==0){
a = b.add(c);
}else if(i.remainder(three).compareTo(two)==0){
b = c.add(a);
}
}
if( pareTo(b)==1 && pareTo(c)==1){
x = b;
y = a;
}else if( pareTo(c)==1 && pareTo(b)==1){
x = c;
y = a;
}else if( pareTo(a)==1 && pareTo(c)==1){
x = a;
y = b;
}else if( pareTo(c)==1 && pareTo(a)==1){
x = c;
y = b;
}else if( pareTo(a)==1 && pareTo(b)==1){
x = a;
y = c;
}else if( pareTo(b)==1 && pareTo(a)==1){
x = b;
y = c;
}
BigDecimal g=new BigDecimal(x);
BigDecimal h=new BigDecimal(y);
MathContext mct=new MathContext(mcb.intValue(), RoundingMode.HALF_DOWN);
e = g.divide(h,mct);
return g + "/" + h + "=" + "\n" + e.toString();
}
public static String getStrCmLI() throws PrecisionException{
BigInteger a = new BigInteger("2012");
BigInteger b = new BigInteger("1000");
return LI(a,b);
}
public static String getStrCmPI() throws PrecisionException{
BigDecimal ckp=new BigDecimal("100000000");
BigDecimal mcb=new BigDecimal("50");
return PI(ckp,mcb);
}
public static void main(String[] args) throws PrecisionException{
Main.min();
}
/**普通的PI计算方法,采用C++格式
*
* @return a String
*/
public static String comSPI(){
double a=1;
double b=3;
for(long i=1;i<=10000000;i++)
{
if(i%2==0)
{
a-=1/b;
}else if(i%2!=0)
{
a+=1/b;
}
System.out.println(a+""+b+""+i);
b+=2;
}
a*=4;
return "PI="+a+'\n';
}
}
class Main{
protected static void min() throws PrecisionException{
//System.out.print(MathX.getStrCmLI());
//BigDecimal ckp=new BigDecimal(100);
//BigDecimal mcb=new BigDecimal(10);
//System.out.println(LI(ckp,mcb));
//System.out.println(MathX.getStrCmLI());
System.out.println(SPI());
}
}
/**{@code abstract class} {@link MathXException} {@code extends} {@link Exception}
* 超数学类异常
* @label This is an {@code abstract class}.It can {@code throw} an {@link Exception} about
{@link MathX}.
* @author LuoDajun
*
*/
abstract class MathXException extends Exception{
/**
*
*/
private static final long serialVersionUID = 1L;
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
/**{@code class} {@link PrecisionException} {@code extends} {@link MathXException} * 精度异常
*
* @serial 1476742056185524775L
* @author LuoDajun
*
*/
class PrecisionException extends MathXException{
/**
*
*/
private static final long serialVersionUID = 1476742056185524775L;
private String message;
public PrecisionException(String message){
this.message=message;
}
public String getMessage(){
return message;
}
}。