【问题标题】:Is there a way to calculate tgammaf(50) on a device using cuda?有没有办法在使用 cuda 的设备上计算 tgammaf(50)?
【发布时间】:2020-09-06 12:50:46
【问题描述】:

我正在使用斯特林的近似值来计算这个,但是我无法存储从斯特林的近似值中得到的巨大答案。有没有什么好的方法可以在设备上存储这么大的数字?

【问题讨论】:

  • 使用doubletgamma?
  • 有人愿意为这个问题添加答案吗?

标签: c++ c++11 cuda wolfram-mathematica random-seed


【解决方案1】:

CUDA standard math library 同时支持双精度函数tgamma 和单精度函数tgammaf。由于 Γ(50) 的数量级为 1062,因此结果溢出了 float 类型的可表示范围,tgammaf (50.0f) 因此返回无穷大。但是,通过使用双精度函数tgamma 并将结果存储到double 变量中,计算可以继续进行而不会溢出,例如

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

__global__ void kernel (int a)
{
    float r = tgammaf ((float)a);
    double rr = tgamma ((double)a);
    printf ("tgammaf(%d) = %23.16e\ntgamma(%d)  = %23.16e\n", a, r, a, rr);
}

int main (void)
{
    kernel<<<1,1>>>(50);
    cudaDeviceSynchronize();
    return EXIT_SUCCESS;
}

上面程序的结果应该是这样的:

tgammaf(50) =                     inf
tgamma(50)  =  6.0828186403426752e+62

【讨论】:

    猜你喜欢
    • 2013-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-24
    • 2021-09-10
    相关资源
    最近更新 更多