The-Clojure-Workshop icon indicating copy to clipboard operation
The-Clojure-Workshop copied to clipboard

Errata in Activity 3.01

Open roboli opened this issue 4 years ago • 2 comments

I got different results because your code is different from that from the book:

(def walking-speed 4)

Book says:

... and an average of walking speed of 5 km per hour.

(defn distance
	"Returns a rough estimate of the distance between two coordinate points, in kilometers. Works better with smaller distance"
	[{lat1 :lat lon1 :lon} {lat2 :lat lon2 :lon}]
	(let [deglen 110.25
				x (- lat2 lat1)
				y (* (Math/cos lat2) (- lon2 lon1))]
				(* deglen (Math/sqrt (+ (* y y) (* x x))))))

Book has:

110.25 * sqrt((lat2 - lat1)^2 + cos(lat1) * (lon2 - lon1)^2)

Please notice that has cos(lat1) not cos(lat2)

roboli avatar Jan 20 '21 12:01 roboli

Roberto, you only identified the smallest problem with this terrible code. Yes, they switched from cos(lat1) to cos(lat2). But they shouldn't be computing cos() of either. Java's Math.cos() method takes radian arguments, not degrees of latitude. You have to first call Math.toRadians(): Math.cos(Math.toRadians(lat1)) Furthermore, they are squaring the cos() factor in (* y y) above, so the result is (cos(lat2)*(lon2-lon1))^2 not what the formula in the book shows at all...

dsletten avatar Feb 13 '21 09:02 dsletten

Bitten by it. However, thanks to @dsletten for, Math.cos(Math.toRadians(lat1)); never occurred to me.

twoe-hub avatar Sep 17 '21 12:09 twoe-hub