Re: Rounding vs. truncation?
with integer division, the result is the integer quotient. ie the maximum number of times that the positive divisor can be subtracted from the positive dividend with the resulting integer being greater than or equal to 0. For integer division the result is an integer and as such 'rounding down' or 'truncation' doesn't really apply as the quotient is an integer.
in terms of code, consider
Code:
int dividend = 19;
int divisor = 2;
int quotient = 0;
while ((dividend -= divisor) >= 0)
++quotient;
cout << "19 / 2 is " << quotient << endl;
Re: Rounding vs. truncation?
Integer division and integer quotient! That's interesting. I suppose that's a completely different way to view this.
But you got me confused by (dividend -= divisor). How should I read this, from right to left or left to right? What's happening here? You decrement dividend and store the result in divisor?
Is ++quotient the same as writing quotient++? I read that these are called post increment and pre increment and they can have different effect. But I'm guessing it doesn't matter in this example?
Re: Rounding vs. truncation?
Quote:
Originally Posted by
cozySam
I see the result of such division referred to as "rounding down". This must be wrong, is it not? I think it should be pretty clear to anyone who had basic math education that you round up numbers that end on the digit 5 or above, and you round down numbers that end on the digit 4 or below.
Why is 9/2 said to be "rounded down" to 4? Isn't this called truncation?
it's neither rounding nor truncation ( where did you read that ? ); arithmetic in programming languages is quite different from usual arithemtic you'd find in math text books. While the latter deals with set theoretic constructions, that is, entities that exist as a consequence of some set of axioms, the former deals with finite sets of bits.
The difference is so huge that the concepts, rules and terminology that works in usual arithmetic fails to work in programming languages, and require different mathematical concepts to model them.
Re: Rounding vs. truncation?
Quote:
But you got me confused by (dividend -= divisor)
Code:
(dividend -= divisor)
is the same as
Code:
(dividend = dividend - divisor)
Quote:
Is ++quotient the same as writing quotient++?
++quotient means add 1 to quotient and return the result (pre-increment). quotient++ means add 1 to quotient and return the value as it was before 1 was added (post-increment).
In the context of the code in post #2, either produces the required result - one added to quotient. However in more advanced c++ when ++ can be applied to an instance of an object then pre-increment can be faster than post-increment and for this reason IMO should be used in preference to post-increment when applicable.
Re: Rounding vs. truncation?
But note that the situation is different for non-integer division. Consider
Code:
double a = 9.0;
double b = a / 2.0;
int e = a / 2.0;
cout << b << endl;
cout << e << endl;
double c = 11.0;
double d = c / 6.0;
int f = c / 6.0;
cout << d << endl;
cout << f << endl;
In this case the output is
but now the division is floating point. But when the result of the floating point division (which is a floating point number) is assigned to an integer than the floating point number is implicitly cast to type integer and the number is truncated. So the result of c / 6.0 is always a floating pointer number but if assigned to an integer type then it is truncated. This is different to 11 / 6 which is integer division with an integer result.