0

I have written a function that calculates an area from coordinated stored in vector arrays:

void areaCalc(int corners, vector<float> x, vector<float> y, float& area)
{
	float sum=0;
	for (int i=0; i<=(corners-2); i++)
	{
		sum+=(((x[i])*(y[(i+1)]))-((x[(i+1)])*(y[i])));
	}
	sum+=(((x[(corners-1)])*(y[0]))-((x[0])*(y[(corners-1)])));
	area=0.5*sum;
}

The problem is that when I compile it I get this warning from VC++:
warning C4244: '=' : conversion from 'double' to 'float', possible loss of data

I can't figure out why this is happening as I haven't defined double as a data type anywhere in my program. This isn't a huge issue as the program still compiles and seems to run correctly but it still worries me that there is a warning.

Thanks for any help!

3
Contributors
3
Replies
4
Views
6 Years
Discussion Span
Last Post by dinners
0

It's on line 9. 0.5 is considered a double. Change line 9 to area = static_cast<float>(0.5*sum); to tell the compiler that's what you mean to do (or use a C style cast like area = (float)(0.5*sum); )

Edited by jonsca: n/a

1

Yes, the problem is in line 9, but there's a much simpler answer:

area = 0.5f*sum;

will solve the problem just fine, no static cast needed. If the constant is specified as a float, the product of a float and a float will be a float. The product of a double and a float is a double.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.