在变量中存储大量数字的正确方法 [英] proper way to store large numbers in a variable
问题描述
我想研究数字,但无论是基本数字,我一直在写斐波那契数列的算法和用于查找素数的蛮力路径!
I would like to play around with numbers and however elementary, Ive been writing algorithms for the fibonacci sequence and a brute force path for finding prime numbers!
我不是程序员,只是一个数学家.
Im not a programmer, just a math guy.
但是,我经常遇到一个安静的问题,就是长而长的双排和浮子经常没有空间.
However, a problem I run into quiet often is that a long long, double and floats often run out of room.
如果我想继续在JAVA中工作,可以用什么方式创建自己的数据类型,以免空间不足.
If I wanted to continue to work in JAVA, in what way can I create my own data type so that I dont run out of room.
从概念上讲,我想像这样将3个双打组合在一起,
Conceptually, I thought to put 3 doubles together like so,
public class run {
static double a = 0;
static double b = 0;
//static double c = 0;
static void bignumber(boolean x) {
if (x == true && a < 999999999) {
++a;
} else if (x == true && a == 999999999) {
++b;
a = 0;
}
System.out.print(b + "." + a + " \n");
}
public static void main(String[] args) {
while(true) {
bignumber(true);
}
}
}
有没有更好的方法可以做到这一点,
is there a better way to do this,
我希望有一天能说
mydataType X = 18476997032117414743068356202001644030185493386634 10171471785774910651696711161249859337684305435744 58561606154457179405222971773252466096064694607124 96237204420222697567566873784275623895087646784409 33285157496578843415088475528298186726451339863364 93190808467199043187438128336350279547028265329780 29349161558118810498449083195195450098483937752272570 52578591944993870073695755688436933812779613089230 39256969525326162082367649031603655137144791393234 7169566988069
mydataType X = 18476997032117414743068356202001644030185493386634 10171471785774910651696711161249859337684305435744 58561606154457179405222971773252466096064694607124 96237204420222697567566873784275623895087646784409 33285157496578843415088475528298186726451339863364 93190808467199043187438128336350279547028265329780 29349161558118810498449083195450098483937752272570 52578591944993870073695755688436933812779613089230 39256969525326162082367649031603655137144791393234 7169566988069
或在此网站
我也尝试过
package main;
import java.math.BigInteger;
public class run {
BigDecimal a = 184769970321174147430683562020019566988069;
public static void main(String[] args) {
}
}
但是它似乎仍然超出范围
But it still seems to be out of range
推荐答案
为此目的使用BigDecimal
(而不是double
)和BigInteger
(而不是int
,long
),但是您只能通过他们的方法与他们合作.没有运算符,可以使用.
Use BigDecimal
(instead of double
), and BigInteger
(instead of int
, long
) for that purpose, But you can only work with them by their methods. No operators, can be used.
像这样使用:
BigInteger big = new BigInteger("4019832895734985478385764387592") // Strings...
big.add(new BigInteger("452872468924972568924762458767527");
与BigDecimal
这篇关于在变量中存储大量数字的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!