Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Errata in Activity 3.01 #4

Open
roboli opened this issue Jan 20, 2021 · 2 comments
Open

Errata in Activity 3.01 #4

roboli opened this issue Jan 20, 2021 · 2 comments

Comments

@roboli
Copy link

roboli commented Jan 20, 2021

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)

@dsletten
Copy link

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...

@twoe-hub
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants