- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有这段代码,它接受一个 digit
并返回 sqrt(2)
中该 digit
位置的数字值>:
public static int sqrtTwo(int digit)
{
//1.41421356237…
double result = Math.sqrt(2.0);
String resultString = String.valueOf(result);
if (digit == 0) {
return Integer.parseInt(resultString.substring(0, 1));
}
if (digit + 1 >= resultString.length()) {
digit = resultString.length() - 2;
}
return Integer.parseInt(resultString.substring(digit + 1, digit + 2));
}
但是我只能通过使用它来获取这么多数字。 Math 类将返回一个受限制的 double 。我想计算一个 sqrt
值,直到某个数字,无论是第 100 位还是第 500 位。我该怎么做?
我找到了这段代码,但它看起来也受到 double
的限制:
static double squareRoot(int number, int precision)
{
int start = 0, end = number;
int mid;
// variable to store the answer
double ans = 0.0;
// for computing integral part
// of square root of number
while (start <= end)
{
mid = (start + end) / 2;
if (mid * mid == number)
{
ans = mid;
break;
}
// incrementing start if integral
// part lies on right side of the mid
if (mid * mid < number) {
start = mid + 1;
ans = mid;
}
// decrementing end if integral part
// lies on the left side of the mid
else {
end = mid - 1;
}
}
// For computing the fractional part
// of square root upto given precision
double increment = 0.1;
for (int i = 0; i < precision; i++) {
while (ans * ans <= number) {
ans += increment;
}
// loop terminates when ans * ans > number
ans = ans - increment;
increment = increment / 10;
}
return ans;
}
最佳答案
大十进制构造函数中的“2”是您想要执行什么操作的值,MathContext 中的 1000 是您想要从中获取多少位数字。
这样我就可以将 sqrt(2) 精确到小数点后 1000 位
public static void main(String[] args) {
BigDecimal digits = new BigDecimal("2");
BigDecimal num = digits.sqrt(new MathContext(1000));
System.out.println(num.toString());
}
关于java - 获取 sqrt 的第 n 位数字 : getting digits that exceed the double scope,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61394846/
我有一个 Node.JS 自动化,它使用 Puppeteer 并在过程中加载一些 URL。我的代码非常基本,仅使用包文档中记录的非常基本的函数。 自动化计划每 15 分钟使用 crontab 运行一次
我尝试阅读 stackoveflow 上回答的一些问题并根据此更改 eclipse.ini: 现在,除了一个应用程序之外,每个应用程序都可以正常运行。它显示此消息: 无法执行 dex:超出 GC 开销
问题描述: Task A. Amount of subtractions You have an array a length n. There are m queries (li,ri), for
编辑:看起来问题是过度使用#includes 创建圈子。我确保只包括那些需要的,它解决了前两个错误。 但是,我仍然为 BUtton 和 Elevator 得到“指定的多个默认构造函数” 每个错误都有两
在CloudKit中,我尝试通过批处理来保存大量的记录。但是,我的应用程序因以下错误而崩溃: Error pushing local data: 这是我的代码: CKModifyRecordsOpe
我正在尝试使用以下代码将 BigQuery 数据集从 Google Cloud Platform 下载到 R 工作区以对其进行分析: library(bigrquery) library(DBI) l
在 Kubernetes 中 Kubernetes Health Check Probes ,如果 timeoutSeconds 超过 periodSeconds 会怎样?例如: initialDel
我们正在使用 youtube 数据 api v3,并且已经有一段时间没有任何问题了。最近,我们收到了这个 403 异常: The request cannot be completed because
我正在将一个项目从gradle版本3.3转换为4.10.1。该项目主要是使用自定义构建步骤构建的C++代码,而不是CMake(externalNativeBuild)或Android.mk(ndkBu
这是我为查找小于或等于给定编号的跳跃数而编写的代码。它显示错误“超出输出限制” int main() { int t; cin>>t; while(t--) { long long int n
我正在尝试使用 Google Translate REST API 并同时请求以下网址: http://ajax.googleapis.com/ajax/services/language/trans
大多数时候,作为 .Net 开发人员,我们可以自由地在高级抽象世界中玩耍,但有时现实会踢你的私密部分,并告诉你要找到一个真的理解。 我刚刚经历过其中一次。我认为将角落数据列为项目列表就足够了,以便您了
我编写了一个更新函数,但是多次执行将产生错误context deadline exceeded。 我的功能: func Update(link string, m bson.M) { conf
我在我的网络服务器上同时使用 mysql 和 asp.net。我还使用 sqlite 数据库以便能够在另一台设备上使用该数据库。我需要在两个数据库之间发送数据。这是一天需要做很多次的事情。这是我如何做
我在我的应用程序中使用 Google TextToSpeech 已经很长时间了,我的许多用户都在使用离线语音,所以我对使用的资源数量没有任何问题。但是在收到 GoogleTTS 的最新更新后,我所有的
我正在尝试从 MySQL 5.0.45 数据库中删除几行: delete from bundle_inclusions; 客户端工作了一段时间,然后返回错误: Lock wait timeout ex
我试图将一个 ~200G 的文件加载到具有 4 个数据节点的 MySQL 集群中,我的目标表的 DDL 是这样的: CREATE TABLE XXXXXX ( ID BIGINT AUTO
我有这个脚本: def number_of_occurences(c, message): position = message.find(c) if position == -1:
我正在尝试对我的应用程序进行单元测试,但大部分测试都失败了,原因是异步等待失败:超过 30 秒的超时时间,未满足预期:“Home Code”。 我不知道为什么会这样失败,但这是我下面的代码 class
我的 HTML 表单是这样的 但是,当我上传一个 3mb 的文件时,它给出错误: Problem: File exceeded max_file_size" 最佳答案 我最后检查过,MAX_FIL
我是一名优秀的程序员,十分优秀!