小数点后的数字计数器 [英] Digits Counter after decimal points

查看:289
本文介绍了小数点后的数字计数器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hmmm在最后一个循环中,它将int(b)的值赋给d ...它总是将int(b)-1赋值给它...就像我输入10.37然后输出的值就像给出的那样以下...

http://postimg.org/image/4hetuo873/ [ ^ ]

hmmm in last loop when it assigns the value of int (b) to d...it always assigns int (b)-1 value to it...like if i enter 10.37 then the values on output are like given below...
http://postimg.org/image/4hetuo873/[^]

// Decimal Points Couter.cpp : Defines the entry point for the console application.
    //
     
    #include "stdafx.h"
    #include <iostream>
    #include <conio.h>
    using namespace std;
    int main ()
    {
            double a,b,c,d;
            int i=0;
            cout << "Enter number a:";
            cin >> a;
            b=a-int (a);
            cout << '\n';
            cout << b << endl;
            cout << '\n' << '\n';
        loop:
            b=b*10;
            cout << b << endl;
        d=int (b);
            cout << d << endl;
            c=b-d;
        cout << c << endl;
            b=c;
            i++;
            cout << '\n';
            if (b>0) goto loop;
        cout << i ;
            _getch();
            return 0;
    }

推荐答案

当你将像0.7这样的分数乘以10.0时,你会得到一个接近7.0但不完全正确的结果7.0。结果可能是例如6.999998。如果你将它分配给一个int变量,结果将是6.



这种行为的原因是双变量分数存储在二进制系统中,而不是在十进制系统中。例如,0.7没有精确表示为二进制分数。这就是为什么而不是0.7,双变量保持类似0.699999999999。
When you multiply a fraction like 0.7 by 10.0 you receive a result that is close to 7.0 but not exactly 7.0. The result might for example be 6.999998. And if you assign that to an int variable the result will be 6.

The reason for that behavior is that in double variables fractions are stored in the binary system, not in the decimal system. For example 0.7 has no exact representation as a binary fraction. And that is why instead of 0.7 the double variable holds something like 0.699999999999.


我不确定我有你,但它似乎与人们用<$ c $的经典问题有关c> double 数字。计算机存储的 double 数字不是(数学)实数数字:它们受内存约束,无法达到无限精度(例如参见\"Double-precision浮点格式[ ^ ])。在您的情况下,如果您看到(例如使用调试器)计算机内存中代表 10.37 的实际数字,那么您可能会对'奇怪' 10.369999999999999 。
I'm not sure I got you, however it seems related on the classic problem people have with double numbers. Computer-stored double numbers are not (maths) real numbers: they are memory constrained and cannot achieve inifinite precision (see for instance "Double-precision floating-point format" at Wikipedia[^]). In your case, if you see (for instance with the debugger) the actual number in computer memory representing 10.37 then you could be surprised by the 'strange' 10.369999999999999.


这就是我得到的...... thanx的支持:)

This is what i got ...thanx for your support :)
#include "stdafx.h"
   #include <iostream>
   #include <conio.h>
   using namespace std;
   int main ()
   {
           double a,b;
           int i=0;
           cout << "Enter number a:";
           cin >> a;
           cout << '\n';
           do
           {
           a=a*10;
           cout << a << endl;
           b=a-int (a);
           cout << b << endl;
           i++;
           cout << '\n';
           }while (b!=0);
           cout << i << endl;
            _getch();
           return 0;
   }


这篇关于小数点后的数字计数器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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