Connecting Tech Pros Worldwide Forums | Help | Site Map

Simple Float Question

Newbie
 
Join Date: Oct 2008
Posts: 12
#1: Mar 10 '09
I made this code as an example of my question. The actual program I will be making is bigger but I made this just to test, and be able to show what I'm asking.
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6.     float average;
  7.     float average2;
  8.  
  9.     average=3.5;
  10.     average2=(7/2);
  11.  
  12.     cout << average << endl;
  13.     cout << average2 << endl;
  14.  
  15.     return 0;
  16. }
  17.  
The result is:
3.5
3
If average2 is a float and is being set equal to (7/2) why isn't it 3.5 instead of 3? I know for int values it drops off the remainder or decimal but if average2 is a float why won't it keep the decimal? It's probably something really easy or stupid I've forgotten.

Thanks.

Familiar Sight
 
Join Date: Jan 2007
Posts: 189
#2: Mar 10 '09

re: Simple Float Question


You are promising that variable1 is a float but you initialise it with ints so it returns an int result.
Change 7 to 7.0 and 2 to 2.0
rgds
Expert
 
Join Date: Mar 2008
Location: Naperville, Illinois U.S.
Posts: 831
#3: Mar 10 '09

re: Simple Float Question


Expand|Select|Wrap|Line Numbers
  1. float average2 = (7/2);
Another way to explain it is that the compiler first evaluates the expression to the right of the equals sign. It sees one integer divided by another integer and therefore decides to use integer mathematics, yielding an integer result of 3 or 4. Then it tries to assign that value to the variable on the left side of the equals sign but finds a type mismatch, so it casts the integer result to float.

By the way, literal constants 3.5, 7.0, and 2.0 are doubles, that will end up being cast to floats in a similar process. Literal float constants are accomplished through an "f" suffix: 3.5f, 7.0f, and 2.0f.
Newbie
 
Join Date: Oct 2008
Posts: 12
#4: Mar 10 '09

re: Simple Float Question


Thank you guys, you did a good job explaining how I cannot get a decimal value from dividing 7/2but can with 7.0/2.0. I'm not sure how I can change an integer value to a double or float though. Here's a snippet of my code (the part that applies to my question)
Expand|Select|Wrap|Line Numbers
  1. if ((length+1)%2 ==0)
  2.     {
  3.         median= ((value[length/2]+value[length/2+1])/2);
  4.     }
  5.  
In this case value[] is an array of int values. If the total length of the array is EVEN you will need to take the average of the two middle numbers to find the median. In this case the code will end up being
median=(3+4)/2
Which returns a 3 as the median like we discussed above. My question now is how can I get around that, in order for it to return 3.5? I have to keep the array as an integer array too.

Thanks again
Familiar Sight
 
Join Date: Mar 2007
Posts: 148
#5: Mar 11 '09

re: Simple Float Question


As mentioned previously if one of the operands is a double then the other will be promoted to double as well.
Since you are using the literal (integer) value '2' just replace it with '2.0' to get the desired result.
Expand|Select|Wrap|Line Numbers
  1. median= ((value[length/2]+value[length/2+1]) / 2.0);
  2.  
If you were not using literal values then you would want to do an explicit cast of one of the operands to a double.
Reply