我有一个数百位长的浮点值(如 pi - 3 的前 100 位),需要一种对其进行操作的方法。有没有什么方法可以存储和操作具有大量小数的浮点数,并通过内置库保持很高的精度?c++中有没有类似python的Decimal模块的东西?
我想检查一个 float32 是否有两个小数位。我这样做的 javascript 方法如下所示:
step := 0.01
value := 9.99
if int(value/step) % 1 == 0 {
printf("has two decimal places!")
}
Run Code Online (Sandbox Code Playgroud)
上面的例子也有效。但是,当 step 不正确时它将不起作用,因为 go 然后无法正确地从 float64 转换为 int。
例子:
step := 0.1
value := 9.99
if int(value/step) % 1 == 0 {
printf("has two decimal places!")
}
Run Code Online (Sandbox Code Playgroud)
编译器错误: constant 9.99 truncated to integer
当我们使用动态值时,它只会为每种情况返回 true。
那么计算小数位数的合适方法是什么?
我有一些float变量会产生像 1.23456789 这样的值。我想把它四舍五入到小数点后 4 位。
setprecision 函数只对输出进行舍入,但我想更改变量本身的值。
所以我正在寻找类似的东西
x=roundoff(x,n)
Run Code Online (Sandbox Code Playgroud)
其中 roundoff 会将 x 舍入到 n 位小数。
我是一个新的bash学习者。如果需要,我想打印作为输入给出的表达式的结果,3 digits小数点后带有四舍五入。我可以使用以下代码,但它不会四舍五入。假设我5+50*3/20 + (19*2)/7为以下代码提供输入,则给定的输出为17.928. 实际结果是17.92857...。因此,它是truncating而不是四舍五入。我想把它四舍五入,这意味着输出应该是 17.929. 我的代码:
read a
echo "scale = 3; $a" | bc -l
Run Code Online (Sandbox Code Playgroud)
等效C++代码可以是(在main函数中):
float a = 5+50*3.0/20.0 + (19*2.0)/7.0;
cout<<setprecision(3)<<fixed<<a<<endl;
Run Code Online (Sandbox Code Playgroud) floating-point bash floating-point-precision floating-point-conversion
我正在尝试使用以下代码分解一个大整数:
library(gmp)
as.bigz(factorize( 113423713055421844361000443))
Big Integer ('bigz') object of length 38:
# [1] 2 2 2 2 2 2 2 2 2 2 2
# [12] 2 2 2 2 2 2 2 2 2 2 2
# [23] 2 2 2 2 2 2 2 2 2 2 2
# [34] 2 3 647 1134247 2998823
Run Code Online (Sandbox Code Playgroud)
这显然不是正确的分解,因为我的整数是奇数但factorize返回 2 作为因子。问题是什么?
我已经着手说明何时我需要将IEEE-754单精度和双精度数字转换成带有基数的字符串10.有FXTRACT可用的指令,但它只为基数2提供指数和尾数,因为数字计算公式为:
value = (-1)^sign * 1.(mantissa) * 2^(exponent-bias)
Run Code Online (Sandbox Code Playgroud)
如果我有特定基数的对数指令,我将能够改变2 指数的基数-表达式中的偏差部分,但目前我不知道该怎么做.我也在考虑使用标准的舍入转换为整数,但它似乎无法使用,因为它不提供精确的转换.有人知道这样做的方法/基本原则是什么?请帮忙.
我终于找到了另一个解决方案(用Java编写)
{
/* handling -infinity, +infinity and NaN, returns "" if 'f' isn't one of mentioned */
String ret = "";
if ((ret = getSpecialFloats(f)).length() != 0)
return ret;
}
int num = Float.toRawIntBits(f);
int exponent = (int)(((num >> 23) & 0xFF)-127); //8bits, bias 127
int mantissa = num & 0x7FFFFF; //23bits
/* stores decimal exponent */
int decimalExponent = …Run Code Online (Sandbox Code Playgroud) string floating-point assembly ieee-754 floating-point-precision
在Python 2.6和Python 2.7中使用Random.random()时,为什么会有不同的精度
例:
import random
import sys
rng = random.Random(0)
print sys.version
for i in range(10):
print repr(rng.random())
Run Code Online (Sandbox Code Playgroud)
2.6.6(r266:84297,2010年8月24日,18:46:32)[MSC v.1500 32 bit(Intel)]
0.84442185152504812
0.75795440294030247
0.4205715808308452.7.5(默认,2013年5月15日,
22:
43:
36 )[MSC v.1500 32 bit(Intel)] 0.8444218515250481 0.7579544029403025 0.420571580830845
为什么有不同的精度?可能是因为这种变化:http: //docs.python.org/2/tutorial/floatingpoint.html#representation-error
在Python 2.7和Python 3.1之前的版本中,Python将此值四舍五入为17位有效数字,给出"0.10000000000000001".在当前版本中,Python显示一个基于最小小数的值,该小数正确地回滚到真正的二进制值,结果只是'0.1'.
我在尝试使用浮点数时遇到了一个奇怪的错误.我正在尝试计算以下数字的log10(在"math.h"中):
-0.000000000000000000000000000000002584877722073 == -2.584877722073e-33
它一直在崩溃.这是一个有效的浮动吗?当然,我正在处理浮动本身的原始数据.
我已经研究了浮点数,根据我的理解,这不是NaN,Inf或非正规数.二进制文件似乎不匹配任何无效的东西:
1 | 00010010 | 10101101011111001000100
这种精度对数字有限制吗?可能导致问题的原因是什么?
我有一个问题 - 对大多数人来说似乎很愚蠢,但我仍然是一个新手编码器.如何将浮点变量四舍五入显示最多只有2或3位数的精度?提前致谢
在我的调试器中,我可以看到以下值:
float min = -3.1931721E38
float max = 3.3434891E38
float temp = (max-min);
Run Code Online (Sandbox Code Playgroud)
这导致无穷大.这是为什么?(2-2 ^ 23)·2 ^ 127比临时变量大吗?
java floating-point floating-accuracy floating-point-precision