Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   C++ strange arithmetic? (http://www.programmingforums.org/showthread.php?t=8907)

Mcoy Mar 16th, 2006 9:12 PM

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...

Mrafcho001 Mar 16th, 2006 9:16 PM

cast the parethesis to float

Jessehk Mar 16th, 2006 9:20 PM

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;


Mcoy Mar 16th, 2006 9:26 PM

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....

DaWei Mar 16th, 2006 9:34 PM

Quote:

I've been practicing c++ for a good couple weeks and never knew that....
LOL!!!!!11111eleven

Mcoy Mar 16th, 2006 9:42 PM

Quote:

LOL!!!!!11111eleven
Yeah I know it sounds stupid, but you'd think itd be one of the first things you learn.

DaWei Mar 16th, 2006 10:06 PM

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.

Mrafcho001 Mar 16th, 2006 10:14 PM

You shouldn't be so negative to beginers... Once you didn't know anything either. He'll learn.

Prm753 Mar 16th, 2006 10:22 PM

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.


DaWei Mar 16th, 2006 10:36 PM

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?


All times are GMT -5. The time now is 9:30 AM.

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