warning C4244: '=' : conversion from 'double' to 'float', possible loss of data

This is a discussion on warning C4244: '=' : conversion from 'double' to 'float', possible loss of data within the C++ Programming forums, part of the General Programming Boards category; Hello. I'm still new in programming. (It has been about 2 days since I started reading tutorials online and practicing ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Location
    Brunei
    Posts
    77

    warning C4244: '=' : conversion from 'double' to 'float', possible loss of data

    Hello. I'm still new in programming. (It has been about 2 days since I started reading tutorials online and practicing it). I still have a lot of time, a lot to learn, fortunately the school holiday is long in my local area (about 3 months).

    Anyway, sorry for the long introduction. I just want to ask you a question regarding the warning message I got stated in the title.

    I'm using an IDE, Microsoft Visual 2010 C++ Express. Please, take a look on this and tell me the part that is needed to be altered so that I solve this problem.

    Code:
    //This program is an answer to the question number 1 (now question 2!) given in http://www.doc.ic.ac.uk/~wjk/C++Intro/RobMillerE2.html.
    
    /*To convert Fahrenheit to Celcius:
        substract by 32,
        multiply by 5,
        then divide by 9.
    
    
      To convert Celcius to Kelvin;
         add 273.15.                        */
    
    
    
    
    #include <iostream>
    #include <cmath>
    #include <iomanip>
    using namespace std;
    
    
    int main() {
    
    
        float LOWER, UPPER, STEP;
    
    
         
    
    
        cout << "This program can help you to make a table of conversion of temperature between Fahrenheit, Celcius, and Kelvin.\n";
        cout << "Please specify the range of values of Fahrenheit temperature (from lowest to highest)\n";
        cout << "and also the desired step (e.g: for 10 steps between range 0 and 100 is: 0,10,20,30... 100).\n";
        cout << "by answering the questions given below.\n\n";
    
    
        cout << "Enter the desired lowest value of Fahrenheit temperature and press ENTER.\n";
        cin  >> LOWER >> "\n";
    
    
        cout << "Enter the desired upper value of Fahrenheit temperature and press ENTER.\n";
        cin  >> UPPER >> "\n";
    
    
        cout << "Enter the desired steps within the range and press ENTER.\n";
        cin  >> STEP >> "\n";
    
    
        //declaring variable.
         float Fahrenheit, Celcius, Kelvin;
     
          cout << setiosflags (ios::left);
          cout.width(20);
          cout << "Fahrenheit";
          cout << "Celcius";
          cout << setiosflags (ios::right);
          cout.width(20);
          cout << "Kelvin\n\n";
    
    
          
          cout.setf(ios::fixed);
          cout.precision(2);
    
    
          for (Fahrenheit = LOWER; Fahrenheit <= UPPER; Fahrenheit = Fahrenheit + STEP) {
    
    
              Celcius = ((Fahrenheit - 32)*5)/9;
    
    
             
    
    
              Kelvin = Celcius + 273.15;
    
    
             
    
    
              cout << setiosflags (ios::left);
              cout.width(20);
              cout << Fahrenheit;
              cout << setiosflags (ios::left);
              cout.width (20);
              cout << Celcius;
              cout << setiosflags (ios::right);
              cout.width(20);
              cout << Kelvin << "\n";
          }
          
        system("pause");
    
    
        return 0;
    
    
    }

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,549
    Code:
    cin  >> LOWER >> "\n";
    You can't do this. cin stores input from the user into a variable. "\n" isn't a variable.

    Regarding the warning... the standard says that a double may be larger than a float, and if such is the case, then you may lose data as a double is truncated to fit into a float.

    Kelvin = Celcius + 273.15;
    Here, Kelvin and Celsius are floats, but 273.15 is a double, hence the warning.
    To avoid it, make all variables double or change 273.15 to 273.15f to tell the compiler that it's a float, not a double.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    There are two representations of floating-point numbers - double precision and single precision. A variable declared to be of type float is single precision, one of type double is double precision. Double-precision floating-point numbers can store a greater range of numbers, and store numbers to a greater accuracy, than single-precision floating-point numbers. So when you convert from double to single precision, there's a chance you could lose information.

    Now, when you just write a number with a decimal point in it, like 273.15, that has double precision. In your code you do a couple of calculations involving such numbers:

    Code:
        Celcius = ((Fahrenheit - 32)*5)/9;
        Kelvin = Celcius + 273.15;
    In e.g. Celcius + 273.15 the calculation must be done with both numbers as double-precision numbers, since otherwise the compiler might force you to lose information - so the result of this calculation is a double-precision number. When assigning this to Kelvin, a conversion to single precision must be made, but the compiler cannot circumvent this in the way it did for Celcius + 273.15, since that's what you've told it to do - instead, it issues a warning to let you know that this could be an error.

    Some people will tell you "you can safely ignore this warning" - my advice would be to always fix all warnings your compiler gives you. It will make your life easier.

    There are two solutions - first, you could use an explicit cast so that the compiler knows that the conversion to single precision is what you want, e.g:

    Code:
        Kelvin = (float)(Celcius + 273.15);
    Secondly, you could change the types of your variables to double.
    Programming and other random guff: cat /dev/thoughts > blogspot.com (previously prognix.blogspot.com)

    ~~~

    "The largest-scale pattern in the history of Unix is this: when and where Unix has adhered most closely to open-source practices, it has prospered. Attempts to proprietarize it have invariably resulted in stagnation and decline."

    Eric Raymond, The Art of Unix Programming

  4. #4
    Registered User
    Join Date
    Nov 2012
    Location
    Brunei
    Posts
    77
    Thanks Elysia and JohnGraham!

    From your reply and a good long explanation, I can safely say that the conversion from float (smaller range) to double (larger range) will not lead to any data loss, right?

    And one more thing, the note from which I read from use this kind of 'explicit cast':

    Code:
     Kelvin = static_cast<float>(Celcius + 273.15);


    Is this the same as

    Code:
     Kelvin = (float)(Celcius + 273.15);
    ??

    Anyway, I'm moving on to question 3.

    Thanks again!

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,549
    Quote Originally Posted by Meerul264 View Post
    From your reply and a good long explanation, I can safely say that the conversion from float (smaller range) to double (larger range) will not lead to any data loss, right?
    You are correct. The compiler does that all the time. In your code, it will convert your Celcius variable to a double before doing the calculation before assigning to Kelvin. So it's okay.

    And one more thing, the note from which I read from use this kind of 'explicit cast':

    Code:
     Kelvin = static_cast<float>(Celcius + 273.15);


    Is this the same as

    Code:
     Kelvin = (float)(Celcius + 273.15);
    Yes, it is the same thing. However, explicit casts are safer because they protect you from more mistakes. Hence, if you must use a cast, then you should use the explicit cast.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,265
    Quote Originally Posted by Elysia View Post
    You are correct. The compiler does that all the time. In your code, it will convert your Celcius variable to a double before doing the calculation before assigning to Kelvin. So it's okay.
    The C/C++ standards also specify that the set of values that a float can represent is a strict subset of the values that a double can represent (or words to that effect).

    Quote Originally Posted by Elysia View Post
    Hence, if you must use a cast, then you should use the explicit cast.
    What you say is perfectly correct (assuming strong highlighting of "if you must use a cast"). In this particular case, it is safest to not use a cast at all (eg use a literal value 273.15f rather than 273.15).
    Right 98% of the time, and don't care about the other 3%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. conversion from double to float - loss of data
    By diyteam in forum C++ Programming
    Replies: 20
    Last Post: 03-04-2008, 01:59 AM
  2. Replies: 15
    Last Post: 05-12-2007, 06:22 AM
  3. Double to Int conversion warning
    By wiznant in forum C Programming
    Replies: 15
    Last Post: 09-19-2005, 09:25 PM
  4. Conversion from float to double
    By Ward in forum C++ Programming
    Replies: 17
    Last Post: 10-08-2003, 05:27 PM
  5. 'float' to 'double' conversion warning
    By Achit in forum C Programming
    Replies: 5
    Last Post: 04-07-2003, 06:14 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21