-Поиск по дневнику

Поиск сообщений в rss_thedaily_wtf

 -Подписка по e-mail

 

 -Постоянные читатели

 -Статистика

Статистика LiveInternet.ru: показано количество хитов и посетителей
Создан: 06.04.2008
Записей:
Комментариев:
Написано: 0


Representative Line: Off in the Distance

Вторник, 06 Декабря 2016 г. 14:30 + в цитатник

Drew W got called in to track down a bug. Specifically, their application needed to take a customers location, and measure the distance to the nearest National Weather Service radar station. It knew the latitude and longitude of each, and needed to find the distance between those points, and it was wrong. It could be off by hundreds or even thousands of miles, especially in more remote locations.

This was the code in question:

from math import sqrt
dist = sqrt((abs(latdiff) * abs(latdiff)) + (abs(londiff) * abs(londiff)))

Now, theres an obvious problem here, and a number of nitpicks. Im going to start with the nitpicks. First, when you multiply a number by itself, itll always be positive, so you dont need the abs, making the line sqrt(latdiff*latdiff + londiff*londiff). Of course, Python also has an exponent operator, allowing you to write the easier-to-read version of, sqrt(latdiff**2 + londiff**2). But now that we bring it up, the math package in Python also includes a hypot function, which just implements the distance formula for you, meaning that whole thing could have been written thus:

from math import hypot
dist = hypot(latdiff, londiff)

Now, if your only criteria is, which solution is more pythonic?, then its clear that the latter solution is superior. Of course, you should still get your fingers whacked with a mechanical keyboard if you tried to check that solution in, because it still has one major problem: its completely and utterly wrong.

If youre not sure why… think of it as a special kind of rounding error.

[Advertisement] Scale your release pipelines, creating secure, reliable, reusable deployments with one click. Download and learn more today!

http://thedailywtf.com/articles/off-in-the-distance

Метки:  

 

Добавить комментарий:
Текст комментария: смайлики

Проверка орфографии: (найти ошибки)

Прикрепить картинку:

 Переводить URL в ссылку
 Подписаться на комментарии
 Подписать картинку