a problem about "sqrt"

L

linlinfan320

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));

}
 
K

Kai-Uwe Bux

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
 
O

osmium

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

James Kanze

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

Juha Nieminen

James said:
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;
 
F

fungus

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

Bo Persson

Juha said:
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
 
J

Juha Nieminen

Bo said:
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;
 
B

Bo Persson

Juha said:
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
 
P

Pawel Dziepak

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

Bo said:
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.mozdev.org

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

Jim Langston

Pawel Dziepak said:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1



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?

But, if you do:

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

It doesn't matter where you put the * does it? Which is the whole point.
Something getting "broken" by where you happen to put a space is not very
good IMO.
 
P

Pawel Dziepak

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

Jim said:
It doesn't matter where you put the * does it? Which is the whole
point. Something getting "broken" by where you happen to put a space is
not very good IMO.

I see your point, but I am used to writing such kind of things mostly in
one line. However, I'd write this in the following style:

double x1, y1;
double x2, y2;

A few variables in one line, but somehow connected which each other.
That's how *I* write my code. I don't know if it is better or worse
style, because I don't think that it really matters. All three styles
are quite easy to read and understand (unless you don't know C/C++).

I really think that this discussion is pointless. You prefer your style
- - use it, but I'll use mine.

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

iEYEARECAAYFAkkM5fAACgkQPFW+cUiIHNo+4ACgsBoGN87QXXgLd9uv8YP44B8m
wwIAoLD48tMNxBRSiafulhClqk+ScLYI
=tCL7
-----END PGP SIGNATURE-----
 
J

Juha Nieminen

Bo said:
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;

You are basically saying that because pointers use a special syntax,
that means that you should put each variable declaration in a separate
line even if those variables are not pointers?

You might argue consistency of style. Well, you might have difficult
reading code like this:

double x = 0, y = 0;
double* xp = 0;
double* yp = 0;

but I don't.
 
B

Bo Persson

Juha said:
You are basically saying that because pointers use a special
syntax, that means that you should put each variable declaration in
a separate line even if those variables are not pointers?

I am saying that as some of the declarators apply only to the first
declared object, and some to all of them, it is confusing. Especially
if you also add a bunch of initializers.

Defining a bunch of uninitialized variables is unusual, and should not
use a special syntax just bacause you can.
You might argue consistency of style. Well, you might have
difficult reading code like this:

double x = 0, y = 0;
double* xp = 0;
double* yp = 0;

but I don't.

Good for you. :)


Bo Persson
 
J

James Kanze

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;

Yes and no. Obviously, even better would be something like:

Point p1 ;
Point p2 ;

The verbosity is largely due to a lack of higher level
abstractions. But I didn't want to get into that in my answer,
so I just left it.

Just as obviously, it's pretty much forbidden to declare more
than one variable in a single statement. This particular case
is probably at the limit of where the rule is applicable, but in
well designed code, you probably don't run into such cases often
enough to justify allowing exceptions.
 
J

James Kanze

You are basically saying that because pointers use a special
syntax, that means that you should put each variable
declaration in a separate line even if those variables are not
pointers?

That's not really the only point.
You might argue consistency of style.

That's the real point. There are cases where not following the
rule won't really cause problems; the original code is probably
one of them. But they aren't frequent enough to warrent making
an exception for them. (And how would you formulate a rule
defining the exceptions?)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top