Talk:Continued fraction/Arithmetic/G(matrix ng, continued fraction n1, continued fraction n2)

Revision as of 15:24, 12 March 2013 by rosettacode>Dkf (→‎Non-unique solutions: One of the more recently standardized areas it seems)

Idiomaticity?

Is putting that many operations on a single line really idiomatic? Yes it makes the code short, but it also makes it very hard to read. Let's face it, newlines just aren't that expensive! (If I was doing a formal code review of this, I'd be complaining about it a lot…) The watchword of Rosetta Code is “idiomatic”; the code is meant to be read by other people in order to learn how to do a task with a particular language or to compare how different languages do the same thing. (I don't just write my Tcl solutions to be short, but rather to demonstrate how to write good Tcl code that can be read long after it was written and that takes advantage of the features of the language. YMMV, but I'd expect something similar; it's not just “look we can solve this” but “we can solve this and you can understand how we did it”.)

You might also like to refactor a bit so that the code to print out a sequence is a shared method. That's the sort of thing that makes an excellent candidate for not being written out every time. –Donal Fellows 12:10, 10 March 2013 (UTC)

Non-unique solutions

The solutions in C++ and Tcl have different answers to the computation of  , but if you work the sequences out they are the same actual fraction; both are correct. I suspect this is due to differences of rounding semantics with mixed-sign integer division. –Donal Fellows 10:22, 11 March 2013 (UTC)

This depends on the definition of the canonical form for negative numbers. 151/77 is [1;1,24,1,2] so the negative form [-1;-1,-24,-1,-2] seems clearer to me. Continued_fraction/Arithmetic/Construct_from_rational_number defines how to construct continued fractions thus determine: the integer part; and remainder part, of N1 divided by N2. It then sets N1 to N2 and N2 to the determined remainder part. It then outputs the determined integer part. It does this until abs(N2) is zero. Here N1 is -151 and N2 is 77. -151/77 is -1 remainder -74. Wikis seem to define the canaonical form as [a0;ai,...,aj] where a0 is an integer and ai,...,aj are positive integers. [1] gives [-2 ; 25,1,2,1705908949761,1,1,4,2,1,4,1,23…] so your version has some support, but if we adopt it then Mathmatica will have to be rewritten and I think the form which makes the negative form similar to the positive form is better.--Nigel Galloway 13:36, 11 March 2013 (UTC)
Right, but the problem is that there are (at least) two different definitions of division / remainder for negative numbers. For example, in C99/Java, (-151) / 77 is -1 and (-151) % 77 is -74, but in Python/Ruby, (-151) / 77 is -2 and (-151) % 77 is 3. --Spoon! 20:27, 11 March 2013 (UTC)
[[2]] says the Quotient is -2 and the Remainder is -74. So let's hope the students being tutored never have to divide negative rational numbers. I'd have to pay for tutoring to get an explanation, so I won't bother (though there is a money back guarantee if I'm not satisfied, and I can't see how I would be). what-s-the-difference-remainder-vs-modulus.aspx gives a good explanation of the difference between remainer and modulus functions, (% in ruby is a modulus function). Quoting from what-s-the-difference-remainder-vs-modulus.aspx "The net result here is that integer division rounds towards zero. That should make sense; we want 123/4 to be 30 with a remainder of 3, not 31 with a remainder of -1. Similarly, we want -123/4 to be -30, not -31! It would be bizarre to say that 4 goes into 123 30 times but goes into -123 -31 times! One expects that changing the sign of a term changes the sign of the result; it does not change the magnitude of the result." I think the other result comes from a linguistic extension of Euclid's algorithm with no consideration for the "bizarre" mathematical logic resulting. So my view remains:
151/77 is 1 remainder 74 and is [1;1,24,1,2]
-151/77 is -1 remainder -74 (not -2 remainder 3) and is [-1;-1,-24,-1,-2] or -[1;1,24,1,2] (not [-2;25,1,2])
the other definition works, but is it pretty or pretty bizzare?--Nigel Galloway 13:27, 12 March 2013 (UTC)
My point is that there is no guarantee that a sequence is actually unique. The fundamental issue is that there are two ways of solving the integer division problem when extending into producing negative results: one states that the sign is an indication of the sign of the inputs, and the other states that division is about partitioning the number line and indicating which partition. (Alternatively, one definition of integer division uses round-to-zero and the other uses round-down rules when converting from a non-integer intermediate result; I've never heard of a universally-accepted definition of which of those two is right.) As long as   is true, mathematical sanity is maintained. Different languages have different rounding rules. (BTW, C prior to C99 has implementation-defined rounding rules[3]. With C++, I think C++11 is the first revision of the standard to define this clearly[4].) –Donal Fellows 15:24, 12 March 2013 (UTC)
Return to "Continued fraction/Arithmetic/G(matrix ng, continued fraction n1, continued fraction n2)" page.