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!

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); )

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.

Ah right, thanks a lot, it really had me stumped!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.