Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. how to square a value

how to square a value

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++helptutorial
8 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    lindag1783
    wrote on last edited by
    #1

    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

    R E S A 4 Replies Last reply
    0
    • L lindag1783

      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

      R Offline
      R Offline
      Russell
      wrote on last edited by
      #2

      Build it by yourself: #define SQR(x) ((x)*(x)) and/or double sqr(double const& x){ return SQR(x); } -- modified at 9:32 Monday 15th May, 2006

      1 Reply Last reply
      0
      • L lindag1783

        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

        E Offline
        E Offline
        Eric Dahlvang
        wrote on last edited by
        #3

        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

        L 1 Reply Last reply
        0
        • L lindag1783

          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

          S Offline
          S Offline
          Stephen Hewitt
          wrote on last edited by
          #4

          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

          R 1 Reply Last reply
          0
          • E Eric Dahlvang

            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

            L Offline
            L Offline
            lindag1783
            wrote on last edited by
            #5

            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

            E 1 Reply Last reply
            0
            • L lindag1783

              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

              E Offline
              E Offline
              Eric Dahlvang
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              • S Stephen Hewitt

                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

                R Offline
                R Offline
                Russell
                wrote on last edited by
                #7

                Than 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:)

                1 Reply Last reply
                0
                • L lindag1783

                  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

                  A Offline
                  A Offline
                  Alton Williams
                  wrote on last edited by
                  #8

                  simple Linda you do this

                  double squared(double dNumberIn)
                  {
                    return dNumberIn *= dNumberIn;
                  }
                  

                  all the best Alton

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups