Re: a simple arithmetic(?)

From: Stefano Bettelli (bettelli@tn.infn.it)
Date: Tue Feb 22 2000 - 18:28:34 MET


Hi Sanchez,
        a division between integer numbers has different
        rules than a division between floating point
        numbers:

                int a=2, b=3;
                float c = 2/3;

        The result is c == 0.0, because a and b are integers, 
        so the '/' operator is the integer division (like floating
        point division with an additional cast to the integer
        type). After this there is another cast to float, because
        c is such, but you already have zero as result!. To get
        the proper result (assuming that you can't change a and b
        to float) you should have at least one operand which is a
        floating point, so as to trigger the floating point division:

                float c = float(a)/b;
                                                Stefano

----------------------------------------------------------
E-Mail: Stefano Bettelli <bettelli@tn.infn.it>
WWW   : http://meitner.tn.infn.it/~bettelli



This archive was generated by hypermail 2b29 : Tue Jan 02 2001 - 11:50:19 MET