# Since float cannot represent all int_, why is C + + converting int to float when type conversion?

Source: Internet
Author: User
Tags arithmetic

Abstract: Since float cannot represent all int, why is C + + converting int to float in type conversion? Question: The code is as follows: int i = 23; float f = 3.14; if (i = = f)//execute a piece of code the compiler will convert I to float type and then compare the size of these two floats, but can float represent all int? Why isn't the int and float converted to a double type for comparison? Answer: In the evolution of integers, when int becomes unsigned, it loses the negative part (interestingly enough, 0u < 1 is the right one).

Since float cannot represent all int, why is C + + converting int to float when type conversion? Problem:

The code is as follows:

1. `int i = 23;`
2. `float f = 3.14;`
3. `if (i == f) // 执行某段代码`

The compiler converts I into a float type and then compares the size of the two float, but can float represent all int? Why isn't the int and float converted to a double type for comparison?

In the evolution of the integer number, when it `int` becomes `unsigned` , it loses the negative part (interestingly, that is, it `0u < -1` is right).

As with most of the mechanisms in C (inherited in C + +), common arithmetic conversions should be straightforward in terms of hardware operations. The inventors of the C language are proficient in the assembly language of the machines they use, and the C language they write has a direct meaning to them and to those who write programs like them, until they are written in assembly language (such as the Unix kernel).

Today, in general, processors do not have mixed-type instruction systems (such as float and double additions, Comparison of int and float, and so on), because if this makes a huge waste of chip wafers-if you want to support more different types, you have to implement more opcode. In practice, however, you only have to implement common directives for functions such as "add int to int", "compare float to float" and "multiply unsigned with unsigned", which makes it necessary to prioritize arithmetic conversions- They are two types of mapping relationships in the instruction system, most of which are useful.

From the point of view of programmers who are accustomed to writing low-level machine code, if there are mixed types, the most likely use of assembly instructions in general is those that require minimal type conversions. One particular case is the conversion of floating-point numbers, especially in the early the 1970s, when the C language was being developed, the computer was running slowly, and the calculation of floating-point numbers was done by software, so the cost of converting was high. This slows down the transformation of the usual arithmetic operations--only one operand is converted (The exception is the conversion of long to unsigned int, which is not required and can be done on most machines.) Of course not all, because there are always exceptions).

So, the usual arithmetic conversions are written to accomplish what the programmer needs to do most of the time: there are two types of mismatches that convert one to another. This is what the assembler code does, unless there are special reasons for other types of conversions. For those who are accustomed to compiling code, they are forced to write a different type of conversion unless it is a special need. Obviously, in this case it is natural to propose to write a conversion. Although, you can simply write this

1. `if((double) i < (double) f)`

By the way, the interesting thing about this problem is that `unsigned` the precedence is higher than that, so when comparing it with the comparison `int` , the `int` unsigned type is the `unsigned` final comparison ( `0u < -1` This is what is mentioned at the beginning). I suspect that this may have been earlier (in the early days of the computer development) when people thought it was `unsigned` `int` less restrictive than the indicated range of values: No sign bit needed now, so additional bits could be used to represent a larger range of values. If you think it `int` might overflow, then use unsigned-the worry will be even more pronounced when using the 16-bit ints.

Since float cannot represent all int_, why is C + + converting int to float when type conversion?

Related Keywords:

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.