Discussion:
A4Q2: Rational input/output, and error handling
(too old to reply)
Jeremy Roman
2011-06-25 22:25:37 UTC
Permalink
The assignment specifies that a rational number should be printed in a
form like "3/4" or "-3/4". It is natural to assume (because the
assignment does not specify) that rational numbers should be read from
an input stream in the same format.

Unfortunately, after implementing this, I discovered that the sample
solution (object file) does *not* accept this format. Is it acceptable
to take rational numbers in the format "3/4", or is there another format
(not given in the assignment) that we should accept, such as "3 4",
which the sample solution seems to parse correctly.

Also, how many edge and error cases are we required to handle? It is
possible to come up with a number of cases which are not representable
(e.g. 1/INT_MAX * 1/2): is it acceptable to have undefined behaviour in
these cases? What about cases where the final result would be
representable, but intermediate values may not be (the assignment does
specify that at least one such case 1/INT_MAX + 1/INT_MAX, should be
handled, but there are numerous others)?
--
Jeremy Roman
Student, Computer Science
University of Waterloo
Jeremy Roman
2011-06-26 02:25:11 UTC
Permalink
Another question: regarding the statistics, the unary negation operator
is not mentioned: should statistics about this operator be gathered, and
if so, under which category should it be counted?
Post by Jeremy Roman
The assignment specifies that a rational number should be printed in a
form like "3/4" or "-3/4". It is natural to assume (because the
assignment does not specify) that rational numbers should be read from
an input stream in the same format.
Unfortunately, after implementing this, I discovered that the sample
solution (object file) does *not* accept this format. Is it acceptable
to take rational numbers in the format "3/4", or is there another format
(not given in the assignment) that we should accept, such as "3 4",
which the sample solution seems to parse correctly.
Also, how many edge and error cases are we required to handle? It is
possible to come up with a number of cases which are not representable
(e.g. 1/INT_MAX * 1/2): is it acceptable to have undefined behaviour in
these cases? What about cases where the final result would be
representable, but intermediate values may not be (the assignment does
specify that at least one such case 1/INT_MAX + 1/INT_MAX, should be
handled, but there are numerous others)?
Terry Anderson
2011-06-28 00:53:42 UTC
Permalink
No statistics should be gathered for the unary negation operator.
--
Terry Anderson
CS 246 Instructor
Another question: regarding the statistics, the unary negation operator is
not mentioned: should statistics about this operator be gathered, and if so,
under which category should it be counted?
Post by Jeremy Roman
The assignment specifies that a rational number should be printed in a
form like "3/4" or "-3/4". It is natural to assume (because the
assignment does not specify) that rational numbers should be read from
an input stream in the same format.
Unfortunately, after implementing this, I discovered that the sample
solution (object file) does *not* accept this format. Is it acceptable
to take rational numbers in the format "3/4", or is there another format
(not given in the assignment) that we should accept, such as "3 4",
which the sample solution seems to parse correctly.
Also, how many edge and error cases are we required to handle? It is
possible to come up with a number of cases which are not representable
(e.g. 1/INT_MAX * 1/2): is it acceptable to have undefined behaviour in
these cases? What about cases where the final result would be
representable, but intermediate values may not be (the assignment does
specify that at least one such case 1/INT_MAX + 1/INT_MAX, should be
handled, but there are numerous others)?
THOM BOHK
2011-06-29 20:42:21 UTC
Permalink
Further questions on I/O:

I discovered that using the sample solution .o file, inputting the
rational number 66/33 (by typing "66 33" at prompt as jeremy pointed
out above) and then outputting the new value of the rational number
with cout, i find that the ouptut is

66/33

NOT

2/1

as I would have expected, and as my own solution would generate.

