Simple Float Question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • TamaThps
    New Member
    • Oct 2008
    • 12

    Simple Float Question

    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.
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	float average;
    	float average2;
    
    	average=3.5;
    	average2=(7/2);
    
    	cout << average << endl;
    	cout << average2 << endl;
    
    	return 0;
    }
    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.
  • whodgson
    Contributor
    • Jan 2007
    • 542

    #2
    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

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      Code:
      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.

      Comment

      • TamaThps
        New Member
        • Oct 2008
        • 12

        #4
        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)
        Code:
        if ((length+1)%2 ==0)
        	{
        		median= ((value[length/2]+value[length/2+1])/2);
        	}
        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

        Comment

        • scruggsy
          New Member
          • Mar 2007
          • 147

          #5
          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.
          Code:
          median= ((value[length/2]+value[length/2+1]) / 2.0);
          If you were not using literal values then you would want to do an explicit cast of one of the operands to a double.

          Comment

          Working...