Democratic Underground Latest Greatest Lobby Journals Search Options Help Login
Google

Why does 7/2 = 3? (C++)

Printer-friendly format Printer-friendly format
Printer-friendly format Email this thread to a friend
Printer-friendly format Bookmark this thread
This topic is archived.
Home » Discuss » The DU Lounge Donate to DU
 
Goldom Donating Member (1000+ posts) Send PM | Profile | Ignore Wed Oct-20-04 05:41 PM
Original message
Why does 7/2 = 3? (C++)
"cout<<7/2;" gives 3. Not 3.5. It's been far too long since I've done this, what am I forgetting? Tried having it go into a double too, it still said just the 3.
Printer Friendly | Permalink |  | Top
Q3JR4 Donating Member (1000+ posts) Send PM | Profile | Ignore Wed Oct-20-04 05:43 PM
Response to Original message
1. Um....
I may be at a loss, but do you have to tell it that it's dealing with a real number and not an integer?

If this doesn't help, please forget I said anything. :)
Printer Friendly | Permalink |  | Top
 
Kellanved Donating Member (1000+ posts) Send PM | Profile | Ignore Wed Oct-20-04 05:46 PM
Response to Original message
2. because it uses the int "/" operator
Try replacing "7" with "7.0".
Printer Friendly | Permalink |  | Top
 
trogdor Donating Member (1000+ posts) Send PM | Profile | Ignore Wed Oct-20-04 05:46 PM
Response to Original message
3. Because you are dividing two ints.
The result must be an int, so it truncates the remainder. Now, if you want the remainder, 7 % 2 = 1.

If you were dividing two floats, you would get a float back. cout << 7/2 assumes 7 and 2 are integers. Casting an int as a double doesn't do any good, either.

Try cout << (float)7/(float)2. Better yet:

float a(7);
float b(2);
cout << a/b;
Printer Friendly | Permalink |  | Top
 
Goldom Donating Member (1000+ posts) Send PM | Profile | Ignore Wed Oct-20-04 05:48 PM
Response to Original message
4. got it.
had to put a .0 after the number it was dividing for some reason.
Printer Friendly | Permalink |  | Top
 
Commie Pinko Dirtbag Donating Member (1000+ posts) Send PM | Profile | Ignore Wed Oct-20-04 05:49 PM
Response to Reply #4
5. Wait till you get to the STL.
:evilgrin:
Printer Friendly | Permalink |  | Top
 
Endangered Specie Donating Member (1000+ posts) Send PM | Profile | Ignore Wed Oct-20-04 05:59 PM
Response to Original message
6. If youre doing it in C++
You need to write 7.0/2.0 = double;... or you could assign double x=7.0; and double y = 2.0; and then do x/y;
Printer Friendly | Permalink |  | Top
 
DU AdBot (1000+ posts) Click to send private message to this author Click to view 
this author's profile Click to add 
this author to your buddy list Click to add 
this author to your Ignore list Sun May 05th 2024, 01:22 PM
Response to Original message
Advertisements [?]
 Top

Home » Discuss » The DU Lounge Donate to DU

Powered by DCForum+ Version 1.1 Copyright 1997-2002 DCScripts.com
Software has been extensively modified by the DU administrators


Important Notices: By participating on this discussion board, visitors agree to abide by the rules outlined on our Rules page. Messages posted on the Democratic Underground Discussion Forums are the opinions of the individuals who post them, and do not necessarily represent the opinions of Democratic Underground, LLC.

Home  |  Discussion Forums  |  Journals |  Store  |  Donate

About DU  |  Contact Us  |  Privacy Policy

Got a message for Democratic Underground? Click here to send us a message.

© 2001 - 2011 Democratic Underground, LLC