[morals is a way to success]How to know a float number is equal to another
2014-01-04 18:36
471 查看
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、未完待续
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、未完待续
相关文章推荐
- know how it works is the best way to understand all
- How to arrange 10 digits so that the product of the some of them is equal to a number represented by the remaining digits
- How to arrange 10 digits so that the product of the some of them is equal to a number represented by the remaining digits
- 1TB is equal to the number of how many GB? 1PB equal to is equal to the number of TB? 1EB PB? | PC-fault.com
- What is the fastest, case insensitive, way to see if a string contains another string in C#?
- how to know wether an element is exist in a html page.
- How to first sort by one attribute, if equal, then sort by another attribute.
- How to know is the file exist in C++ - 在C++中如何判断文件是否存在
- How to check if a polygon is completely inside another polygon
- iOS开发之抛出异常 NSDecimalNumber isEqualToString
- What Is Float And How To Calculate Total & Free Float?
- What is global registry and how to know a repository is a global registry
- How to know whoever is login and running the Webapplication ?
- How to use HttpWebRequest to post data to another page which is on another server
- There is only one success --- to be able to spend your life in your own way.
- How to check port number is open for a windows server
- how to know process is alive in linux?
- How to know who is openning excel file on share folder?
- enough is enough - how to make a dmg the way you expect it
- How to know one virtual machine is in which host?