LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-11-2004, 03:22 PM   #1
nostrum
Member
 
Registered: Jun 2004
Location: New York
Distribution: Red Hat
Posts: 32

Rep: Reputation: 15
Uhm a beginner with C question....


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.

Thanks everyone,
Spiros
 
Old 07-11-2004, 03:42 PM   #2
rajatgarg
Member
 
Registered: Oct 2003
Posts: 41

Rep: Reputation: 15
1) i am not very sure abt this, but here it goes,

if x and y are integer, distance is double, then

distance = sqrt(x+y) shd work. If not , try distance = (double) sqrt(x+y);

if distance is integer,

distance = (int) sqrt(x+y);

one of the above will work....i think the problem is: output is double where as your variabl might be integer, so u need typecasting.

2) try :
if ((x>70) && (x <80)) {
...
}

this will do the trick....

PS: please read some elimentary C tutorial.

Regards,
Rajat Garg
Student, Stanford University
 
Old 07-11-2004, 04:10 PM   #3
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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;

Good luck .. PSM
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
qt beginner question fakie_flip Programming 1 08-30-2005 06:10 PM
Qtk n00b question - beginner TuxFreak Programming 3 08-29-2005 11:42 AM
Question from a real beginner dodo1983 Linux - Newbie 26 06-04-2004 01:53 PM
Quick beginner question Selbie Linux - Software 1 06-23-2003 07:44 PM
Beginner question Ephack Linux - Networking 3 10-26-2001 03:40 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:13 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration