ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi yea I'm just beginning ot work with C and I had a two questions. By the way thanks to everyone who has helped me in the past, I much obliged.
Ok here goes:
1) I'm trying calculate the distance between two points on a plain and i was trying to use the sqrt() function from math.h, but I can't get the syntax right. I was trying to do this basically:
/* brace yourself for some bad pseudo code */
x, y are my variables for the squared value of the the previous computation which is (x2- x1)e2 + (y2 - y1)e2 so far i've squared subtracted the coordinances and squared them so to complete the distance formula:
distance = sqrt(x + y)
When I man'd sqrt it showed the syntax as double (sqrt double x) I think? I tried that and every other substitution with double and such and still can't get it working. I've also just tried distance = sqrt(x +y) and that didn't work either.
Any ideas?
2) How would I go about in an if declaration I wanted a to exectute the effect if a number was between two numbers. e.g.:
if (x < 70 > 60)
printf("Student has D')
I'm not sure how do setup the statement would the && operator work? or would what I just typed above work? Sorry I'm lost any help would be great.
In other words, Rajat and I are guessing that the problem is numeric conversion errors between floats, doubles and integers.
Floating point constants (like "1.0") default to "float" in C/C++ (thankfully, they now default to "double" in languages like Java and C#), input parameters and return values are usually "double" in the C/C++ math libraries, and it sounds like at least some of your variables are "int".
The easiest, safest approach is:
1. Make all of the variables you pass into the math routines, and use in arithmetic expressions, "double".
2. Explicitly cast *ALL* of your floating point literals to "double", *ALWAYS*:
x = y * (double)1.0;
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.