(NaN) during power calculation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • karthigeyantp
    New Member
    • Jul 2008
    • 22

    (NaN) during power calculation

    I need to do this -0.7658^0.5 in c++ . if i use pow(-0.7658,0.5) it shows
    NaN . i don't know what to do. i need it urgently plz some one help me...
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Are you trying to take the square root of a negative number?

    Comment

    • karthigeyantp
      New Member
      • Jul 2008
      • 22

      #3
      Originally posted by r035198x
      Are you trying to take the square root of a negative number?
      I tried but it showed nan.

      Comment

      • karthigeyantp
        New Member
        • Jul 2008
        • 22

        #4
        I f know any way to calculate plz tell me

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by karthigeyantp
          I f know any way to calculate plz tell me
          The square root of a negative number is not a real number.

          Comment

          • donbock
            Recognized Expert Top Contributor
            • Mar 2008
            • 2427

            #6
            Can you tell us a little of the context ... what sort of problem are you trying to solve, and why do you think the square root of this value leads towards the solution.

            Comment

            • donbock
              Recognized Expert Top Contributor
              • Mar 2008
              • 2427

              #7
              The problem here is not in your program -- it has given you the correct answer as far as it is able.

              Are you familiar with any of the following mathematical terms:
              ... Integer
              ... Rational Number
              ... Irrational Number
              ... Real Number
              ... Imaginary Number
              ... Complex Number
              These are the names for various sets of numbers. When solving numerical problems it can sometimes help to think through which set(s) your inputs and output(s) belong to. It is easy to fall into the trap of assuming that floating-point variables lead to the most accurate results, but that isn't always true.

              C/C++ use the floating-point types (float, double) to represent a subset of the Real Numbers. The inputs to and output from pow() are all floating-point (Real) numbers. The term "NaN" is an acronym for "not a number".

              There are no Real Numbers that when squared yield a negative number. That is, the square root of a negative number is not a Real Number. There is an answer, but it isn't a Real Number. Try to solve your problem with a calculator.

              For C89 and C++ (or a calculator) you need to understand the mathematics well enough to express the problem differently (into a form that can be solved with real numbers) and then reinterpret the real number result to get the final answer.

              C99 has an extended math library that can actually solve this problem directly; but you won't be able to use it if you don't understand the underlying mathematics well enough to set up the problem properly.

              Comment

              • karthigeyantp
                New Member
                • Jul 2008
                • 22

                #8
                Originally posted by donbock
                Can you tell us a little of the context ... what sort of problem are you trying to solve, and why do you think the square root of this value leads towards the solution.

                I am calculating the covarience for some problem , from that result finding cross covarience. The covarience is giving -ve number which i want it raise to the power of 0.5. If i use pow(-0.6585,0.5) it gives nan . how do i need to calculate. how do i calculate this problem.

                Comment

                • JosAH
                  Recognized Expert MVP
                  • Mar 2007
                  • 11453

                  #9
                  Why do you want to take the square root of that covariance? If it is negative then
                  you know that a series X decreases while a series Y increases and vice versa.

                  kind regards,

                  Jos

                  Comment

                  • karthigeyantp
                    New Member
                    • Jul 2008
                    • 22

                    #10
                    Originally posted by JosAH
                    Why do you want to take the square root of that covariance? If it is negative then
                    you know that a series X decreases while a series Y increases and vice versa.

                    kind regards,

                    Jos
                    I wat to plot a graph

                    Comment

                    • donbock
                      Recognized Expert Top Contributor
                      • Mar 2008
                      • 2427

                      #11
                      When I say SquareRoot below I'm referring to the mathematical square root operation, not a C/C++ function.

                      One well-known property of SquareRoot is the following ...
                      SquareRoot(a*b) = SquareRoot(a) * SquareRoot(b)

                      It therefore follows that
                      SquareRoot(-0.7658) = SquareRoot(0.76 58) * SquareRoot(-1)

                      The first term can be readily computed on a calculator or via the C/C++ pow function. The second term is, by definition, the value i (the imaginary unit).

                      That is, SquareRoot(-0.7658) = 0.8751i (an Imaginary Number).
                      I don't see how that helps you draw a graph.

                      Comment

                      • karthigeyantp
                        New Member
                        • Jul 2008
                        • 22

                        #12
                        Originally posted by donbock
                        When I say SquareRoot below I'm referring to the mathematical square root operation, not a C/C++ function.

                        One well-known property of SquareRoot is the following ...
                        SquareRoot(a*b) = SquareRoot(a) * SquareRoot(b)

                        It therefore follows that
                        SquareRoot(-0.7658) = SquareRoot(0.76 58) * SquareRoot(-1)

                        The first term can be readily computed on a calculator or via the C/C++ pow function. The second term is, by definition, the value i (the imaginary unit).

                        That is, SquareRoot(-0.7658) = 0.8751i (an Imaginary Number).
                        I don't see how that helps you draw a graph.
                        Thanks a lot for u r solution.
                        Is there anny way to convert from a+bi to real number

                        Comment

                        • JosAH
                          Recognized Expert MVP
                          • Mar 2007
                          • 11453

                          #13
                          Originally posted by karthigeyantp
                          Thanks a lot for u r solution.
                          Is there anny way to convert from a+bi to real number
                          No there isn't. The set of real numbers is a real subset of the set of complex numbers.
                          iow there exist numbers in the complex set that are not in the real set. Any number
                          a+bi for b != 0 is such a number.

                          kind regards,

                          Jos

                          Comment

                          Working...