[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

a problem about "sqrt"

linlinfan320

11/1/2008 1:23:00 AM


why it print wrong result? I can't find the wrong place.


#include<stdio.h>
#include<math.h>

double distance(double a ,double b,double c,double d);

int main()
{
double x1,y1,x2,y2;
double result;

printf("Enter four numbers:");
scanf("%f%f%f%f",&x1,&y1,&x2,&y2);

result=distance(x1,y1,x2,y2);

printf("The distancd is %.1f",result);

return 0;
}
double distance(double a,double b,double c,double d)
{
return sqrt((a-c)*(a-c)+(b-d)*(b-d));

}
18 Answers

Kai-Uwe Bux

11/1/2008 1:55:00 AM

0

linlinfan320@163.com wrote:

>
> why it print wrong result? I can't find the wrong place.
>
>
> #include<stdio.h>
> #include<math.h>
>
> double distance(double a ,double b,double c,double d);
>
> int main()
> {
> double x1,y1,x2,y2;
> double result;
>
> printf("Enter four numbers:");
> scanf("%f%f%f%f",&x1,&y1,&x2,&y2);

Print the values of x1, x2, y1, and y2 here.

Are they, what you expect?

> result=distance(x1,y1,x2,y2);
>
> printf("The distancd is %.1f",result);
>
> return 0;
> }
> double distance(double a,double b,double c,double d)
> {

Or, print the values of a, b, c, and d here. E.g.:

std::cout << a << " " << b << " " << c << " " << d << "\n";

> return sqrt((a-c)*(a-c)+(b-d)*(b-d));
>
> }


Best

Kai-Uwe Bux

osmium

11/1/2008 2:12:00 AM

0

<linlinfan320@163.com> wrote:

> why it print wrong result? I can't find the wrong place.
>
>
> #include<stdio.h>
> #include<math.h>
>
> double distance(double a ,double b,double c,double d);
>
> int main()
> {
> double x1,y1,x2,y2;
> double result;
>
> printf("Enter four numbers:");
> scanf("%f%f%f%f",&x1,&y1,&x2,&y2);

The penalties for lying to the I/O routines are often severe.

>
> result=distance(x1,y1,x2,y2);
>
> printf("The distancd is %.1f",result);
>
> return 0;
> }
> double distance(double a,double b,double c,double d)
> {
> return sqrt((a-c)*(a-c)+(b-d)*(b-d));
>
> }


James Kanze

11/1/2008 9:07:00 AM

0

On Nov 1, 2:23 am, linlinfan...@163.com wrote:
> why it print wrong result? I can't find the wrong place.

> #include<stdio.h>
> #include<math.h>
>
> double distance(double a ,double b,double c,double d);
>
> int main()
> {
>    double x1,y1,x2,y2;
>    double result;
>
>    printf("Enter four numbers:");
>    scanf("%f%f%f%f",&x1,&y1,&x2,&y2);
>
>    result=distance(x1,y1,x2,y2);
>
>    printf("The distancd is %.1f",result);
>
>    return 0;
> }

> double distance(double a,double b,double c,double d)
> {
>    return sqrt((a-c)*(a-c)+(b-d)*(b-d));
> }

The reason why it prints the wrong result is that you're using
scanf. Try doing it the C++ way:

#include <iostream>
#include <cstddef>
#include <cmath>

extern double distance( double a, double b, double c, double d ) ;

int
main()
{
std::cout << "Please enter four numbers: " ;
double x1 ;
double y1 ;
double x2 ;
double y2 ;
if ( std::cin >> x1 >> y1 >> x2 >> y2 ) {
std::cout << "The distance is: "
<< distance( x1, y1, x2, y2 )
<< std::endl ;
} else {
std::cerr << "Those weren't four legal numbers"
<< std::endl ;
}
return EXIT_SUCCESS ;
}

double
distance(
double x1,
double y1,
double x2,
double y2 )
{
return std::sqrt( ((x1-x2) * (x1-x2)) + ((y1-y2)*(y1-y2)) ) ;
}

You should just forget about stdio.h (except for a few things
like remove or rename); the interface is horribly broken, and
extremely error prone and unnatural. (The iostream interface
isn't without problems either, but it's still an order of
magnitude better than stdio.h.)

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Juha Nieminen

11/1/2008 9:54:00 AM

0

James Kanze wrote:
> double x1 ;
> double y1 ;
> double x2 ;
> double y2 ;

A question of style, but is it *really* necessary to be so verbose?
I honestly don't think this is any less clear (if anything, it's the
contrary):

