![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Mar 2006
Posts: 20
Rep Power: 0
![]() |
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;
} |
|
|
|
|
|
#2 |
|
Newbie
|
cast the parethesis to float
|
|
|
|
|
|
#3 |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 644
Rep Power: 4
![]() |
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! |
|
|
|
|
|
#4 |
|
Newbie
Join Date: Mar 2006
Posts: 20
Rep Power: 0
![]() |
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....
|
|
|
|
|
|
#5 | |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Quote:
__________________
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 |
|
|
|
|
|
|
#6 | |
|
Newbie
Join Date: Mar 2006
Posts: 20
Rep Power: 0
![]() |
Quote:
|
|
|
|
|
|
|
#7 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#8 |
|
Newbie
|
You shouldn't be so negative to beginers... Once you didn't know anything either. He'll learn.
|
|
|
|
|
|
#9 | |
|
Professional Programmer
|
Things you will look back on and wish you hadn't said:
Quote:
__________________
The world's first athletic computer geek! The home of PrProgramsStudios How not to post a question: <-- Please don't reply |
|
|
|
|
|
|
#10 | |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Quote:
__________________
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 |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|