C Prog Q: Find Dist btwn 2 Sets of Points Without math.h

In summary, the student is having difficulty finding the square root of a number without the use of the math library. He is looking for a quick and simple method. He could use the Newton-Raphson root finding method or he could keep iterating until the value only changes by say 0.0001 or less from one iteration to the next.
  • #1
mathrocks
106
0
I have a question about a C program that I'm trying to write. I need to write a program that finds the distance between two sets of points (x1, y1) and (x2, y2) without using the math.h header file. I'm currently doing functions in my class, so I suppose I have to write a separate function that calculates the distance. But what I'm having trouble with is how to take the square root without using the math library.
 
Computer science news on Phys.org
  • #2
You could brute force through steps of a fixed size. You could do a binary search. You could remember back to your math classes and use a numerical approximation. You could look up a numerical approximation in a reference book. Depending on your task, the squared difference might even be enough, meaning you don't have to take the square root!
 
  • #3
Use the Newton-Raphson root finding method. It's quick and simple.

- Warren
 
  • #4
Which compiler do you use? You could easily embed 1 line of asm (which is essentially what math.h does).

I'm asking about which compiler because the syntax of inline asm varies from compiler to compiler. I'm sure you'll find it on google though.
 
  • #5
Yes, you could certainly talk to the floating-point unit directly via asm, but I don't really think that's what the teacher intends for him to do. I believe this assignment is designed to teach C, not x86 architecture.

- Warren
 
  • #6
Wow, all great responses but I guess I didn't tell you guys my level of programming. This is my first programming class, it's a beginners course and the most we've done is recursive functions and this problem was in that section. Any way to do it really simple?
 
  • #7
I believe my answer is quite simple to implement. The Newton-Raphson method is an iterative way to numerically find a zero of a function.

If you're looking for the square root of some number m, it should be obvious that you're looking for the zero of the function [itex]f(x) = x^2 - m[/itex].

The first derivative of this function is [itex]f^\prime (x) = 2x[/itex].

The Newton-Raphson method begins with a guess. Any guess will do, but a bad guess will require more iterations to converge on the final answer. To apply the Newton-Raphson method, plug your guess into the relation

[tex]x_{n+1} = \frac{x_n^2 - m}{2 x_n}[/tex]

Where [itex]x_n[/itex] is the current value, and [itex]x_{n+1}[/itex] is the next value. Keep doing this iteration, plugging that [itex]x_{n+1}[/itex] back in as [itex]x_n[/itex]. You'll see that after five or ten iterations, you've calculated a very very good approximation of the square root.

You could also keep iterating until the value only changes by say 0.0001 or less from one iteration to the next, guaranteeing at least that much precision.

You should be able to write this code in something like three or four lines of C code. If you need additional help, please let me know.

- Warren
 
  • #8
Use Newtons method as already stated.
Here, this process should help: http://www.merriampark.com/bigsqrt.htm

You can do this as a recursive function, but that's not required.

Code:
//the non-recursive way to do it.
//doing this recursively is just as easy, good luck.
double squareRoot(double numToRoot, double guess)
{
	while(true) //sets up an infinite loop
	{
		//function to approximate the root
		double n=((numToRoot/guess) + guess)/2; 
		if (n==guess) //breaks out of loop when root is found
		break;
		guess=n; //reassignes guess for next iteration of loop
	}
	return guess; //returns the root to the function call.
}

Just as an aside, though you're learning about recursive functions, you should also avoid them like the plague unless absolutely necessary. Recursive functions can become unweildy very quickly.

Well, good luck.

[edit] Also, the above code probably won't work if you cut and paste it. If you're learning C see if you can spot the 'error' in my implementation.
 
Last edited:
  • #9

1. How do I find the distance between two sets of points without using the math.h library in C?

To find the distance between two sets of points without using the math.h library in C, you can use the Pythagorean theorem. This theorem states that the distance between two points (x1,y1) and (x2,y2) is equal to the square root of the sum of the squared differences between their x and y coordinates. In C, this can be implemented as:
double distance = sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));

2. Can I use the math.h library in C to find the distance between two sets of points?

Yes, the math.h library in C contains the sqrt() and pow() functions that can be used to find the distance between two sets of points using the Pythagorean theorem. However, if you do not want to use the math.h library, you can implement the formula manually as shown in the previous answer.

3. Is there a more efficient way to find the distance between two sets of points without using the math.h library in C?

Yes, there are other ways to find the distance between two sets of points without using the math.h library in C. One way is to use the Euclidean distance formula, which is the square root of the sum of the squared differences between the coordinates of the two points. This formula can be implemented as:
double distance = sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));

4. Can I find the distance between two sets of points without using any mathematical operations in C?

No, you will need to use some mathematical operations in C to find the distance between two sets of points. This is because calculating distance involves finding the square root and performing some basic arithmetic operations such as addition and subtraction.

5. Is it necessary to use the Pythagorean theorem to find the distance between two sets of points in C?

No, while the Pythagorean theorem is a commonly used method to find the distance between two sets of points, there are other distance formulas that can be used. For example, the Manhattan distance formula, which calculates the distance by adding the absolute differences between the x and y coordinates, can also be used in C. It is up to the programmer to choose the most appropriate formula for their specific application.

Similar threads

  • Programming and Computer Science
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
14
Views
2K
  • Computing and Technology
Replies
2
Views
10K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • Programming and Computer Science
3
Replies
81
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Programming and Computer Science
Replies
14
Views
31K
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Introductory Physics Homework Help
Replies
7
Views
1K
Back
Top