how to square a value
-
Hi I am wondering how you can square a value in C++. I want to work out the square of a number that the user inputs but I dont know how you would type in the command so that the computer will square this number. What is the command for squared? Please help. Thank You. linda
-
Hi I am wondering how you can square a value in C++. I want to work out the square of a number that the user inputs but I dont know how you would type in the command so that the computer will square this number. What is the command for squared? Please help. Thank You. linda
-
Hi I am wondering how you can square a value in C++. I want to work out the square of a number that the user inputs but I dont know how you would type in the command so that the computer will square this number. What is the command for squared? Please help. Thank You. linda
pow Calculates x raised to the power of y. #include <math.h> #include <stdio.h> void main( void ) { double x = 9.0, y = 2.0, z; z = pow( x, y ); printf( "%.1f to the power of %.1f is %.1f\n", x, y, z ); }
9 to the power of 2 is 81
---------- There go my people. I must find out where they are going so I can lead them. - Alexander Ledru-Rollin -
Hi I am wondering how you can square a value in C++. I want to work out the square of a number that the user inputs but I dont know how you would type in the command so that the computer will square this number. What is the command for squared? Please help. Thank You. linda
The best way is just to multiply it by itself. Using
pow
is fine but not as efficient as simply multiplying the number by itself as it’s more general. Defining a macro is not a good idea (I can hear the screams of people objecting already): prefer inline functions to macros. There are many reasons for this, including: - Macros are hard to debug. For example try stepping into a macro. - Macros don't respect the C++ language rules. For example you can't put them into namespaces or use function overloading. - Macros can have side effects which are very hard to identify and debug. Macros have their place but most C++ programmers overuse them. Steve -
pow Calculates x raised to the power of y. #include <math.h> #include <stdio.h> void main( void ) { double x = 9.0, y = 2.0, z; z = pow( x, y ); printf( "%.1f to the power of %.1f is %.1f\n", x, y, z ); }
9 to the power of 2 is 81
---------- There go my people. I must find out where they are going so I can lead them. - Alexander Ledru-RollinHi everyone thanks a bunch for all your help here. :-) I did want the value to be squared, not power to, it is the opposite of what pow does (if pow is to the power of - seeing it starts with Pow for Power). I was after the square - you know the square button on your calculator. So the square root. Thanks:laugh: linda
-
Hi everyone thanks a bunch for all your help here. :-) I did want the value to be squared, not power to, it is the opposite of what pow does (if pow is to the power of - seeing it starts with Pow for Power). I was after the square - you know the square button on your calculator. So the square root. Thanks:laugh: linda
In English, the Square of a number is the number raised to the power of two (or, multiplied by itself). The Square Root of a number is "that number or quantity which, multiplied by itself, produces the given number." sqrt Calculates the square root. double sqrt( double x ); Example /* SQRT.C: This program calculates a square root. */ #include <math.h> #include <stdio.h> #include <stdlib.h> void main( void ) { double question = 45.35, answer; answer = sqrt( question ); if( question < 0 ) printf( "Error: sqrt returns %.2f\n, answer" ); else printf( "The square root of %.2f is %.2f\n", question, answer ); } Output The square root of 45.35 is 6.73 ---------- There go my people. I must find out where they are going so I can lead them. - Alexander Ledru-Rollin
-
The best way is just to multiply it by itself. Using
pow
is fine but not as efficient as simply multiplying the number by itself as it’s more general. Defining a macro is not a good idea (I can hear the screams of people objecting already): prefer inline functions to macros. There are many reasons for this, including: - Macros are hard to debug. For example try stepping into a macro. - Macros don't respect the C++ language rules. For example you can't put them into namespaces or use function overloading. - Macros can have side effects which are very hard to identify and debug. Macros have their place but most C++ programmers overuse them. SteveThan you for the great lesson:-O: I can add olso that they could be coded into a slower code: (see my prev post and...)
SQR(3+5*7-6+sqr(5.9))
will be, unfortunatly:sigh:, coded into( 3+5*7-6+sqr(5.9)) ) * ( 3+5*7-6+sqr(5.9)) )
and we can say goodbye to the powerfull C++! (Sorry, but nobody will read this post!:sigh:) -
Hi I am wondering how you can square a value in C++. I want to work out the square of a number that the user inputs but I dont know how you would type in the command so that the computer will square this number. What is the command for squared? Please help. Thank You. linda
simple Linda you do this
double squared(double dNumberIn) { return dNumberIn *= dNumberIn; }
all the best Alton