I'm assuming that this is an error in the sample solution, since it is
specified that rational numbers should be stored and displayed in
reduced form, yes? Or is the purpose of the >> operator to be able to
override this and store rational numbers in whatever representation we
want?
Post by Jeremy Roman
Another question: regarding the statistics, the unary negation operator
is not mentioned: should statistics about this operator be gathered, and
if so, under which category should it be counted?
Post by Jeremy Roman
The assignment specifies that a rational number should be printed in a
form like "3/4" or "-3/4". It is natural to assume (because the
assignment does not specify) that rational numbers should be read from
an input stream in the same format.
Unfortunately, after implementing this, I discovered that the sample
solution (object file) does *not* accept this format. Is it acceptable
to take rational numbers in the format "3/4", or is there another format
(not given in the assignment) that we should accept, such as "3 4",
which the sample solution seems to parse correctly.
Also, how many edge and error cases are we required to handle? It is
possible to come up with a number of cases which are not representable
(e.g. 1/INT_MAX * 1/2): is it acceptable to have undefined behaviour in
these cases? What about cases where the final result would be
representable, but intermediate values may not be (the assignment does
specify that at least one such case 1/INT_MAX + 1/INT_MAX, should be
handled, but there are numerous others)?
Peter A. Buhr
2011-06-29 21:44:35 UTC
Permalink
Post by THOM BOHK
I discovered that using the sample solution .o file, inputting the
rational number 66/33 (by typing "66 33" at prompt as jeremy pointed
out above) and then outputting the new value of the rational number
with cout, i find that the ouptut is
66/33
NOT
2/1
I'm assuming that this is an error in the sample solution, since it is
specified that rational numbers should be stored and displayed in
reduced form, yes?
Yes, it is an error and the sample ".o" files have been updated.
achow
2011-06-29 22:33:03 UTC
Permalink
I have a question about edge cases too. Generally, how far should we
go? It seems the reference file manages ok with some edge cases and
fails spectacularly with others (sometimes without realizing it). If I
am trying to replicate the reference file's behaviour, I basically
have to try to come up with as many test cases as possible and check
how the reference behaves (as opposed to trying to come up with a
"perfect" implementation that will, hopefully, work with anything).

For example, with the following code:

Rationalnumber largeTest = Rationalnumber((INT_MAX-1),(INT_MAX));
std::cout << largeTest << std::endl;
Rationalnumber largeTest2 = Rationalnumber((INT_MAX-4),
(INT_MAX-5));
std::cout << largeTest2 << std::endl;
std::cout << (largeTest > largeTest2) << std::endl;
std::cout << (largeTest-largeTest2) << std::endl;


The output is:

2147483646/2147483647
2147483643/2147483642
1
-7/2147483642

Evidently, the implementation used correctly recognizes that largeTest
largeTest2. However, when subtracting, the implementation is wholly
unaware that it's output is incorrect.

And then there are cases in which the reference file fails gracefully.

For example:
Rationalnumber problem = Rationalnumber((INT_MAX),(INT_MIN));

produces a "Floating exception". I'm not entirely sure why, but in any
case, it fails gracefully.

Which test cases are we allowed to fail spectacularly (like the
subtraction example above), in which should be fail gracefully (like
the instantiation of the strange number above), and in which cases
should be succeed properly?

Thanks,
Alex
Another question: regarding the statistics, the unary negation operator
is not mentioned: should statistics about this operator be gathered, and
if so, under which category should it be counted?
Post by Jeremy Roman
The assignment specifies that a rational number should be printed in a
form like "3/4" or "-3/4". It is natural to assume (because the
assignment does not specify) that rational numbers should be read from
an input stream in the same format.
Unfortunately, after implementing this, I discovered that the sample
solution (object file) does *not* accept this format. Is it acceptable
to take rational numbers in the format "3/4", or is there another format
(not given in the assignment) that we should accept, such as "3 4",
which the sample solution seems to parse correctly.
Also, how many edge and error cases are we required to handle? It is
possible to come up with a number of cases which are not representable
(e.g. 1/INT_MAX * 1/2): is it acceptable to have undefined behaviour in
these cases? What about cases where the final result would be
representable, but intermediate values may not be (the assignment does
specify that at least one such case 1/INT_MAX + 1/INT_MAX, should be
handled, but there are numerous others)?
Terry Anderson
2011-06-29 23:17:44 UTC
Permalink
As I said in my previous post, you should make sure your solution handles
the special cases explicitly mentioned in the assignment (and you should
also be sure to test these).

