# 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. ## 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;

#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. 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.

3. 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.

4. 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. Originally Posted by Meerul264
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.

6. Originally Posted by Elysia
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).

Originally Posted by Elysia
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).