有没有办法在 R 中精确地存储大量数字?
double存储为二进制小数,其精度随值而变化,整数的范围限制为 4 个字节。
如果我想精确地存储一个非常大的数字怎么办?
在我的一个课程中,我有一个例程来读取和写入 Decimal 类型的数组(使用BinaryReader/BinaryWriter和方法,即:ReadDecimal()Write()
BinaryReader inputReader = new BinaryReader(File.OpenRead(BaseFilePath));
for (int x = 0; x < 6; x++) {
for (int y = 0; y < m_Codes[x].GetLength(0); y++) {
for (int z = 0; z < m_Codes[x].GetLength(1); z++) {
m_Codes[x][y, z] = inputReader.ReadDecimal();
}
}
}
Run Code Online (Sandbox Code Playgroud)
和
for (int x = 0; x < 6; x++) {
for (int y = 0; y < m_Codes[x].GetLength(0); y++) {
for (int z = 0; z < m_Codes[x].GetLength(1); …Run Code Online (Sandbox Code Playgroud) 我玩java.math.BigInteger.这是我的java类,
public class BigIntegerTest{
public static void main(String[] args) {
BigInteger fiveThousand = new BigInteger("5000");
BigInteger fiftyThousand = new BigInteger("50000");
BigInteger fiveHundredThousand = new BigInteger("500000");
BigInteger total = BigInteger.ZERO;
total.add(fiveThousand);
total.add(fiftyThousand);
total.add(fiveHundredThousand);
System.out.println(total);
}
}
Run Code Online (Sandbox Code Playgroud)
我认为结果是555000.但实际是0.为什么?
我正在玩Java 8,并使用程序比较Java 6中的一些东西来计算大型列表中偶数的总和
Java 8
public class ListOperationsJava8 {
static List<BigInteger> list = new LinkedList<>();
public static void main(String[] args) {
createList();
long start = System.currentTimeMillis();
/*System.out.println(list.parallelStream().
filter(n -> n.mod(new BigInteger("2")).equals(BigInteger.ZERO)).
mapToInt(BigInteger::intValue).sum()); --> gives result -1795017296 */
System.out.println(list.parallelStream().
filter(n -> n.mod(new BigInteger("2")).equals(BigInteger.ZERO)).
mapToLong(BigInteger::longValue).sum());
long end = System.currentTimeMillis();
System.out.println("Time taken using Java 8: " + (end - start) + " ms");
}
private static void createList() {
for (int i = 0; i < 100000; i++) {
list.add(new BigInteger(String.valueOf(i)));
}
} …Run Code Online (Sandbox Code Playgroud) 欢迎。我正在尝试实施 MillerRabin 测试来检查给定的大数是否为素数。这是我的代码:
public static bool MillerRabinTest(BigInteger number)
{
BigInteger d;
var n = number - 1;
var s = FindK(n, out d);
BigInteger a = 2;
BigInteger y = Calc(a, d, number); //a^d mod number
if (y != BigInteger.One && y != n)
{
for (var r = 1; r <= s - 1; r++)
{
y = Calc(y, 2, number);
if (y == 1)
return false;
}
if (y != n)
return false;
}
return true; //it is …Run Code Online (Sandbox Code Playgroud) 我正在解决这个问题,他们要求第一个斐波纳契数为1000位的索引,我的第一个想法是类似于:
BigInteger x = 1;
BigInteger y = 1;
BigInteger tmp = 0;
int currentIndex = 2;
while (x.NoOfDigits < 1000)
{
tmp = x + y;
y = x;
x = tmp;
currentIndex++;
}
return currentIndex;
Run Code Online (Sandbox Code Playgroud)
但是,据我所知,没有方法可以计算BigInteger的位数.这是真的?绕过它的一种方法是使用BigInteger的.ToString().Length方法,但我被告知字符串处理很慢.
BigInteger也有一个.ToByteArray(),我想把BigInteger转换成一个字节数组并检查该数组的长度 - 但我不认为这唯一地决定了BigInteger中的位数.
为了它的价值,我实现了另一种解决方法,即手动将Fibonacci数存储在数组中,并在数组填满后立即停止,并将其与基于.ToString的方法进行比较,大约为2.5时间慢,但第一种方法需要0.1秒,这也似乎很长一段时间.
编辑:我已经在下面的答案中测试了这两个建议(一个是BigInteger.Log,一个是MaxLimitMethod).我得到以下运行时间:
程序
using System;
using System.Collections.Generic;
using System.Numerics;
using System.Diagnostics;
class Program
{
static void Main(string[] args)
{
Stopwatch clock = new Stopwatch();
clock.Start();
int index1 = Algorithms.IndexOfNDigits(1000);
clock.Stop();
var elapsedTime1 = …Run Code Online (Sandbox Code Playgroud) 这对我来说很奇怪,但你可以试试这个简短的代码:
var num1 = BigInteger.Parse("1e+9999", NumberStyles.AllowExponent);
var num2 = BigInteger.Parse("1e+9998", NumberStyles.AllowExponent);
var div = num1 / num2; // is 1, but must be 10
var eq = num1 == num2; // true, but must be false
Run Code Online (Sandbox Code Playgroud)
建议?
冒着这个问题被投票重复,甚至被关闭的风险,我提出了这个问题。
背景
在“普通”数据类型(例如 int、long long 等)中,要将二进制数值转换为十进制字符串,您可以执行以下操作(以伪代码形式):
Set length = 0
Set divisor to largest base10 value the data type will hold (Divisor).
Loop
Divide number in question by divisor.
Place result in a string at position length.
Increment the length by 1.
Divide the divisor by 10.
Reverse the string.
Print the string.
Run Code Online (Sandbox Code Playgroud)
(大多数)任何语言的实际实现都是相当简单的。
问题
我在使用上述方法时遇到的问题是,对于大整数(也称为任意精度算术),没有最大的基数 10 值可以开始。所以问题是“如果无法知道除数是多少,如何将除数初始化为最大可能的 base10 值?”
我尝试过的
仍在尝试起草解决方案。
研究
我在这里找到的一些链接包括以下内容:
将“大”十六进制数(字符串格式)转换为十进制数(字符串格式),无需 BigInteger 类
考虑以下代码:
int i = 1;
System.out.println("1 binary: " + Long.toBinaryString(i));
long ri = Long.reverse(i);
System.out.println("1 reverse bit decimal: " + ri);
System.out.println("1 reverse bit binary: "+ Long.toBinaryString(ri));
BigInteger bil = new BigInteger(1, Longs.toByteArray(ri));
System.out.println("1 Sign-Magnitude BigInteger toString: " + bil.toString());
Run Code Online (Sandbox Code Playgroud)
输出是:
1 binary: 1
1 reverse bit decimal: -9223372036854775808
1 reverse bit binary: 1000000000000000000000000000000000000000000000000000000000000000
1 Sign-Magnitude BigInteger toString: 9223372036854775808
Run Code Online (Sandbox Code Playgroud)
谁能帮助解释为什么“1 Sign-Magnitude BigInteger toString:”的值为 9223372036854775808 (2^63)?
读取文本文件、拆分数字,并将它们全部保存到BigInteger数组中的正确方法是什么?
BigInteger[] a = new BigInteger[1000];
try (Stream<String> stream = Files.lines(Paths.get(filePath))) {
} catch (IOException e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
可以直接完成还是应该先将整个文件保存为一个大文件String,然后用流拆分?
String content = new String(Files.readAllBytes(Paths.get(filePath)));
Run Code Online (Sandbox Code Playgroud)