//flex table opened by JP

Click to See Complete Forum and Search --> : roots in c++?


pauloh22
01-11-2004, 12:55 AM
how do u do square roots in c++? im trying to do a program to do the quadratic formula so my homework's a little easier. i tried defining my variables like this
m*m = blah blah
but i got errors.. im new to programming, but i cant find how to do roots. also, how would i prevent the program from trying to calculate an irrational number. how would i tell it to just give the number up to a few decimal places?


thanks - paul

nothing
01-11-2004, 02:05 AM
int square;

square = aNumber * aNumber;


That is how you should declare and use your variable.

You can also use the function pow(), found in the cmath header file.

square = pow(aNumber, 2);


If you need more help, just ask ;)

fishybawb
01-11-2004, 06:31 AM
Originally posted by pauloh22
how do u do square roots in c++?

You asked about square roots, but your code seems to suggest you're after squares, which nothing showed you how to do. Seeing as you're doing quadratics, you'll need to find square roots anyway. Include math.h and use the sqrt() function:


int squareRoot;
squareRoot = sqrt(aNumber);


:t

nothing
01-11-2004, 11:32 AM
I better start paying more attention to other people's questions :rolleyes:

It was very late in the night when I answered his question though :D


Sorry :(

fishybawb
01-11-2004, 01:38 PM
Originally posted by nothing
Sorry :(

Don't be, I think he might have been asking for both - you need squares and square roots for quadratics. Plus he wrote:

m*m = blah blah

which would suggest he was trying to do what you said, just getting it a bit muddled. I hope he comes back so I can ask what an irrational number is, my maths sucks. Bit of a problem seeing as I'm experimenting with 3d programming at the minute :eek: :D

nothing
01-11-2004, 02:40 PM
I don't know what an irrational number is eitheir and I don't feel like reading right now, but if you're interrested, take a look at this page I found on google...

Irrational numbers (http://www.jimloy.com/algebra/irration.htm)

:t

fishybawb
01-11-2004, 04:16 PM
Cool :) I'm sure I covered that in school at some point, but the passing years (all 6 of them) have been unkind to my fragile brain cells :D

pauloh22
01-13-2004, 10:04 PM
thanks for the help. and an irrational number is a decimal that cannot be written in decimal form (it goes on forever and doesnt repeat)

:)

ryan855
01-13-2004, 10:34 PM
a general rule to find out if a number is irrational or not is- if it can be made into a fraction it is rational. if it can't be made into a fraction it is irrational