Help with simple division

This is a discussion on Help with simple division within the C++ Programming forums, part of the General Programming Boards category; Hello. I'm just beginning learning C++ and I'm having a problem with simple division. First, here's the code that I'm ...

  1. #1
    Registered User
    Join Date
    Feb 2004
    Posts
    9

    Help with simple division

    Hello. I'm just beginning learning C++ and I'm having a problem with simple division. First, here's the code that I'm having a problem with. . .

    Code:
    #include<iostream>
     
    int main()
    {
    double number = 1;
    while(1)
      {
      double eight = .125*number;
      double one = 1;
      double seven = 7;
      double fract = one/seven;
      double seventh = fract * number;
      double test = 4100 + seventh + eight;
      std::cout << number << " " << seventh << " " << eight << " " << test << std::endl;
         if (test == number)
         break;
      number+=1;
      }
    return 0;
    }
    The program works, but I'd like to know how to simply divide one by seven without having to define them as doubles first. For such a small program, it really doesn't make a difference but I want to know why something like the following doesn't work. . .

    Code:
    double test = 4100 + ((1/7)*number) + ((1/8)*number);
    Finally, if it's not too much trouble, can someone explain the right way to do the following? I'm pretty sure that this isn't the right way.

    Code:
    while(1)
    {
    //code
    }
    Thank you very much.

    -Matt

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    You need floating point numbers - ie, 7.0, and not 7.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    To divide 1 by seven, go (1.0/7) or (1/7.0) or (1.0/7.0).

    See,
    Code:
    double seven = 7;
    automatically converts 7 to a double, 7.0.

    (1/7) will be 0, because you're using whole numbers, not floating point numbers.

    One last note: To convert an int to a double, rather than go
    Code:
    int i = 4;
    double r = (i*1.0)/7.0;
    you can use a cast
    Code:
    int i = 4;
    double r = (double)i / 7.0;
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Code:
    while(1)
    {
    //code
    }
    That code will work. But you need to terminate the loop at some time or another, using a break statement.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    >> That code will work. But you need to terminate the loop at some time or another, using a break statement.

    You missed this:
    Code:
    if (test == number)
    break;
    Of course comparing floating point numbers for equality is a bad idea.
    http://www.parashift.com/c++-faq-lit...html#faq-29.17
    Last edited by Daved; 07-12-2005 at 12:48 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. creating very simple text editor using c
    By if13121 in forum C Programming
    Replies: 9
    Last Post: 10-19-2010, 06:26 PM
  2. Simple message encryption
    By Vicious in forum C++ Programming
    Replies: 10
    Last Post: 11-07-2004, 11:48 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  4. Simple simple program
    By Ryback in forum C++ Programming
    Replies: 10
    Last Post: 09-09-2004, 06:48 AM
  5. Need help with simple DAQ program
    By canada-paul in forum C++ Programming
    Replies: 12
    Last Post: 03-15-2002, 08:52 AM

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