negative zero in float calculation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mahmoodn
    New Member
    • May 2009
    • 77

    #1

    negative zero in float calculation

    Hi,
    I have written this code:
    Code:
    	float x, y, y1, y2, z, e;
    	float deltax = 0.1, deltay = 0.1;
    	int width = 10;
    	int precision = 4;
    
    	outF << "Zeta" << setw(width) << "Eta"  << endl;
    	outF.setf( ios::fixed | ios::showpoint );
    	outF.precision(precision);
    	for( x=0; x<=0.5; x+=deltax ){
    		y1 = 0.1*x;
    		y2 = 3*x;
    		for( y=y1; y<=y2; y+=deltay ){
    			z = (5.4*x - 1.8*y) / (1.2*y + 10.2);
    			e = (y - 0.1*x) / (0.1*x + 1.4);
    			outF << z << setw(width)<< e << endl;
    		}
    	}
    However the output file looks like this:
    Code:
    Zeta       Eta
    0.0000    0.0000
    0.0511   -0.0000
    0.0331    0.0709
    How can I fix the "negative zero"?
  • mac11
    Contributor
    • Apr 2007
    • 256

    #2
    I'm guessing it's a very small negative number and you just aren't showing enough decimal places to see the value.

    You can show more decimal places or output using scientific notation or if you don't care about the tiny value you can round or truncate it before outputting.

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      You should not use a floating point variable as a loop control variable. Due to the approximate nature of floating point variables you just can't be sure how many times the loop will iterate. Use an int (or integer type) to control loop iterations and then the guarantee that you get exactly the right number.

      Comment

      • mahmoodn
        New Member
        • May 2009
        • 77

        #4
        >I'm guessing it's a very small negative number and you just aren't showing enough decimal places to see the value.

        Yes. It is in the order of 1E-18

        >Use an int (or integer type) to control loop iterations and then the guarantee that you get exactly the right number.

        You are right. thanks for the tip. However the negative zero which is the value of 'e' does not control the loop.

        My problem is how can I be sure that C++ automatically fix -0 and change it to 0

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          e might not control the loop but x does and that is still a floating-point type (float).

          BTW another bit of best practise is to always use double rather than float unless you have a good communicable reason to use float, that is double should be your default choice for a floating point type because it has better precision unless you have a stringent memory requirement.

          The -0.000 does not need "fixing" as far as the maths libraries are concerned, it is correctly displaying a negative number with a very small magnitude to small number of decimal places.

          If you want to show numbers that would display as -0.000 as 0.000 then you will have to alter your code to detect that condition and output something different (or change the value of e) however in doing so you should be aware that you are making the result less accurate.
          Last edited by Banfa; Apr 6 '11, 11:27 AM. Reason: Corrected tags

          Comment

          • mahmoodn
            New Member
            • May 2009
            • 77

            #6
            thanks for your helpful explanation. i understand :)

            Comment

            Working...