标签: floating-point-precision

可以以某种方式改变变量吗?

所以我有一个看起来像这样的函数:

float function(){
    float x = SomeValue;
    return x / SomeOtherValue;
}
Run Code Online (Sandbox Code Playgroud)

在某些时候,此函数溢出并返回一个非常大的负值.为了尝试准确地追踪这发生的位置,我添加了一个cout语句,以便函数看起来像这样:

float function(){
    float x = SomeValue;
    cout << x;
    return x / SomeOtherValue;
}
Run Code Online (Sandbox Code Playgroud)

它工作了!当然,我通过使用双重完全解决了这个问题.但我很好奇为什么这个功能在我做的时候能正常工作.这是典型的,还是我错过了其他地方的错误?

(如果有任何帮助,浮点数中存储的值只是一个整数值,而不是一个特别大的值.我只是把它放在一个浮点数中以避免转换.)

c++ floating-point cout cpu-registers floating-point-precision

9
推荐指数
1
解决办法
2192
查看次数

这是一个有效的浮动比较,它占了一定数量的小数位吗?

我正在编写一个扩展方法,使用一组小数点(有效数字)来比较两个浮点数,以确定它们是否相等而不是容差或百分比差异.通过关于浮点数比较的其他问题,我看到了复杂的实现.我是否过度简化或是否有效?

/// <summary>
/// Determines if the float value is equal to (==) the float parameter according to the defined precision.
/// </summary>
/// <param name="float1">The float1.</param>
/// <param name="float2">The float2.</param>
/// <param name="precision">The precision.  The number of digits after the decimal that will be considered when comparing.</param>
/// <returns></returns>
public static bool AlmostEquals(this float float1, float float2, int precision = 2)
{
    return (Math.Round(float1 - float2, precision) == 0);
}
Run Code Online (Sandbox Code Playgroud)

注意:我正在寻找小数位的比较,而不是容差.我不希望1,000,000等于1,000,001.

c# floating-point floating-point-precision

9
推荐指数
1
解决办法
1385
查看次数

为什么浮动变量通过以奇怪的方式在点之后切割数字来节省值?

我有这个简单的代码行:

float val = 123456.123456;
Run Code Online (Sandbox Code Playgroud)

当我打印此val或查看范围时,它存储值123456.13

好吧,没关系,它不能在4个字节之后存储所有那些数字,但为什么它在点之后产生13?不应该是12吗?

(在win32上使用vc ++ 2010 express)

c++ floating-point digits ieee-754 floating-point-precision

9
推荐指数
3
解决办法
910
查看次数

选择具有给定精度的FLOAT

我想将我的值存储为FLOATS,但是在检索它们时,用户可以指定(并在运行时更改)他希望在小数点后看到多少位数.

我可以通过SELECT某种方式传递它,还是我必须在我的应用程序中执行此操作?

mysql floating-point-precision

8
推荐指数
2
解决办法
9339
查看次数

C++如何避免浮点算术错误

我正在编写一个使用float递增的循环,但是我遇到了以下示例中说明的浮点算术问题:

for(float value = -2.0; value <= 2.0; value += 0.2)
    std::cout << value << std::endl;
Run Code Online (Sandbox Code Playgroud)

这是输出:

-2
-1.8
-1.6
-1.4
-1.2
-1
-0.8
-0.6
-0.4
-0.2
1.46031e-07
0.2
0.4
0.6
0.8
1
1.2
1.4
1.6
1.8
Run Code Online (Sandbox Code Playgroud)

我到底为什么要1.46031e-07而不是0?我知道这与浮点错误有关,但是我无法理解它为什么会发生以及我应该做些什么来防止这种情况发生(如果有办法).有人可以解释(或指向我的链接),这将有助于我理解?任何输入都表示赞赏.谢谢!

c++ floating-point floating-point-precision

8
推荐指数
5
解决办法
2万
查看次数

Java - 一次双加/减的最大精度损失

是否有可能建立甚至粗略地说明在处理doublejava中的两个值(加/减)时最大精度损失是多少?可能最坏的情况是两个数字无法准确表示,然后对它们执行操作,这导致一个也无法准确表示的值.

java double double-precision floating-point-precision

7
推荐指数
2
解决办法
2771
查看次数

生成大量唯一的随机float32数字

我需要生成一个二进制文件,其中只包含唯一的随机数,具有单精度.然后,目的是计算该文件的熵并将其与其他数据集熵一起使用以计算比率entropy_file/entropy_randUnique.该值被命名为"随机性".

我可以在python中使用双精度数字并插入它们set(),使用struct.pack如下:

    numbers = set()
    while len(numbers) < size:
        numbers.add(struct.pack(precision,random.random()))
    for num in numbers:
        file.write(num)
Run Code Online (Sandbox Code Playgroud)

但是当我改为单精度时,我不能只改变包方法(这会产生很多相同的数字而while会永远不会结束),而且我无法生成单精度数random.我已经研究过,numpy但发电机的工作原理与我理解的相同.如何在二进制文件中获得370914252(这是我最大的测试用例)唯一的float32,即使它们不是随机的,我认为一个洗牌序列就足够了......

python floating-point numpy floating-point-precision floating-point-conversion

7
推荐指数
1
解决办法
286
查看次数

C++ sqrt函数精度为正方形

设,x是一个整数和y = x * x.

那保证sqrt(y) == x吗?

例如,我可以确定sqrt(25)sqrt(25.0)将返回5.0,不是5.00000000034.999999998

c++ floating-point-precision

7
推荐指数
2
解决办法
2521
查看次数

数字的力量接近1

我猜有一些我无法找到的标准技巧:无论如何,我想以数字稳定的方式计算一个非常接近1的数字的大功率(认为1-p,其中p <1e-17) .在我的系统上,1-p被截断为1.

使用对数的泰勒展开式,我得到以下边界

式

我能做些什么更聪明的事吗?

floating-point r numerical-stability floating-point-precision

7
推荐指数
1
解决办法
168
查看次数

使用{0:g}删除尾随零时,.format()返回ValueError

我正在尝试生成一个字符串,其中包含偶尔带有尾随零的浮点数.这是文本字符串的MWE,我试图用以下方法删除它们{0:g}:

xn, cod = 'r', 'abc'
ccl = [546.3500, 6785.35416]
ect = [12.350, 13.643241]

text = '${}_{{t}} = {0:g} \pm {0:g}\;{}$'.format(xn, ccl[0], ect[0], cod)
print text
Run Code Online (Sandbox Code Playgroud)

不幸的是,这回归

ValueError: cannot switch from automatic field numbering to manual field specification
Run Code Online (Sandbox Code Playgroud)

这个问题使用.format()来格式化具有相同问题报告的字段宽度参数的列表,但我无法弄清楚如何将答案应用于此问题.

python string formatting floating-point-precision

7
推荐指数
1
解决办法
5789
查看次数