Quick question about double - float

I have a quick question about the error C4244 "Conversion from double to float - possible loss of data".

Why am I getting this message when I build my program if I don't have one single double type variable ? I've looked around on Google a bit, but everywhere I saw it mentioned it included the person having previously declared double variables.

Thanks.
Using floating point arithmetic is a bag of worms. Even if you don't get that compile error, it may happen anyway.

We can offer much more help for your specific problem if you post the line of code that is causing the problem (plus a few before and after).

It might be as simple as using a floating-point literal in your code -- which is by default a double. You have to add an 'f' to get a float literal:

float pi = 3.141592f;
This is where I'm getting the message: " 'initializing' : truncation from 'double' to 'const float' " (specifically the lines 5 to 8)

1
2
3
4
5
6
7
8
9
10
//Déclaration des variables et constantes

		char Aperforer
                charTypeFaconnage;
		const float PrixBroche = 0.03;	
		const float PrixEncoller = 0.60;
		const float PrixTable = 0.35;
		const float PrixDosCheval = 0.10;
		const float PrixPerforer = 3;				
		float CoutFaconnage;	


And this is where I'm getting the message: "warning C4244: '=' : conversion from 'double' to 'float', possible loss of data" (The bold lines)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//Calcul du coût de façonnage

		if(Aperforer == 'O')
		{
			CoutFaconnage = (NbImpR + NbImpRV) * 0.003;
		}
		else
			CoutFaconnage = 0;	

		if(FormatPapier == '1' || FormatPapier == '2')
		{
			switch(TypeFaconnage)
			{
				case '1':	CoutFaconnage = CoutFaconnage + (NbExe * 0.03);
							break;
				case '2':	CoutFaconnage = CoutFaconnage + (NbExe * 0.60);
							break;
				case '3':	CoutFaconnage = CoutFaconnage + (NbExe * 0.35);
							break;
				case '4': 
				case '5':	CoutFaconnage = CoutFaconnage + 0;
							break;
			}
		}
		else
		{
			switch(TypeFaconnage)
			{
				case '1':	CoutFaconnage = CoutFaconnage + (NbExe * 0.03);
							break;
				case '2':	CoutFaconnage = CoutFaconnage + 0;
							break;
				case '3':	CoutFaconnage = CoutFaconnage + (NbExe * 0.35);
							break;
				case '4':	CoutFaconnage = CoutFaconnage + (NbExe * 0.10);
							break;
				case '5':	CoutFaconnage = CoutFaconnage + 0;
							break;
			}
		}
Last edited on
Well, these are warnings rather than errors, but it is still good practice to eliminate them if you can.

The cause is assigning a value of type double to a variable of type float. The solution is to make the types match.

That gives you two choices. (a) make everything of type float, like this:
1
2
    const float PrixBroche = 0.03f;	
    const float PrixEncoller = 0.60f;

or (b) make everything of type double, like this:
1
2
    const double PrixBroche = 0.03;	
    const double PrixEncoller = 0.60;

Personally, I'd go with the second option and use double throughout. There are very few reasons to prefer the use of float rather than double. I'd recommend using double as a matter of routine, and only use float when there is a compelling reason to do so (which may be extremely rare).
That did it.

I went with option (a) this time because this is part of an assignment and the variables and types were pre-defined, but I will keep option (b) in mind for any other incoming projects.

Thanks a lot !
Just wondering, does somebody have a clue as to why my teacher wouldn't mention these warnings. I'm assuming he'll eventually mention it, but it's not like it's that hard to understand once you've been told so...
Well, which warnings you get will depend upon which compiler you use, and which settings are used.
I'm using VS since that's what they have at school. I just figured the teacher wouldn't make these variables floats if it'd cause these warnings and most likely confuse the entire class.
The teacher has probably not done the assignment himself. Sad, but true.
Well, I think you could talk to your teacher about that. But unless your compiler is configured in a very strict way (perhaps it is?) the program would still have worked even with these warnings. The danger is, if there are hundreds of these warnings, you might miss another message which pointed to a different and significant issue.
I wouldn't waste my breath saying anything to the teacher. In all likelihood he won't actually care.

You might be better served by using lookup tables (arrays) instead of big switch statements.
I see, so they're more nuisances than anything else.

I will look into arrays for sure, thanks for the tip. This particular exercise is about incorporating switch statements actually, but they shouldn't be mandatory in the future.
Topic archived. No new replies allowed.