|
-
October 2nd, 2016, 03:25 PM
#1
Rounding vs. truncation?
Hello!
Code:
#include <iostream>
using namespace std;
int main()
{
int a = 9;
int b = a/2;
cout << b << endl;
int c = 11;
int d = c/6;
cout << d << endl;
return 0;
}
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 truncated to 4, is it not? And 11/6 is truncated to 1? You don't "round down" 1.8333... do you?
Last edited by cozySam; October 2nd, 2016 at 03:29 PM.
-
October 2nd, 2016, 04:15 PM
#2
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;
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
October 2nd, 2016, 05:50 PM
#3
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?
-
October 3rd, 2016, 01:51 AM
#4
Re: Rounding vs. truncation?
 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.
-
October 3rd, 2016, 02:21 AM
#5
Re: Rounding vs. truncation?
But you got me confused by (dividend -= divisor)
Code:
(dividend -= divisor)
is the same as
Code:
(dividend = dividend - divisor)
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.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
October 3rd, 2016, 04:39 AM
#6
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.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|