[morals is a way to success]How to know a float number is equal to another

        Just now, I had a test about C programming language. But I was so sad to say that I debug an easy question for more than one hour.

        It blames to me. I haven't went to the C class to learn the class for the whole semester. So , I didn't receive the warning which the teacher told us.

        But now, I know it. And this thing encourages me to read the book I bought to learn C in a deep way.


        Now, I want to record the morals I learned and at the same time, I'll learn the algorithm.


        This is the first passage about the morals.


        I want to say that , when we meet the float number, we have to be more cautious about it. 



1、When we compare a float number to another, We cant use "==".(当我们比较两个浮点数的时候,一定不要使用“==”)

         Because of the particularity of the storage of a float, there are always a small deviation between the two float numbers.At this time, If we use "==" to judge that the left float number is equal to the right one, There will be an error. Because the "==" can only show complete equality, can't allow even a small deviation.

         (由于浮点数存储的特殊性,两个浮点数总有误差。在这时,如果我们使用“==”来判断两个浮点数相等,就会出现错误。“==”只能用来判断两个数完全相等,而不能出现一丝一毫的误差。)

        So we must find another way to solve this trouble. 

        Usually, We use "fab(a-b)<1e-6"  to judge the two float number is equal or not. It allows a small deviation between the two numbers.

        (因此,我们必须找另外的方法来解决这个问题。

                 通常,我们通过判断 a-b的绝对值是否小于0.000001 来判断两个浮点数是否相等。这样,就允许出现一些误差。)


2、Amazing float number(神奇的浮点数)

        Here is a small question: what does this sentence "printf("%f",10/3);" finally output? 

       (这有一个问题:“语句 "printf("%f",10/3);" 将输出什么?”)

        You may say : " Of course 3.333333." . If your answer is it, congratulations to you, You are completely wrong. 

        Then you may say "Perhaps it's 3.000000?". Congratulations to you, too . You are also wrong.

        What is it finally output? The answer may be amazing. "The right answer is 0.000000.".

        (你可能会说:”当然是3.333333了。“如果你的答案是这个,那么恭喜你,你错了。

            之后你可能会说:”那么是3.000000么?“恭喜你再一次错了。

             那么最终的答案是什么呢?可能有些神奇。最终的答案是”0.000000.“)

        Why do we get such an amazing answer?  Let's see.

        (我们为什么会得到这么神奇的答案呢?让我们一起来看看。)


        First, "10/3" is an int , It equals to "3". So the sentence is the same as "printf("%f",3);".

        (首先,10/3是整型运算,等于3,所以上文的语句相当于"printf("%f",3);".)

        Second, "%f" is a signal to the computer to suggest the computer to explain the number as the floating point representation. And "3"is stored in the memory as "000000000000000000000011",  If we use the floating point representation to explain it, we will get a very small number, But float can only output six number after the point. So, we get "0.000000".

         (其次,”%f“ 告诉计算机用浮点数表示法来解释后面的数,而3在内存中存储的数为000000000000000000000011 如果我们用浮点数表示法去解释它,我们将得到一个非常非常小的数,但是浮点数只能输出小数点后6位的有效数字,所以我们得到了0.000000。)


3、未完待续


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值