Nombre: Java big numbers.
Descripción:
Working with BigDecimal and BigInteger, Java large numbers.
URL: http://www.mygnet.net/codigos/java/analisisnumericos/java_big_numbers.2722
Código Fuente:
/*
L. I. Gerardo Ángeles Nava, Julio 09, 2008
Serie de Fibonacci
Using BigInteger and BigDecimal. big, large, places numbers.
**/
import java.math.*;
class fibonacciBigNumbers{
public static void main(String args[]){
double i=1;
int k = 0;
double j=0, t = 0;
double n = 11;
for(k = 1; k <= n; k++){
t = (i + j);
i = j;
System.out.println(k + "t" + i);
j = t;
}
System.out.println("nDouble, while-----------------------------------------n");
i=1; k = 0;
j=0; t = 0;
boolean Continuar = true;
while(Continuar){
t = (i + j);
i = j;
System.out.println(k + "t" + i);
String str = Double.toString(i);
if(str.indexOf("E50") >= 0){
Continuar = false;
}
j = t;
k++;
}
System.out.println("nBigInteger, example-----------------------------------------n");
System.out.println("Here's Long.MAX_VALUE:t" + Long.MAX_VALUE);
BigInteger bInt = new BigInteger("3419229223372036854775807");
System.out.println("Here's a bigger number:t" + bInt);
System.out.println("Here it is as a double:t" + bInt.doubleValue());
System.out.println("nBigDecimal, for-----------------------------------------n");
i=1; k = 0;
j=0; t = 0;
n = 250;
for(k = 1; k <= n; k++){
t = (i + j);
i = j;
BigDecimal dd = new BigDecimal(i);
System.out.println(k + "t" + dd);
j = t;
}
System.out.println("nBigDecimal, while-----------------------------------------n");
i=1; k = 0;
j=0; t = 0;
Continuar = true;
while(Continuar){
t = (i + j);
i = j;
BigDecimal dd = new BigDecimal(i);
System.out.println(k + "t" + dd + "t" + dd.toString().length());
if(dd.toString().length() == 50){
Continuar = false;
}
j = t;
k++;
}
System.out.println("nBigInteger, for-----------------------------------------n");
BigInteger bi = BigInteger.valueOf(1);
k = 0;
BigInteger bj = BigInteger.valueOf(0);
BigInteger bt = BigInteger.valueOf(0);
n = 11;
for(k = 1; k <= n; k++){
bt = bi.add(bj);
bi = bj;
System.out.println(k + "t" + bi + "t" + bi.toString().length());
bj = bt;
}
System.out.println("nBigInteger, while-----------------------------------------n");
bi = BigInteger.valueOf(1);
k = 0;
bj = BigInteger.valueOf(0);
bt = BigInteger.valueOf(0);
Continuar = true;
while(Continuar){
bt = bi.add(bj);
bi = bj;
System.out.println(k + "t" + bi + "t" + bi.toString().length());
if(bi.toString().length() == 50){
Continuar = false;
}
bj = bt;
k++;
}
}//public end
}//class end