Mathematical Error

This is a discussion on Mathematical Error within the C++ Programming forums, part of the General Programming Boards category; Hello you guys! I don't know why my compiler is giving me this error: Code: code.cpp(23) : error C2296: '^' ...

  1. #1
    Registered User
    Join Date
    May 2005
    Location
    Texas
    Posts
    103

    Mathematical Error

    Hello you guys! I don't know why my compiler is giving me this error:
    Code:
    code.cpp(23) : error C2296: '^' : illegal, left operand has type 'float'
    code.cpp(26) : error C2296: '^' : illegal, left operand has type 'float'
    And here is the place where I get the error:
    Code:
    float Height(float G,float V, float S,int POSNEG)
    {
          if(POSNEG == POSITIVE)
          {return(((-2*V)/(2*G)) + sqrt((-2*S)/(G)) + ((2*V^2)/(4*G))*2*G);}
    
          if(POSNEG == NEGATIVE)
          {return(((-2*V)/(2*G)) - sqrt((-2*S)/(G)) + ((2*V^2)/(4*G))*2*G);}
    
          else
          {
                return ERROR;
          }
    }
    Be easy on me...only 14

  2. #2
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    you are using ^ as an exponentential operation.. when in actuality, it is a bitwise operation using the XOR bitwise operator. common mistake, but easy to fix. try using the pow( ) function from <cmath>
    Last edited by The Brain; 11-25-2006 at 07:53 PM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  3. #3
    Registered User
    Join Date
    May 2005
    Location
    Texas
    Posts
    103
    So how exactly do I implement that function in this case??
    Be easy on me...only 14

  4. #4
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    Code:
    ((2*V^2)/(4*G))*2*G);

    could be written as:
    Code:
    ((pow(2*V, 2)/(4*G))*2*G);
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  5. #5
    Registered User
    Join Date
    May 2005
    Location
    Texas
    Posts
    103
    thanx!!
    Be easy on me...only 14

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Actually, that would be 2*pow(V, 2), assuming that what's meant is 2*(V^2) and not (2*V)^2. In this case it would be easier and more efficient to just write it as 2*V*V.

    Edit: And in the second case, 4*V*V.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,656
    Quote Originally Posted by robatino
    Actually, that would be 2*pow(V, 2), assuming that what's meant is 2*(V^2) and not (2*V)^2.
    Indeed. exponentiation has a higher precedence mathematically than multiplication. So (for ^ being exponentiation and * being multiplication) any mathematician would read 2*v^2 as 2*(v^2) rather than as (2*v)^2.

    Quote Originally Posted by robatino
    In this case it would be easier and more efficient to just write it as 2*V*V.
    Indeed. This is one of the simple common tricks used in numerical code. Exponentiation is often markedly less efficient (in terms of machine clock cycles, and therefore runtime speed) than multiplication --- and a compiler has more chance of optimising 2*v*v than it does of optimising 2*pow(v, 2.0).

    I would also do some basic algebra (elimination of common terms, precedence of operations) to turn this;
    Code:
    return (((-2*V)/(2*G)) + sqrt((-2*S)/(G)) + ((2*V*V)/(4*G))*2*G);
    into
    Code:
       return (sqrt(-2*S/G) + V*V - V/G );

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  2. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 10:15 PM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 02:17 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21