为什么此阶乘算法不准确 [英] Why Is This Factorial Algorithm Not Accurate

查看:91
本文介绍了为什么此阶乘算法不准确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,我问这个问题很愚蠢,并且准备丢掉一半的观点,但是为什么这个算法不起作用?它工作到一定程度.在数字13之后,阶乘略有减少.例如,数字在十万个位置及以后并没有完全匹配.

Sorry I feel stupid asking this and am prepared to lose half of my points asking this but why does this algorithm not work? It works up to a point. After the number 13 the factorials are a little off. For instance the numbers do not entirely match in the hundreds thousands place and onward.

#include <stdio.h>

float factorial(unsigned int i) {

    if (i <= 1) {
        return 1;
    }
    return i * factorial(i - 1);
}

int  main() {
    int i = 13;
    printf("Factorial of %d is %f\n", i, factorial(i));
    return 0;
}

这是输出:

Factorial of 13 is 6227020800.000000

以下是输出不准确的示例:

Here is an example of inaccurate output:

Factorial of 14 is 87178289152.000000

数字14的输出实际上应该是这样(来自mathisfun.com)

The output for the number 14 should actually be this (from mathisfun.com)

14     87,178,291,200

14        87,178,291,200

我将返回类型更改为float以获得更准确的输出,但是我大部分是从这里获得此代码的: https://www.tutorialspoint.com/cprogramming/c_recursion.htm

I changed the return type to float to obtain more accurate output but I obtained this code for the most part from here: https://www.tutorialspoint.com/cprogramming/c_recursion.htm

编辑:如果我将返回类型更改为两倍,则输出的精度最高为21.我正在%Lf 字符串格式化程序用于 printf 函数.

If I change to the return type to double the output is accurate up to 21.I am using the %Lf string formatter for the output in the printf function.

推荐答案

不久前有人发布了类似的问题.共识是,如果您要使用大型库(例如GMP)来编写它,而如果是编程练习,则使用字符数组来编写解决方案.

Someone posted a similar question a while back. The consensus was if you're writing it for work use a big number library (like GMP) and if it's a programming exercise write up a solution using a character array.

例如:

/* fact50.c

   calculate a table of factorials from 0! to 50! by keeping a running sum of character digits
*/

#include <stdio.h>
#include <string.h>

int main (void)
{
    printf ("\n                            Table of Factorials\n\n");

    // length of arrays = 65 character digits

    char str[] =
    "00000000000000000000000000000000000000000000000000000000000000000"; 
    char sum[] =
    "00000000000000000000000000000000000000000000000000000000000000001"; 

    const int len = strlen (str);
    int index;

    for ( int i = 0; i <= 50; ++i ) {

        memcpy (str, sum, len);

        for ( int j = 1; j <= i - 1; ++j ) {

            index = len - 1;        
            int carry = 0;

            do {
                int digit = (sum[index] - '0') + (str[index] - '0') + carry;            
                carry = 0;
                if ( digit > 9 ) {
                    carry = 1;
                    digit %= 10;
                }            
                sum[index] = digit + '0';
                --index;
            }
            while ( index >= 0 );

        }

        printf ("%2i! = ", i);
        for ( index = 0; sum[index] == '0'; ++index )
            printf ("%c", '.');
        for ( ; index < len; ++index )
            printf ("%c", sum[index]);
        printf ("\n");        

    }

    return 0;
}

这篇关于为什么此阶乘算法不准确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