473,387 Members | 1,669 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

a problem about "sqrt"


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

}
Nov 1 '08 #1
16 2031
li**********@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
Nov 1 '08 #2
<li**********@163.comwrote:
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));

}

Nov 1 '08 #3
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;
Nov 1 '08 #4
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...)

Nov 1 '08 #5
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
Nov 1 '08 #6
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;
Nov 1 '08 #7
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
Nov 1 '08 #8
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
Nov 1 '08 #9
-----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.mozdev.org

iEYEARECAAYFAkkM1DkACgkQPFW+cUiIHNo1uQCfXpFefGkMSR eJUkeiecJdFuCZ
RcIAn2QE9Fgf+lb4IzvLkkKojFHqTx50
=CXW2
-----END PGP SIGNATURE-----
Nov 1 '08 #10
Ian Collins wrote:
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"
Well, I guess the reference is the words "only usable when ...", which is
refuted successfully by the example.
Best

Kai-Uwe Bux
Nov 1 '08 #11

"Pawel Dziepak" <pd******@quarnos.orgwrote in message
news:ge**********@registered.motzarella.org...
-----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?
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.

Nov 1 '08 #12
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Jim Langston wrote:
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+4ACgsBoGN87QXX gLd9uv8YP44B8m
wwIAoLD48tMNxBRSiafulhClqk+ScLYI
=tCL7
-----END PGP SIGNATURE-----
Nov 1 '08 #13
Bo Persson wrote:
> 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;
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.
Nov 2 '08 #14
Juha Nieminen wrote:
Bo Persson wrote:
>> 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;

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
Nov 2 '08 #15
On Nov 1, 10:53*am, Juha Nieminen <nos...@thanks.invalidwrote:
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;
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.

--
James Kanze (GABI Software) email:ja*********@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
Nov 2 '08 #16
On Nov 2, 1:00*am, Juha Nieminen <nos...@thanks.invalidwrote:
Bo Persson wrote:
*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;
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?)

--
James Kanze (GABI Software) email:ja*********@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
Nov 2 '08 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: bq | last post by:
Hello, Two questions related to floating point support: What C compilers for the wintel (MS Windows + x86) platform are C99 compliant as far as <math.h> and <tgmath.h> are concerned? What...
5
by: Tamir Khason | last post by:
Someone knows "Arrange All" windows calculation formula used in MDI?
11
by: Russ | last post by:
I have a couple of questions for the number crunchers out there: Does "pow(x,2)" simply square x, or does it first compute logarithms (as would be necessary if the exponent were not an integer)?...
7
by: Kenneth Brody | last post by:
(From something brought up on "Help with array/pointer segmentation fault needed" thread.) Is "?" a sequence point? Or, more directly, is the following defined? /* Will "ptr" be guaranteed...
15
by: Bjorn Jensen | last post by:
Hi! An beginner question: Pleas help me with this (-: Error (the arrow points on the s in sqrt) ===== tal.java:6: cannot find symbol symbol : method sqrt(int) location: class tal...
48
by: Tony | last post by:
How much bloat does the STL produce? Is it a good design wrt code bloat? Do implementations vary much? Tony
3
by: sigkill9 | last post by:
I've been working on this script all weekend and am almost done with it accept for one last detail; implementing a "process another number?" prompt after a user input is processed. The script...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.