Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 16th, 2006, 8:12 PM   #1
Mcoy
Newbie
 
Mcoy's Avatar
 
Join Date: Mar 2006
Posts: 20
Rep Power: 0 Mcoy is on a distinguished road
C++ strange arithmetic?

Why is it that when I add two integers values(5 and 2),divide them by two and assign the result to a float value,
that the float value refuses to have decimals?
ex:

#include <iostream>
using namespace std;

int main(){
    
    int number1(5);
    int number2(2);
    float result;
    
    result = (number1+number2)/2;
    cout << result;
    
    
    cin >> number1;
    return 0;
}
Outputs 3...
Mcoy is offline   Reply With Quote
Old Mar 16th, 2006, 8:16 PM   #2
Mrafcho001
Newbie
 
Join Date: Dec 2005
Posts: 4
Rep Power: 0 Mrafcho001 is on a distinguished road
Send a message via AIM to Mrafcho001
cast the parethesis to float
Mrafcho001 is offline   Reply With Quote
Old Mar 16th, 2006, 8:20 PM   #3
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 644
Rep Power: 4 Jessehk is on a distinguished road
When you add number1 and number2 (both being of type int), the result of the calculation is int.

When you divide an integer by another integer, you will also end up with an integer (hence your answer of 3 -- the remainder is ignored).

The solution (like Mrafcho001 said), is to cast number1 + number2 as a float.

result = static_cast<float>(number1 + number2) / 2;
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS!
Jessehk is offline   Reply With Quote
Old Mar 16th, 2006, 8:26 PM   #4
Mcoy
Newbie
 
Mcoy's Avatar
 
Join Date: Mar 2006
Posts: 20
Rep Power: 0 Mcoy is on a distinguished road
Good thing to know! Could have messed me up later on. I've been practicing c++ for a good couple weeks and never knew that....
Mcoy is offline   Reply With Quote
Old Mar 16th, 2006, 8:34 PM   #5
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Quote:
I've been practicing c++ for a good couple weeks and never knew that....
LOL!!!!!11111eleven
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Mar 16th, 2006, 8:42 PM   #6
Mcoy
Newbie
 
Mcoy's Avatar
 
Join Date: Mar 2006
Posts: 20
Rep Power: 0 Mcoy is on a distinguished road
Quote:
LOL!!!!!11111eleven
Yeah I know it sounds stupid, but you'd think itd be one of the first things you learn.
Mcoy is offline   Reply With Quote
Old Mar 16th, 2006, 9:06 PM   #7
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
No attempt to denigrate you, it just sounds funny, "a good couple weeks, now." Think of it like this, it'll make sense: you operate with two integers. The result is an integer. THEN the assignment is made; a promotion takes place, but there's no fractional value to promote. If you cause the promotion to take place FIRST, you're walking in tall cotton. Besides learning syntax, which is really about all you've had time for, you might want to skim some more esoteric things, such as precedence of operations. In other words, given more than one way to do things, or more than one possible order to do them in, what have the language's authors decided? It's additional effort, but it might pay off in fewer mysteries while you progress.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Mar 16th, 2006, 9:14 PM   #8
Mrafcho001
Newbie
 
Join Date: Dec 2005
Posts: 4
Rep Power: 0 Mrafcho001 is on a distinguished road
Send a message via AIM to Mrafcho001
You shouldn't be so negative to beginers... Once you didn't know anything either. He'll learn.
__________________
http://martin.thejefffiles.com/Sig.gif
My Website
010000110010100100101001
Mrafcho001 is offline   Reply With Quote
Old Mar 16th, 2006, 9:22 PM   #9
Prm753
Professional Programmer
 
Prm753's Avatar
 
Join Date: Oct 2005
Location: United States
Posts: 447
Rep Power: 4 Prm753 is on a distinguished road
Send a message via AIM to Prm753 Send a message via MSN to Prm753
Things you will look back on and wish you hadn't said:
Quote:
Originally Posted by Mrafcho001
You shouldn't be so negative to beginers... Once you didn't know anything either. He'll learn.
__________________
The world's first athletic computer geek!
The home of PrProgramsStudios
How not to post a question: <-- Please don't reply
Prm753 is offline   Reply With Quote
Old Mar 16th, 2006, 9:36 PM   #10
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Quote:
You shouldn't be so negative to beginers... Once you didn't know anything either. He'll learn.
Reread my posts. It's humor, not negativity, and I took the time to explain some things that would allow him to arrive at conclusions with reason and research. You should develop some reason and ability to think before you flap your dumb tongue. You probably floorboard your accelerator before your car's in gear, right?
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:43 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC