php.de

Zurück   php.de > php.de Intern > Off-Topic Diskussionen

Off-Topic Diskussionen Mach mal Pause vom Programmieren!

Antwort
 
LinkBack Themen-Optionen Thema bewerten
Alt 10.01.2006, 15:07  
Erfahrener Benutzer
 
Registriert seit: 21.05.2008
Beiträge: 9.937
Zergling-new wird schon bald berühmt werden
Standard [C++] double overflow, long double

Hallo,

ich habe folgene Funktion zum Berechnen der Distanz zweier Punkte (zweidimensional):
Code:
double DistPointPoint (long x1, long y1, long x2, long y2)
{
  return sqrt(pow(labs(x1 - x2),2) + pow(labs(y1 - y2),2));
}
Sprich, Pythagoras: a² + b² = c²; c = sqrt(a² + b²)
Leider ist das c² im WorstCase 36893488130239234050, denn
Code:
sqrt( labs(-2.147.483.648 - 2.147.483.647)^2 + labs(-2.147.483.648 - 2.147.483.647)^2 );
(Pseudocode)
ergibt ja innerhalb der Wurzel 36.893.488.130.239.234.050 (36 Trillionen).
Theoretisch müsste es mit "long double" erfassbar sein (2^80), die Funktion
Code:
// DistPointPoint( -2147483647, -2147483647,  2147483647, 2147483647);
double DistPointPoint (long x1, long y1, long x2, long y2)
{  
	double dx = x1 - x2;
	double dy = y1 - y2;

	long double extension = dx*dx + dy*dy; // huge extension
	
	return (double)sqrt(extension);
}
liefert trotz Zwischenspeicherung ein falsches Ergebnis (2.8284..). Richtig müsste laut meiner Rechnung mit dem Windows Calculator aber 6074000997.1236722601565869624492 sein.

Sieht jemand nen Denk- und/oder Programmierfehler?!
Zergling-new ist offline   Mit Zitat antworten
Sponsor Mitteilung
PHP Code Flüsterer

Registriert seit: 21.08.2005
Beiträge: 4682
PHP-Kenntnisse:
Fortgeschritten

Alt 10.01.2006, 15:40  
Erfahrener Benutzer
 
Registriert seit: 21.05.2008
Beiträge: 9.937
Zergling-new wird schon bald berühmt werden
Standard

habe mit folgendem Code ein halbwegs richtiges Ergebnis erhalten. Leider nur halbwegs richtig.

Code:
double DistPointPoint (long x1, long y1, long x2, long y2)
{  
	long double dx = (long double)x1 - x2;
	long double dy = (long double)y1 - y2;

	return (double)sqrt(dx*dx + dy*dy);
}
// 6074000998.5378857	long double
// cast zu double
// 6074000997.1236725	double
Der Windows-Calculator hatte ja
6074000999,9520993849027770600526
errechnet.
Da der Return-Type aber double bleiben muss, wäre das Ergebnis 6074000997.1236725

Die Abweichung ist minimal, aber vorhanden. Trotzdem wohl akzeptabel. Wenn jemand eine korrektere Lösung weiß, nur her damit
Zergling-new ist offline   Mit Zitat antworten
Alt 10.01.2006, 18:28  
Gast
 
Beiträge: n/a
Standard

Dein zweites Ergebnis mit dem Calculator mal außen vorgelassen sieht das doch ganz akzeptabel aus... :wink:

Zitat:
Zitat von Zergling
mit dem Windows Calculator aber 6074000997.1236722601565869624492
Zitat:
Zitat von Zergling
das Ergebnis 6074000997.1236725
Kleine Frage am Rande: Wie verwendete man noch gleich printf() in Verknüpfung mit long double?

· printf("%Lf", (long double) 4911); // => -0.000000
  Mit Zitat antworten
Antwort


Themen-Optionen
Thema bewerten
Thema bewerten:

Forumregeln
Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are an
Gehe zu

Ähnliche Themen
Thema Autor Forum Antworten Letzter Beitrag
*** glibc detected *** double free or corruption (!prev): gunterrainer Server, Hosting und Workstations 3 18.05.2006 11:19
Code Syntax Highlighting imported_Lumio PHP-Fortgeschrittene 9 16.08.2005 11:06
String into Double gibt immer 0 aus? PHP Tipps 2004 1 05.09.2004 19:55

Besucher kamen über folgende Suchanfragen bei Google auf diese Seite
c double, double c, double overflow, c double overflow, long double, c overflow, long double c, overflow c, double overflow c, c long double, double Überlauf c, c long, long c, c overflow double, c double überlauf, double überlauf, new double c, c double long double, overflow double, http://www.php.de/off-topic-diskussionen/35710-c-double-overflow-long-double.html

Alle Zeitangaben in WEZ +2. Es ist jetzt 13:02 Uhr.




Powered by vBulletin® Version 3.7.2 (Deutsch)
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
Aprilia-Forum, Aquaristik-Forum, Liebeskummer-Forum, Zierfisch-Forum, Geizkragen-Forum

Creative Commons License
Dieser Inhalt ist unter einer Creative Commons-Lizenz lizenziert.