Thread: math function (order of operations)

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    23

    math function (order of operations)

    Can someone please explain the difference between the two, given the same radius? They produce different results. Thanks in advance

    Code:
    #define PI 3.1416
    volume = (4*PI/3)*pow(radius,3);
    volume = (4/3) * PI * pow(radius,3);

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Hint: integer division.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    23
    ahh okay thanks.

    so if you do (4.0/3.0) it should work, right?

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Yes. At least one of the constants needs to be floating point representation so that the compiler uses floating point math.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Order of operations question
    By nicomp in forum C++ Programming
    Replies: 9
    Last Post: 09-20-2006, 07:18 PM
  2. Order of Operations Question
    By chix/w/guns in forum C++ Programming
    Replies: 35
    Last Post: 07-17-2004, 03:38 AM
  3. order of operations...
    By jverkoey in forum C++ Programming
    Replies: 2
    Last Post: 05-21-2004, 11:38 PM
  4. Order of Operations
    By C-Duddley in forum C Programming
    Replies: 3
    Last Post: 12-06-2002, 08:10 PM
  5. order of operations
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 10-31-2002, 08:51 PM