简单的C程序处理小数 [英] Simple C Program dealing with decimals

查看:72
本文介绍了简单的C程序处理小数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
在float与float文字比较中的奇怪输出

因此该程序只读取一堆数字,然后通过除以输入的总数来求出平均值.但是,最终结果在末尾增加了几位小数,我不确定为什么会这样做.

So the program just reads in a bunch of numbers, and finds the average of that by dividing by the total numbers entered. However, the final result adds in a few more decimals at the end and I'm not sure why it does that.

对于此给定的输入:483,10,3051,188,200,0

For this given input: 483, 10, 3051, 188, 200, 0

输出应为786.4但是INSTEAD是786.400024.我究竟做错了什么?在此先感谢伙计.

The output should be 786.4 BUT INSTEAD it is 786.400024. What am I doing wrong? Thanks in advance fellas.

int main(int argc, char** argv)
{
    int averageOfNumbers = 0;

    printf("Enter the sequence of numbers:");
    int nextNumber;
    float numberCounter = 0;
    do
    {
            scanf("%d", &nextNumber);
            if(nextNumber > 0)
            {
                    numberCounter++;
                    averageOfNumbers += nextNumber;
            }
    } 
    while(nextNumber > 0);
    float finalAverage = (float) (averageOfNumbers/numberCounter);
    averageOfNumbers = averageOfNumbers/numberCounter;
    printf("Average of the numbers in the sequence is %f\n", finalAverage);

}

推荐答案

浮点数提供了与实数的精确但不精确的近似值.(精度的高低取决于它们的大小:您使用的浮点类型中有多少精度可用.)

Floating point numbers provide a precise, but inexact approximation to real numbers. (How precise depends on their size: how many bits of precision are available in the floating point type you're using.)

IEEE 754浮点(在许多计算机上非常流行的表示形式)使用二进制.这意味着可以精确表示二进制分数,例如1/2、1/4、1/8及其组合:3/8、5/16等(在可用的精度位数范围内).不基于2的幂的分数不能精确表示.

IEEE 754 floating point (a very popular representation used on many computers) uses binary. This means that binary fractions such as 1/2, 1/4, 1/8 and combinations thereof: 3/8, 5/16, etc are represented exactly (within the limits of how many bits of precision are available). Fractions not based on powers of two are not representable exactly.

数字1/10或0.1没有确切的表示.当您在机器中输入0.1时,它将转换为具有许多接近0.1的二进制数字的值.当您使用 printf 或其他内容将其打印回来时,由于 printf 函数将其四舍五入,因此您再次获得 0.1 .精确表示了 0.1 : 0.1 进入了计算机,然后由Golly发出了 0.1 .假设要看到 0.1 与实际值之间的差异,需要10位小数位数的精度,但是您只能打印8位数字.好吧,它当然会显示为 0.1 .您的浮点打印例程消除了该错误!

The number 1/10 or 0.1 has no exact representation. When you input 0.1 into the machine, it is converted to a value with many binary digits that is close to 0.1. When you print this back with printf or what have you, you get 0.1 again because the printf function rounds it off, and so it looks like 0.1 is being exactly represented: 0.1 went into the machine, and by golly, 0.1 came out. Suppose that it takes 10 decimal digits of precision to see the difference between 0.1 and the actual value, but you are only printing to 8 digits. Well, of course it will show as 0.1. Your floating point printing routine has chopped off the error!

C printf中的%f 转换说明符将使用更多的数字表示较大的数字,因为它使用小数点后的固定位数,默认位数为6.因此,例如0.002将打印为0.002000.但是数字123456将打印为123456.000000.数量级越大,打印出的有效数字就越多.当用%f打印786.4时,实际上是在要求9个十进制数字的精度.三个是786整数部分,然后是六个.

The %f conversion specifier in the C printf will use more digits for larger numbers because it uses a fixed number of places past the decimal point, with the default being six. So for instance 0.002 will print as 0.002000. But the number 123456 will print as 123456.000000. The larger the number in terms of magnitude, the more significant figures are printed. When you print 786.4 with %f, you're actually asking for 9 decimal digits of precision. Three for the 786 integer part and then six more.

您正在使用 float ,它很可能是32位IEEE 754浮点数.这只有24位精度.(1位用于符号,7位用于二进制指数,剩下24.)24位仅等效于精度的7个十进制数字!

You're using float which is most likely a 32 bit IEEE 754 float. This has only 24 bits of precision. (1 bit is used for the sign, and 7 for the binary exponent, leaving 24.) 24 bits is equivalent to only about 7 decimal digits of precision!

因此,您要让机器将786.4(对于它来说,它具有不精确的表示形式,请记住!)从九个有效点的浮点表示形式打印到九个有效数字.您要再输入两个不存在的精度数字,因此会得到两个错误数字.

So, you're asking the machine to print 786.4 (for which it has an inexact representation, remember!) to nine significant figures, from a floating-point representation that is only good for about 7 decimal significant figures. You're asking for two more digits of precision which are not there, and therefore getting two error digits.

因此,您可以做的是使用更广泛的类型,例如 double 和/或更改打印结果的方式.不要要求那么多重要的数字.例如,尝试%.3f (小数点后三位).

So what you can do is use a wider type like double and/or change the way you're printing the result. Do not ask for so many significant figures. For instance try %.3f (three digits past decimal point).

C中的 float 类型应该很少使用.您通常希望使用 double 类型. float 有其用途,例如在大型数组中节省空间.

The float type in C should rarely be used by the way. You usually want to be working with the type double. float has its uses, such as saving space in large arrays.

这篇关于简单的C程序处理小数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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