Thread: square root function

  1. #1
    Unregistered
    Guest

    square root function

    Im wondering how do I get the square root of a number in C. Can any body help. Thanx

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Im wondering how do I get the square root of a number in C.
    The easiest way would be the sqrt() function in math.h.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    Example:
    Code:
    #include <stdio.h>
    #include <math.h>
    int main()
    {
    	printf(sqrt(20));
    	return 0;
    }
    -Luke
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > printf(sqrt(20));
    First parameter to printf is a string

    printf( "%f\n", sqrt(20));

  5. #5
    Registered User stautze's Avatar
    Join Date
    Apr 2002
    Posts
    195
    You could also easily write your own by using Newton's method of successive approximations.
    'During my service in the United States Congress, I took the initiative in creating the Internet.' - Al Gore, March 9, 1999: On CNN's Late Edition

  6. #6
    Unregistered
    Guest
    or use a very simple ( time consuming ) method:

    use a loop using long doubles and do a comparison of number * number to equal the desired number. a very basic approach. this will have some trouble with some numbers, though. didn't test it yet.

    - toaster

  7. #7
    Registered User stautze's Avatar
    Join Date
    Apr 2002
    Posts
    195
    >use a loop using long doubles and do a comparison of number * number to equal the desired number. a very basic approach.

    While your at it, you could reinvent the wheel.
    'During my service in the United States Congress, I took the initiative in creating the Internet.' - Al Gore, March 9, 1999: On CNN's Late Edition

  8. #8
    Unregistered
    Guest
    haha. no.

  9. #9
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    This is my favourite math reference:

    http://mathworld.wolfram.com/SquareRootAlgorithms.html

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  2. Really basic string operation
    By bobthebullet990 in forum C Programming
    Replies: 6
    Last Post: 11-28-2005, 05:18 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM