This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Type casting problem


Hello all. I am having a bit a problem with type
casting a double to a long. I am using gcc version
2.96 20000731 (Red Hat Linux 7.1 2.96-81), on an x86
type processer.

Here's the code:

#include <stdio.h>

int main(int argc, char *argv[])
{
    double  amount;
    double  cost;
    long    iamount;

    cost = 0.06;
    amount = cost * 100;
    iamount = (long)(((double)cost) * 100);

    printf("Amount %lf\n", amount);
    printf("Integer Amount %ld\n", iamount);

    return (0);
}

Here's the output:

Amount 6.000000
Integer Amount 5


There should be no difference between the 2 numbers,
but the type cast seems to be losing some presision
somewhere. Any help or tips would be greatly
appeciated.

Thanks,

_Kevin


__________________________________________________
Do You Yahoo!?
Get personalized email addresses from Yahoo! Mail - only $35 
a year!  http://personal.mail.yahoo.com/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]