Programming in Scala Forum
Rational.gcd (Section 6.8, PDF page 144)

2 replies on 1 page. Most recent reply: Jan 23, 2017 5:35 AM by Peter Tury

 Previous Topic Next Topic
 Flat View: This topic has 2 replies on 1 page
 Peter Tury Posts: 4 Nickname: 103000 Registered: Jan, 2017
Rational.gcd (Section 6.8, PDF page 144) Posted: Jan 22, 2017 12:24 PM
Hi,

1)
I think gcd shouldn't be part of Rational, because they are completely independent mathematical ideas. In the current* way, I feel, the book encourages that we implement what we need in the classes where we need them. In the practice this would lead to a) confused classes and b) many independent implementations of the same functionalities...

*: Build date of my PDF is April 6, 2016

2)
What bothers me more, that gcd does not give the right result for some pairs containing negative numbers and for the pair (0,0). E.g.:

scala> def gcd(a: Int, b: Int): Int =
| if (b == 0) a else gcd(b, a % b)
gcd: (a: Int, b: Int)Int

scala> gcd(3,-7)
res2: Int = -1

scala> gcd(-2,-6)
res3: Int = -2

scala> gcd(0,0)
res2: Int = 0

Note: gcd must always be positive and has no meaning for (0,0).

This is not a problem in the current code as we require( d != 0) and call gcd with abs-s. However, I think gcd should not rely on its callers to get always fine parameters and should always give back good results.

Agree?

 Peter Tury Posts: 4 Nickname: 103000 Registered: Jan, 2017
Re: Rational.gcd (Section 6.8, PDF page 144) Posted: Jan 23, 2017 4:29 AM
BTW gcdLoop (Listing 7.2 on PDF page 157) and gcd (Listing 7.4) suffer from the same problem: they calculate gcd correctly for positiv numbers, while give unreliable results for others.

I think it is a bad message if the book suggests that we do not have to write correct programs, "good enough" is good enough...

 Peter Tury Posts: 4 Nickname: 103000 Registered: Jan, 2017
Re: Rational.gcd (Section 6.8, PDF page 144) Posted: Jan 23, 2017 5:35 AM
Looking more into it, I see that accoring to some definitions of gcd, gcd(0,0) is indeed 0 and both -3 and 3 can be seen as the gcd-s of (-3,-6)... However, such definitions are so uncommon that I can't find any of them on Internet... So virtually all definitions on Internet (Wikipedia, wolframalpha, matlab, various univerity papers, etc.) show, that the gcd and gcdLoop functions (all 3 in the book) are wrong.

I would suggest a clarification footnote, referring perhaps to a correct definition and/or fixing the functions to comply with the common definition.

 Flat View: This topic has 2 replies on 1 page
 Previous Topic Next Topic