将双精度分割为C中的数组

Aar*_*ken 0 c

我想要以下代码:

double num = 31415; /* num will never have a decimal part */
/* ... code here so that we can now say */
printf("%d", num_array[0] == 3 && num_array[1] == 1 && num_array[4] == 5); //1
Run Code Online (Sandbox Code Playgroud)

我意识到使用整数执行此操作是微不足道的(只是int i=0; while(num>0) numarr[size-++i] = num%10, num/=10;大小预先确定为数字中的位数),但这显然不适用于浮点数/双精度数,因为您无法修改一个浮点数.

是的,我需要使用浮点数/双打,尽管没有使用浮点部分(这是一个练习).

我已经弄清楚如何使用floor()来确定double中的位数.

/* n > 0, n=floor(n) */
int numdigits(double n){
    int i = 0;
    while (n >0)
        n = floor(n/10), i++;
    return i;
}
Run Code Online (Sandbox Code Playgroud)

Jer*_*fin 6

查一查fmod.使用的位数可能更容易计算log10.