小编mar*_*zzz的帖子

为什么浮点数中从大到小相加会引入更多误差?

不知道为什么,但如果我将大到小的 fp 数字相加,似乎增量误差会更大

#include <iostream>
#include <math.h>

int main() {
    std::cout.precision(50);

    const int numLoops = 1000;
    const long length = 10000;
    const double rate = 0.1;

    long totalLength = length * numLoops;
    long long steps = (long long)(totalLength / rate);

    double sum = 0.0;
    double sumRemainder = 0.0;
    for (long long step = 0; step < steps; step++) {
        if (sumRemainder >= length) {
            sumRemainder = fmod(sumRemainder, length);
        }

        sum += rate;
        sumRemainder += rate;
    }

    std::cout << …
Run Code Online (Sandbox Code Playgroud)

c++ floating-point rounding-error

-1
推荐指数
1
解决办法
1457
查看次数

如何在纯Javascript跨浏览器上转换此jQuery处理程序?

我有这个jQuery处理程序:

$('a[rel="entra_handler"]').hover(
    function() {
        $(this).children().fadeOut(200);
    },
    function() {
        $(this).children().fadeIn(200);
    }
);
Run Code Online (Sandbox Code Playgroud)

我想将它转换为纯Javascript,适用于各种浏览器.这很容易吗?

html javascript jquery

-2
推荐指数
1
解决办法
208
查看次数

如何返回Task &lt;int&gt;?

我有以下代码:

static void Main(string[] args)
{
    // start import process
    Task<int> task = StartImportProcess();
    task.Wait();
    // check result

    // process finished
    Console.ReadKey();
}

static async Task<int> StartImportProcess()
{
    int result = 0;
    result = await ImportCustomers();

    // some other async/await operations

    return result;
}

static Task<int> ImportCustomers()
{
    // some heavy operations

    Thread.Sleep(1000);

    return 1; // <<< what should I return?
}
Run Code Online (Sandbox Code Playgroud)

使用Taskasync/await。我想int作为任务的结果返回。我应该归还哪个对象?return 1;将无法正常工作。

c# task async-await

-2
推荐指数
1
解决办法
1104
查看次数

方法被称为线程!它们是否处于有序堆栈中?

我有这个代码:

for (int i = 0; i < 10000; i++)
{
    printValue(i);
}

private void printValue(int value)
{
    Console.WriteLine(value);
}
Run Code Online (Sandbox Code Playgroud)

是否有任何情况下打印的数字不符合规定?

我的意思是,方法printValue(60)的线程可能比printValue(50)之前执行?(就像例子).

或者他们是同步的?

c#

-3
推荐指数
1
解决办法
57
查看次数

如何将公里转换为米?

我有一些定义“公里”的字符串,例如:

sValore = "64.8";
sValore = "64,8";
sValore = "64";
sValore = "64.82323523";
Run Code Online (Sandbox Code Playgroud)

我需要一个将其转换为仪表的功能。

第一个问题是将其转换为“双精度”。我尝试了:

double valore = double.Parse(sValore.Replace(",", "."));
Run Code Online (Sandbox Code Playgroud)

但实际上,first(64.8)的结果是648(必须为64.8)。

然后,valore * 1000我应该解决问题,但是最后一种情况呢?结果必须是64823,而不是64823.3523

那么,我该如何解决这些麻烦呢?

.net c# math

-5
推荐指数
1
解决办法
2569
查看次数

标签 统计

c# ×3

.net ×1

async-await ×1

c++ ×1

floating-point ×1

html ×1

javascript ×1

jquery ×1

math ×1

rounding-error ×1

task ×1