转换长多长双

时间:2021-09-03 20:09:52

I am trying to convert the first row of a long long matrix to long double, but when I print the result it only prints 0's. Here is the function:

我试图将长长矩阵的第一行转换为long double,但是当我打印结果时它只打印0。这是功能:

void convertLLtoLD(long long **B, long double **C, int n){
    int i, j;
    for (i = 0; i < n; i++) {
        for (j = 0; j < n; j++) {
            C[i][j] = 0;
        }
    }

    for (i = 0; i < n; i++) {
        C[0][i] = B[0][i]; //C[0][i] = (long double)B[0][i];
    }
    for(i = 0; i < n; i++)
        printf("%lf ", C[0][i]);
}

Here is the code where I allocate C that is done before calling the function:

这是我在调用函数之前分配C的代码:

long double** C = (long double**)malloc(n * sizeof(long double*));

for (i = 0; i < DIM; i++) {
    C[i] = (long double*)malloc(n * sizeof(long double));
}

I'm compiling the code with GCC 4.8. B is read from a file and it's values are correct since I print them before I call the function. Even when I print them inside the function the result is the same. The result is also the same when I use a cast as in the comment. Can someone explain me why it isn't working?

我正在用GCC 4.8编译代码。从文件中读取B并且它的值是正确的,因为我在调用函数之前打印它们。即使我在函数内部打印它们,结果也是一样的。当我在评论中使用强制转换时,结果也是一样的。有人可以解释我为什么不工作?

Thanks

1 个解决方案

#1


5  

In the statement :

在声明中:

printf("%lf ", C[0][i]);

You use an incorrect specifier (it expects a double).

您使用不正确的说明符(它需要一个双精度数)。

Use %Lf to print a long double, use%lld to print a long long.

使用%Lf打印long double,使用%lld打印long long。

Note:

  • In C++, just dont use printf, and prefer std::cout and the standard library
  • 在C ++中,只是不使用printf,而更喜欢std :: cout和标准库

  • printf specifiers from en.cppreference.com
  • en.cppreference.com的printf说明符

#1


5  

In the statement :

在声明中:

printf("%lf ", C[0][i]);

You use an incorrect specifier (it expects a double).

您使用不正确的说明符(它需要一个双精度数)。

Use %Lf to print a long double, use%lld to print a long long.

使用%Lf打印long double,使用%lld打印long long。

Note:

  • In C++, just dont use printf, and prefer std::cout and the standard library
  • 在C ++中,只是不使用printf,而更喜欢std :: cout和标准库

  • printf specifiers from en.cppreference.com
  • en.cppreference.com的printf说明符