Thread: Division Calculations

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    45

    Division Calculations

    Hi guys,

    I recently came across an error in my programs. When completing a calculation such as 4 / 3 it automatically returns 1, even if I include it in a double. Please tell me how to get around this problem. It is an essential to my code

    Regards and thanks in advance,
    Regards,

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by mintsmike
    When completing a calculation such as 4 / 3 it automatically returns 1, even if I include it in a double.
    That is because you are still doing integer division although the result is stored in a double variable. The solution is to make at least one of the operands be a double, or you can type cast.
    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
    Mar 2009
    Posts
    45
    Thank you very much.

    I would now like to know how to give it to a certain degree of accuracy or a certain number of decimal points

    Regards

  4. #4
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    double is 15 digits, float is 6. If you want to set the places seen by say std::cout then look for the function std::setprecision (I think that's the one).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Doing calculations on a data file
    By bassist11 in forum C Programming
    Replies: 2
    Last Post: 03-30-2009, 07:47 PM
  2. Get jiffies?
    By pgzh in forum Linux Programming
    Replies: 6
    Last Post: 03-17-2008, 10:30 AM
  3. pseudo code for division
    By svaidya in forum C Programming
    Replies: 14
    Last Post: 05-31-2007, 01:48 PM
  4. modulo 2 division question help
    By shaq8u in forum C Programming
    Replies: 9
    Last Post: 08-20-2003, 08:37 AM
  5. Modular Division problem
    By Malek in forum C++ Programming
    Replies: 7
    Last Post: 05-24-2003, 06:08 PM