Integer vs floating division & # 8594; Who is responsible for providing the result?

I programmed in C ++ for some time, but suddenly a doubt arose and wanted to clarify the Stackoverflow community.

When an integer is divided by another integer, we all know that the result is an integer and as wise, a float divided by a float is also a float.

But who is responsible for providing this result? Is this a compiler or a div statement?

+7
c ++ c integer division integer-division
source share
6 answers

At the time of compilation, it will be decided which form of division is required based on the types of variables used - at the end of the day, a DIV (or FDIV) instruction of one form or another will be involved.

+3
source share

It depends on whether your architecture has a DIV instruction. If your architecture has both integer and floating point separator instructions, the compiler will issue the correct instruction for the case specified in the code. The language standard defines the rules for type advancement and whether to use an integer or floating point in every possible situation.

If you have only an integer division command or only a floating point division command, the compiler will embed some code or generate a call to the math support library to handle division. Division commands are known to be slow, so most compilers will try to optimize them, if at all possible (for example, replace with shift instructions or pre-calculate the result to separate compile time constants).

+11
source share

Equipment separation instructions almost never include conversion between integer and floating point. If you get separation instructions at all (they are sometimes not taken into account, because the separation scheme is large and complicated), they will almost certainly “divide int by int, produce int” and “divide float by float, produce float”, and it usually happens that both entry and exit are also the same size.

The compiler is responsible for creating any operation written in the source code on top of these primitives. For example, in C, if you split the float by int, the compiler will emit the int-to-float transform and then split the float.

(I don’t know, but I don’t know, but I wouldn’t miss VAX to have instructions “divide float by int.” Itanium really didn’t have a separation command, but its “Separate helper” was only for floating point, you should have fake integer division to vertex floating point!)

+3
source share

One of the most important rules in the C ++ standard is the “as if” rule:

The semantic descriptions in this International Standard define a parametrized non-deterministic abstract machine. There are no requirements for the structure of the corresponding implementations in this International Standard. In particular, they do not need to copy or emulate the structure of an abstract machine. Rather, appropriate implementations are needed to emulate (only) the observed behavior of an abstract machine, as described below.

Which, in relation to your question, means that it doesn't matter which component performs the separation while this is being done. It can be executed by machine DIV code, it can be executed by more complex code if there is no corresponding instruction for the processor in question.

He can also:

  • Replace the operation with a bit change operation if necessary and is likely to be faster.
  • Replace the operation with a literal if it is evaluated during compilation or assignment, if, for example, during processing x / y during compilation it can be shown that y will always be 1.
  • Replace the operation with an exception throw, if during compilation it can be shown that it will always be a whole division by zero.
+1
source share

Your question does not make sense. The DIV instruction does nothing by itself. No matter how loud you shout at him, even if you try to bribe him, he is not responsible for anything

When you program in the programming language [X], the compiler [X] is responsible for creating a program that executes what you described in the source code.

If division is requested, the compiler decides how to do the division. This can happen by creating an opcode for a DIV instruction if your CPU has targeting. This can be by pre-subtracting the division at compile time and simply pasting the result directly into the program (assuming both operands are known at compile time), or it can be done by creating a sequence of instructions that together emulate the division.

But it is always up to the compiler. Your C ++ program has no effect if it is not interpreted in accordance with the C ++ standard. If you interpret it as a regular text file, it does nothing. If your compiler interprets it as a Java program, it will choke and reject it.

And the DIV instruction knows nothing about the C ++ standard. On the other hand, the C ++ compiler is written for the sole purpose of understanding the C ++ standard and transforming the code according to it.

The responsible compiler is always .

+1
source share

Almost

The C99 standard defines "When integers are divisible, the result of the / operator is an algebraic factor with any fractional part discarded." And he adds to the footnote that "this is often called" truncation to zero. "

Story

Historically, the language specification is responsible.

Pascal defines its operators , so using / to divide always returns real (even if you use it to separate 2 integers), and if you want to separate integers and get an integer result, you use the div operator instead. (Visual Basic has a similar distinction and uses the \ operator for integer division, which returns an integer result.)

In C, it was decided that the same difference should be made by converting one of the integer operands to float if you want to get a floating point result. It becomes an agreement to consider types with integers and floating-point types as you describe in many C-derivative languages. I suspect this agreement could have arisen in Fortran.

0
source share

All Articles