double x1, y1, x2, y2;

fungus

11/1/2008 11:06:00 AM

0

On Nov 1, 2:23 am, linlinfan...@163.com wrote:
> why it print wrong result? I can't find the wrong place.
>
>    double x1,y1,x2,y2;
>
>    printf("Enter four numbers:");
>    scanf("%f%f%f%f",&x1,&y1,&x2,&y2);
>

Using %f in scanf doesn't read a double, it reads a float.

Best not to use scanf, it's a dangerous function (as you're
finding out...)

Bo Persson

11/1/2008 11:39:00 AM

0

Juha Nieminen wrote:
> James Kanze wrote:
>> double x1 ;
>> double y1 ;
>> double x2 ;
>> double y2 ;
>
> A question of style, but is it *really* necessary to be so verbose?
> I honestly don't think this is any less clear (if anything, it's the
> contrary):
>
> double x1, y1, x2, y2;

Your style is only usable when declaring several uninitialized
variables of the same type. That makes it very unusual.

In all other cases, James' version is much better. Using it even for
this corner case, is at least more consistent.


Bo Persson


Juha Nieminen

11/1/2008 9:21:00 PM

0

Bo Persson wrote:
>> double x1, y1, x2, y2;
>
> Your style is only usable when declaring several uninitialized
> variables of the same type. That makes it very unusual.

So you call this "unusable"?

double x1 = 0, y1 = 0, x2 = 0, y2 = 0;

Ian Collins

11/1/2008 10:00:00 PM

0

Juha Nieminen wrote:
> Bo Persson wrote:
>>> double x1, y1, x2, y2;
>> Your style is only usable when declaring several uninitialized
>> variables of the same type. That makes it very unusual.
>
> So you call this "unusable"?
>
> double x1 = 0, y1 = 0, x2 = 0, y2 = 0;

"unusual"

--
Ian Collins

Bo Persson

11/1/2008 10:03:00 PM

0

Juha Nieminen wrote:
> Bo Persson wrote:
>>> double x1, y1, x2, y2;
>>
>> Your style is only usable when declaring several uninitialized
>> variables of the same type. That makes it very unusual.
>
> So you call this "unusable"?
>
> double x1 = 0, y1 = 0, x2 = 0, y2 = 0;

I said unusual, but you are close. Try this one, and you are already
in trouble:

double* x1 = 0, y1 = 0, x2 = 0, y2 = 0;


Bo Persson


Pawel Dziepak

11/1/2008 10:12:00 PM

0

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Bo Persson wrote:
> I said unusual, but you are close. Try this one, and you are already
> in trouble:
>
> double* x1 = 0, y1 = 0, x2 = 0, y2 = 0;

That's why I'd use:
double *x1, *y1, ...;
However, this leads us to another pointless discussion which coding
style is the best and why each other is so horribly ugly. I think that
each style is good as long as you can easily read and understand the
code. There are better things to discuss, aren't there?

Pawel Dziepak
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Fedora - http://enigmail....

iEYEARECAAYFAkkM1DkACgkQPFW+cUiIHNo1uQCfXpFefGkMSReJUkeiecJdFuCZ
RcIAn2QE9Fgf+lb4IzvLkkKojFHqTx50
=CXW2
-----END PGP SIGNATURE-----