|
|
|
186
   
Group: Forum Members
Last Login: 17/12/2008 12:02:39
Posts: 112,
Visits: 196
|
|
how would i find the sqaure root of something using c?
Cheers,Myyy Yea!, yea!, yeeeaa!
|
|
|
|
|
286
   
Group: Forum Members
Last Login: 03/01/2009 17:27:50
Posts: 231,
Visits: 384
|
|
C, C+, C++ or C#?
For C++ have a look here - I'm not sure about the others, but I'm sure they'll be similar.
Rad
Seeking true enlightenment and a decent sig!
|
|
|
|
|
Pentium
   
Group: Forum Members
Last Login: Today @ 00:14:47
Posts: 3,012,
Visits: 6,161
|
|
just wondering but what are you programming?
-------------------------------------------------------------------
www.alex3410.com <- updated take a look (29/10/08)
CPU: core2duo E4300 1.8GHz (@2.6GHz), RAM: 2GB corsair XMS2, Gcard: Geforce 7900GS 256MB, Mboard:ASROCK4CoreDual-SATA2, HDD: 1 X 500GB 2X250GB, Monitor:19" acer widescreen

|
|
|
|
|
386
   
Group: Forum Members
Last Login: Yesterday @ 20:25:59
Posts: 586,
Visits: 1,439
|
|
If you are using the standard C library then you would use:
#include limits.h /* for range checking*/
#include math.h /*standard c library*/
the latter internally includes xmath.h
note the header should have a 'less than' before and a 'greater than' after, but the forum strips these out!
The function sqrt takes a double as an argument and returns a double. It works by partitioning the argument into an exponent and a fraction. The function first computes a quadratic least-squares fit then applies Newton's method three times to obtain the needed precision.
Square root calculations are frequently built into hardware nowadays and the compiler optimiser should take care of this automatically, however if you are using primitive hardware (e.g. a PIC or similar) then be aware that sqrt is a relatively time intensive routine, and if in a big time critical loop you may want to consider squaring the loop's contents to avoid doing more than once.
[EDIT] If you are asking 'how' does it work, then just print out the sqrt section in math.h and examine it. You will find that a good half of the code is given over to range/error checking.
If you have just started learning C, then you will soon need to get a good book to help you come to grips with pointers. 'The C Odyssey' by Vijay Mukhi is a very easily read series of books. A bit dated nowadays as the trendy ones have moved over to C++ object oriented code. However if you want really tight coding for BASIC controllers and the like then C is almost as good as Assembler (I accept that modern pc compilers have taken out most of the C++ overhead, so C++ is better/easier to maintain in this case).
Once you have a reasonable grasp of C's structure I'd recommend you try and get hold of 'Michael Abrash's Graphics Programming Black Book' to see how the first Quake program was written in C!
|
|
|
|
|
386
   
Group: Forum Members
Last Login: Yesterday @ 20:25:59
Posts: 586,
Visits: 1,439
|
|
I should have course have given you an online resource - try here . Once you tune in, you should find the basic syntax very easy to learn. As I said before, people seem to have problems with pointers. I should have also warned you that stack/heap memory control and garbage collection are huge problem areas once you start using strings/arrays. Learn and relearn about memory management, instantiation and scope!
More modern languages like C# try to automate these areas as if badly done, it results in memory loss, system resources disappearing and program/pc crashes. However if you learn to handle these areas in C you will be a much better C++ or C# or even java coder, as you will be able to do 'illegal' things that other coders find difficult/impossible.
|
|
|
|