2012-03-26 6 views
0
def findDistance(): 
    first_coord = raw_input("Enter first coordinate set (format x, y): ").split(",") 
    second_coord = raw_input("Enter second coordinate set (format x, y): ").split(",") 

    x1 = float(first_coord[0]) 
    x2 = float(second_coord[0]) 
    y1 = float(first_coord[1]) 
    y2 = float(first_coord[1]) 

    print math.sqrt(float(((x2 - x1) * (x2 - x1))) + float(((y2 - y1) * (y2 - y1)))) 

Setzen in die Serie (10, 12), (12, 10) gibt mir 2.0, wenn der tatsächliche Abstand (etwas abgerundet) ist 2.82842. Es scheint, Python ist meine Nummer. Warum und wie geschieht das?Warum (und wie) rundet Python diese mathematische Gleichung?

Antwort

7

Kopieren & einfügen Fehler. Die Linie

y2 = float(first_coord[1]) 

sollte

y2 = float(second_coord[1]) 

Natürlich ist das Ergebnis auch nach IEEE doppelter Genauigkeit gerundet wird, aber der Grund, das Ergebnis ist, dass weit weg ist der obige Fehler.

+0

Das war es! Vielen Dank. * facepalm * Ich war auf der Suche nach etwas weniger Offensichtlichem. –

+1

@ElliotBonneville: Immer zuerst das Offensichtliche suchen. Ich brauche weniger Zeit. –

+0

Lektion gelernt, danke nochmal. Akzeptieren in 5 min. –

Verwandte Themen