Why do I get a conversion from 'double' to 'float', possible loss of data warning?(c)

10,133

Solution 1

In several lines you multiply by constant values like 0.01 and 0.25. These values are doubles and the result of the multiplication is double which you then store in a float variable having less precision. This gives you a compiler warning. To fix it you can attach the f literal suffix to the constant values, e.g. 0.01f and 0.25f etc.

Solution 2

Because sizeof(double) > sizeof(float) and hence you loose precision.

Share:
10,133
Maybe
Author by

Maybe

Updated on June 05, 2022

Comments

  • Maybe
    Maybe almost 2 years

    I'm writing a program which basically calculates the duty on a car imported. The user is supposed to input the values the values for the price of the car and the shipment. But when I calculate I get this error which is not good.

    //TOTAL DUTY TO BE CHARGED ON A CAR IMPORTED//

    main()
    {
    
            float purchaseprice, shipmentcosts, ecowaslevy, importlevy, GCNETlevy, TotalDuty;
    
            printf("Enter the purchase price and shipment cost");
    
    
            scanf("%f,%f", &purchaseprice, &shipmentcosts);
            ecowaslevy = 0.01f, importlevy = 0.25f, GCNETlevy = 0.05f;
            //FORMULAS FOR THE LEVY//
            ecowaslevy = (purchaseprice + shipmentcosts) * (0.01); 
            importlevy = (purchaseprice + shipmentcosts) * (0.25); 
            GCNETlevy =  (purchaseprice + shipmentcosts) * (0.05); 
            //FORMULA FOR TOTAL DUTY//
            TotalDuty = (ecowaslevy + importlevy + GCNETlevy)*(3.2);
            printf("TOTAL DUTY=GHC.%f", TotalDuty);
            system("pause");
    
    }
    

    These are the warnings:

    1>c:\users\david\documents\visual studio 2013\projects\total expenses\total expenses\tot.c(9): warning C4244: '=' : conversion from 'double' to 'float', possible loss of data

    1>c:\users\david\documents\visual studio 2013\projects\total expenses\total expenses\tot.c(10): warning C4244: '=' : conversion from 'double' to 'float', possible loss of data

    1>c:\users\david\documents\visual studio 2013\projects\total expenses\total expenses\tot.c(11): warning C4244: '=' : conversion from 'double' to 'float', possible loss of data

  • Maybe
    Maybe over 9 years
    In my code i have attached it. Look at at. I still get the warning
  • Martin Liversage
    Martin Liversage over 9 years
    @DavidAsmah: No, you clearly use the literal 0.01 in line 9 which is a double literal. Replace it with the float literal 0.01f and the warning goes away. I just tested this myself.
  • Maybe
    Maybe over 9 years
    but when i tested it i got a negative value, which is wrong
  • riv
    riv about 5 years
    Why does the warning say "possible loss of data" though, surely it should know whether 0.01 can be represented as a float or not?
  • Martin Liversage
    Martin Liversage about 5 years
    @riv The possible loss of data is when a double is stored in a float variable. This happens at run time and the compiler is emitting a warning about this. The compiler cannot at compile time know if the conversion loses precision. And the loss is possible because it doesn't happen all the time. 1d converts to 1f which is still just 1. However, 0.1 does not have an exact representation using a binary floating point number and 0.1d is not quite the same number as 0.1f. On my computer 0.1f - 0.1d is 1.49011611383365×10⁻⁹.