warning C4244: '=' : 'double' to 'float'

Mar 14, 2009 at 10:27am
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
41
42
AVSValue Spline(AVSValue args, void*, IScriptEnvironment* env )
{
    float *xa, *ya, *y2a;
	int n;
	float x,y;
	int i;
	bool cubic;

	AVSValue coordinates;

	x = args[0].float AsFloat(0);
	coordinates = args[1];
	cubic = args[2].AsBool(true);

	n = coordinates.ArraySize() ;

	if (n<4 || n&1) env->ThrowError("To few arguments for Spline");

	n=n/2;

    xa  = new float[n+1];
    ya  = new float[n+1];
    y2a = new float[n+1];

	for (i=1; i<=n; i++) {
		xa[i] = coordinates[(i-1)*2].AsFloat(0);
		ya[i] = coordinates[(i-1)*2+1].AsFloat(0);
	}

	for (i=1; i<n; i++) {
		if (xa[i] >= xa[i+1]) env->ThrowError("Spline: all x values have to be different and in ascending order!");
	}
	
	spline(xa, ya, n, y2a);
	splint(xa, ya, y2a, n, x, y, cubic);

    delete[] xa;
    delete[] ya;
    delete[] y2a;

	return y;
}


The three lines from the above code that give me this warning:
warning C4244: '=' : conversion from 'double' to 'float', possible loss of data

line 1
x = args[0].AsFloat(0);

Line 2
xa[i] = coordinates[(i-1)*2].AsFloat(0);

Line 3
ya[i] = coordinates[(i-1)*2+1].AsFloat(0);

I am use visual studio 2008 professional 90 day trial and the 3.5 net sdk from microsoft.

I have looked around the internet for a solution and they say to put the letter f somewhere, but I don't know where.

I tried a long time, 1 or 2 hours so far.
Can you please help me? :)
Mar 14, 2009 at 1:00pm
Seems that AVSValue::AsFloat returns a double, if you want you can cast it to a float
eg:
1
2
3
4
//some ways of doing so:
x = (float) args[0].AsFloat(0);
x = float ( args[0].AsFloat(0) );
x = static_cast<float> ( args[0].AsFloat(0) );

The 'f' suffix is used to specify that a floating point number is a float, not a double:
1.0 = double
1.0f = float
Mar 14, 2009 at 4:34pm
Thank you for the reply. I had some help from the msdn forum and was shown this code.
x = (float)(args[0].AsFloat(0));

I then applied it to my 3 lines of code that were giving me the warning and it was fixed.

This is what the code looks like now:

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
41
42
1	AVSValue Spline(AVSValue args, void*, IScriptEnvironment* env ) 
2	{ 
3	    float *xa, *ya, *y2a; 
4	    int n; 
5	    float x,y; 
6	    int i; 
7	    bool cubic; 
8	 
9	    AVSValue coordinates; 
10	 
11	    x = (float)(args[0].AsFloat(0)); 
12	    coordinates = args[1]; 
13	    cubic = args[2].AsBool(true); 
14	 
15	    n = coordinates.ArraySize() ; 
16	 
17	    if (n<4 || n&1) env->ThrowError("To few arguments for Spline"); 
18	 
19	    nn=n/2; 
20	 
21	    xa  = new float[n+1]; 
22	    ya  = new float[n+1]; 
23	    y2a = new float[n+1]; 
24	 
25	    for (i=1; i<=n; i++) { 
26	        xa[i] = (float)(coordinates[(i-1)*2].AsFloat(0)); 
27	        ya[i] = (float)(coordinates[(i-1)*2+1].AsFloat(0)); 
28	    } 
29	 
30	    for (i=1; i<n; i++) { 
31	        if (xa[i] >= xa[i+1]) env->ThrowError("Spline: all x values have to be different and in ascending order!"); 
32	    } 
33	     
34	    spline(xa, ya, n, y2a); 
35	    splint(xa, ya, y2a, n, x, y, cubic); 
36	 
37	    delete[] xa; 
38	    delete[] ya; 
39	    delete[] y2a; 
40	 
41	    return y; 
42	} 


Thanks for the reply though. :)
Topic archived. No new replies allowed.