Two small knowledge points related to float double in. Net/C #

Source: Internet
Author: User
Tags float double

Today, I saw this code in a C # exercise 100 example:

 

 1             float f_a = 2323.03f;
2 double d_a = 2323.03d;
3 bool b_a = (f_a == d_a);
4 Console.WriteLine(b_a);

It indicates that the output result is false. I still don't believe it. The result is true if I try it ..... A little speechless. Why? This is too easy for people to make mistakes.

 

In the example, the comment says, "The result here is false, Which is 2323.03. Because the data types are different, the storage length is different, and the approximate results are different, so they are not equal. "

The storage length cannot be used to tell the story. If there is a heap of 0 at both ends, it is considered that the data value is different? This is not quite consistent with the natural style of C #. It should be equivalent here.

Can I convert short type to growth type and then compare them with implicit operations?

Well, let's take a look at the manual forced conversion,

 

 1             float f_a = 2323.03f;
2 double d_a = 2323.03d;
3 bool b_a = ((double)f_a == d_a);
4 Console.WriteLine(b_a);

If the result is still false, I really don't understand it. If the mandatory conversion is not equal, then I can only assign this float value to a double variable. Why? Really troublesome, don't understand

 

I suddenly thought, then I forced the double to float and then looked at it:

 

 1             float f_a = 2323.03f;
2 double d_a = 2323.03d;
3 bool b_a = (f_a == (float)d_a);
4 Console.WriteLine(b_a);

It's really a crash ..... This time, the output is true, equal ........

 

Then I will try the comparison between int, float, and double:

 

Code

 1             int i_a = 23;
2 float f_a = 23f;
3 double d_a = 23d;
4 bool b_a = (i_a == f_a);
5 Console.WriteLine(b_a);
6 b_a = (i_a == d_a);
7 Console.WriteLine(b_a);

 

This is good. Two values are true ,,,,,,,

 

Either simply stick to the strong type of style, different types of incomparable, to allow comparison, it is smart, this is done...

Remember that different types of values cannot be out of order. Even if the forced type is converted, it is difficult to make it!

 

 

Postscript: Representation of floating point in memory and comparison of Floating Point Numbers

Floating point representation and basic operations

In-depth introduction to floating point numbers

 

 

Note:

I encountered this problem in the Code a few days ago. The problem of two float types Subtraction is probably as follows:

 

Float F1 = 935.5f;
Float F2 = 909.9f;

 

Then a subtraction is performed on F1 and F2, and the result is 25.6 instead of 25.59998. With the previous lessons, I thought that most of them were the reason for the internal storage format of floating point numbers.

That is to say, how does this 25.9998 come from? I have to figure it out, and then I will find a solution.

 

If this is the reason why binary data in a computer cannot be accurately expressed as a floating point type, it should also be wrong in other languages, then I went down to Dev C ++ and ran the following program:

 

Float F1 = 935.5f;
Float F2 = 909.9f;

Cout <F3 <Endl;

 

The output result is 25.6, which is correct here .... What's going on ......

Then I found out in a mess on the Internet: "There are Precision Problems in floating point computing. Each compiler has compilation options for precision adjustment. If you increase the precision, this problem should not occur.
But the speed also slows down ." I will go to vs2008 to check whether this configuration item is available. The result is not found .....

 

Some people say this: "The problem you mentioned exists in all compilers, and even in all languages. Double is like this. If you want to perform exact operations, do not use floating point numbers, split the decimal number into two integers and calculate the number. Many of the subsequent languages provided the exact decimal type for storing currencies ."

 

But how can we split the decimal number into two integers and then calculate it ??????? Don't understand .....

 

To be continued...

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.