Do not worry about other errors, and do not worry about any other cases
near INT_MIN and INT_MAX (like the ones you mentioned in your post).

Also, a reminder (for all students): *never* post code related to the
assignment, test cases, solutions, solution ideas, partial solutions,
etc. to the newsgroup. Please check with one of the instructors before
posting if you are unclear what does/does not count as an academic
offense.
--
Terry Anderson
CS 246 Instructor
Post by achow
I have a question about edge cases too. Generally, how far should we
go? It seems the reference file manages ok with some edge cases and
fails spectacularly with others (sometimes without realizing it). If I
am trying to replicate the reference file's behaviour, I basically
have to try to come up with as many test cases as possible and check
how the reference behaves (as opposed to trying to come up with a
"perfect" implementation that will, hopefully, work with anything).
Rationalnumber largeTest = Rationalnumber((INT_MAX-1),(INT_MAX));
std::cout << largeTest << std::endl;
Rationalnumber largeTest2 = Rationalnumber((INT_MAX-4),
(INT_MAX-5));
std::cout << largeTest2 << std::endl;
std::cout << (largeTest > largeTest2) << std::endl;
std::cout << (largeTest-largeTest2) << std::endl;
2147483646/2147483647
2147483643/2147483642
1
-7/2147483642
Evidently, the implementation used correctly recognizes that largeTest
largeTest2. However, when subtracting, the implementation is wholly
unaware that it's output is incorrect.
And then there are cases in which the reference file fails gracefully.
Rationalnumber problem = Rationalnumber((INT_MAX),(INT_MIN));
produces a "Floating exception". I'm not entirely sure why, but in any
case, it fails gracefully.
Which test cases are we allowed to fail spectacularly (like the
subtraction example above), in which should be fail gracefully (like
the instantiation of the strange number above), and in which cases
should be succeed properly?
Thanks,
Alex
Another question: regarding the statistics, the unary negation operator
is not mentioned: should statistics about this operator be gathered, and
if so, under which category should it be counted?
Post by Jeremy Roman
The assignment specifies that a rational number should be printed in a
form like "3/4" or "-3/4". It is natural to assume (because the
assignment does not specify) that rational numbers should be read from
an input stream in the same format.
Unfortunately, after implementing this, I discovered that the sample
solution (object file) does *not* accept this format. Is it acceptable
to take rational numbers in the format "3/4", or is there another format
(not given in the assignment) that we should accept, such as "3 4",
which the sample solution seems to parse correctly.
Also, how many edge and error cases are we required to handle? It is
possible to come up with a number of cases which are not representable
(e.g. 1/INT_MAX * 1/2): is it acceptable to have undefined behaviour in
these cases? What about cases where the final result would be
representable, but intermediate values may not be (the assignment does
specify that at least one such case 1/INT_MAX + 1/INT_MAX, should be
handled, but there are numerous others)?
Terry Anderson
2011-06-28 00:50:04 UTC
Permalink
You may assume that input is given in the form you mentioned: num denom
(the numerator, followed by whitespace, followed by the denominator).

Regarding your question about boundary and error cases: you do not
need to handle or consider any boundary/special/error cases other
than the ones explicitly mentioned in the assignment.
--
Terry Anderson
CS 246 Instructor
The assignment specifies that a rational number should be printed in a form
like "3/4" or "-3/4". It is natural to assume (because the assignment does
not specify) that rational numbers should be read from an input stream in the
same format.
Unfortunately, after implementing this, I discovered that the sample solution
(object file) does *not* accept this format. Is it acceptable to take
rational numbers in the format "3/4", or is there another format (not given
in the assignment) that we should accept, such as "3 4", which the sample
solution seems to parse correctly.
Also, how many edge and error cases are we required to handle? It is possible
to come up with a number of cases which are not representable (e.g. 1/INT_MAX
* 1/2): is it acceptable to have undefined behaviour in these cases? What
about cases where the final result would be representable, but intermediate
values may not be (the assignment does specify that at least one such case
1/INT_MAX + 1/INT_MAX, should be handled, but there are numerous others)?
--
Jeremy Roman
Student, Computer Science
University of Waterloo
Loading...