Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 21st, 2006, 2:56 PM   #1
Fall Back Son
Hobbyist Programmer
 
Join Date: Oct 2006
Posts: 190
Rep Power: 2 Fall Back Son is on a distinguished road
How do I take a square root?

I was making a program just to make sure I can, and I decided to make a pythagorean theorem program. what happened is I defined

"C" as C = ( A * A ) + ( B * B )

which gives the wrong answer, because the formula is supposed to be C square = A squared + B squared. Anyway, my question is in the title: is there any way I can just take the square root of ( A * A ) + ( B * B ) ? If so, thanks. If not... and anybody has any good ways that I could accomplish the same thing, please let me know. thanks!
Fall Back Son is offline   Reply With Quote
Old Oct 21st, 2006, 2:59 PM   #2
niteice
Programmer
 
niteice's Avatar
 
Join Date: Aug 2005
Posts: 98
Rep Power: 3 niteice is on a distinguished road
Send a message via AIM to niteice
sqrt((a*a)+(b*b));
niteice is offline   Reply With Quote
Old Oct 21st, 2006, 3:05 PM   #3
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,791
Rep Power: 5 Sane will become famous soon enough
What niteice said. But you must also include the math.h library.

#include <math.h>

Then, for sake of listing alternatives, you can also do...
x_sqrt = pow(x, 0.5)

Although I'm not sure if "pow" accepts a float as a second argument.
Sane is offline   Reply With Quote
Old Oct 21st, 2006, 3:43 PM   #4
Fall Back Son
Hobbyist Programmer
 
Join Date: Oct 2006
Posts: 190
Rep Power: 2 Fall Back Son is on a distinguished road
ok, thanks. out of curiosity... what if I wanted to use the variable C.

So I would have

C = ( A * A ) + (B * B )

then I would have sqrt (C). But that doesn't work, due to me not defining the variable or something like that. I'd appreciate it if somebody can tell me the proper way to make that work... and I'd appreciate it even more if somebody can explain WHY it works that way. thanks. you guys are amazing. btw, the reason linux gave me that it would work is because "called object is not a function"
Fall Back Son is offline   Reply With Quote
Old Oct 21st, 2006, 3:49 PM   #5
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,791
Rep Power: 5 Sane will become famous soon enough
If C is not defined, then define it! Wherever you define "A" and "B", just add in "C" to define it as well.

If that's not the problem, you can post your code (wrapped in [code][/code] tags), and we could give you further assistance.
Sane is offline   Reply With Quote
Old Oct 21st, 2006, 3:51 PM   #6
Fall Back Son
Hobbyist Programmer
 
Join Date: Oct 2006
Posts: 190
Rep Power: 2 Fall Back Son is on a distinguished road
C was defined, as far as I know. It said "float A, B, C ; " which I think, at least, defines C. Anyway, code will be edited in here

#include <stdio.h>
#include <math.h>
int main ()

{
float A, B, C, sqrt ; 

/* get A from user */ ;

printf (" enter variable A : ") ;
scanf ("%f", &A) ; 

/* get B from user */ ;

printf (" enter variable B : ") ; 
scanf ("%f", &B) ; 

/* calculate C */ ; 

C = ( A * A ) + ( B * B ) ; 
C = sqrt (C) ; 

printf (" %.2f C \n", C) ; 

return 0 ; 

}
Fall Back Son is offline   Reply With Quote
Old Oct 21st, 2006, 4:00 PM   #7
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,791
Rep Power: 5 Sane will become famous soon enough
The function "sqrt" accepts an integer value. You are giving it a float. This can be fixed by using the function "sqrtf". The "f" on the end is there because it will accept a float as its parameter, just as "sqrtl" accepts a long integer.

New Code:
C = ( A * A ) + ( B * B ) ; 
C = sqrtf (C) ;

And I don't want to confuse you, but it's important to note that it can also be rewritten as:
C = sqrtf ( ( A * A ) + ( B * B ) );
This has to do with the "transitive" property of mathematics, as it applies to computer programming. Essentially, anything that's equivilent, can be restated wherever it applies.
Sane is offline   Reply With Quote
Old Oct 21st, 2006, 5:58 PM   #8
Fall Back Son
Hobbyist Programmer
 
Join Date: Oct 2006
Posts: 190
Rep Power: 2 Fall Back Son is on a distinguished road
Thanks. and for the record, I understand mathematics, I am mostly just a noob at programming & chemistry. Lol. Thanks for all the help though.
Fall Back Son is offline   Reply With Quote
Old Oct 21st, 2006, 6:09 PM   #9
Fall Back Son
Hobbyist Programmer
 
Join Date: Oct 2006
Posts: 190
Rep Power: 2 Fall Back Son is on a distinguished road
It still does not work though. If i remove "sqrtf" from the variable declaration (where it says float A, B, C, sqrtf ; ) the compiler tells me that there is a "implicit differentiation of function sqrtf" and that there is an "undefined reference to sqrtf."

If I leave sqrtf in the variable declaration, it says that on line 21 (the line where it says C = sqrtf (C) )

there is an "error: called object is not a function."

wth??!!! haha
Fall Back Son is offline   Reply With Quote
Old Oct 21st, 2006, 6:14 PM   #10
stevengs
Professional Programmer
 
stevengs's Avatar
 
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4 stevengs is on a distinguished road
you declared sqrt to be a float.. then you try to call it as a function???
__________________
-Steven
"Is this a piece of your brain?" - Basil Fawlty
stevengs is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
test lostcauz Coder's Corner Lounge 9 Jul 18th, 2006 3:20 PM
Sudoku program info & description Adak Software Design and Algorithms 4 Jul 2nd, 2006 12:49 PM
Square Roots? Jakeyman_I_Am Visual Basic .NET 1 Jan 24th, 2006 4:01 PM
BinaryTree , I need help on inserting expression to the Tree Master C# 3 Oct 14th, 2005 3:42 PM
Magic Square program help :) Benoit C 9 May 2nd, 2005 12:39 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:36 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC