Double to int errors

The Partridge Family were neither partridges nor a family. Discuss.
User avatar
Asimov
Posts: 814
Joined: May 19th, 2012, 11:38 pm

Double to int errors

Post by Asimov » July 5th, 2012, 1:20 am

Hi all,

I am getting a lot of errors. I know why I am getting the error. It is because in my missile class I am using doubles to get more accurate angles as Lux suggested, but when I call putpixel or any of gfx functions they are all integers. So I set about changing them all to doubles to get rid of all my errors LOL. The more I changed the more I found. Then I found out that even the mouse coordinates are integers. Do you think I should not bother and just live with the errors when I compile, or do you think I should do something about them? The program works fine as they are.


1>..\Assets\D3DGraphics.cpp(203): warning C4244: 'initializing' : conversion from 'double' to 'int', possible loss of data
1>..\Assets\D3DGraphics.cpp(213): warning C4244: 'initializing' : conversion from 'double' to 'int', possible loss of data
1>..\Assets\D3DGraphics.cpp(216): warning C4244: '=' : conversion from 'double' to 'int', possible loss of data
1>..\Assets\D3DGraphics.cpp(221): warning C4244: 'initializing' : conversion from 'double' to 'float', possible loss of data
1>..\Assets\D3DGraphics.cpp(222): warning C4244: 'initializing' : conversion from 'double' to 'int', possible loss o

Asimov
----> Asimov
"You know no matter how much I think I have learnt. I always end up hitting brick walls"
http://www.asimoventerprises.co.uk

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: Double to int errors

Post by chili » July 5th, 2012, 2:03 am

They aren't errors brah, they're warnings. Do not change your ints to doubles just to lose the warnings. Change ints to doubles because you need doubles! I don't even see why you're using doubles. Why not use floats?

Anyways, when you need to convert from a float to an int, just do this:
myInt = (int)myFloat;
Chili

User avatar
Asimov
Posts: 814
Joined: May 19th, 2012, 11:38 pm

Re: Double to int errors

Post by Asimov » July 5th, 2012, 2:32 am

Hi Chilli,

Well Lux helped me out with my velocity code, because my maths were not strong enough.
In the example he gave me he used doubles. I have no idea if that was right or wrong, I just followed advice.

The doubles are set up in the header and here is the code that has the velocity in.
I am not sure why doubles were used, but say I used floats instead. Surely the compiler will give me errors about convertiing floats to int so I would be no better off, or am I wrong?

Obviously all the routines in the gfx class all use integers.

I obviously still have a lot of grey areas to iron out in my Brain LOL.

Code: Select all

if (Fired)
	{	
		
		double FireRad = atan2((TargetY - MY), (TargetX - MX));	// Calculates angle between mouse and the turret

		if (FireRad < 0) {FireRad = abs(FireRad);}			// This next part simpy fixes the angle,
		else {FireRad = (M_PI * 2) - FireRad;}				// so it wont give strange numbers,
		FireRad = (2 * M_PI) - FireRad;						// not sure if needed in this case

		x = x + Velocity * cos(FireRad);	// Calculates new location
		y = y + Velocity * sin(FireRad);

		double Distance = sqrt((double)( (x-TargetX)*(x-TargetX) + (y-TargetY)*(y-TargetY) )); // Collision check
	
		if (Distance <= Velocity)
		{
			Explode = true;
		//	Fired=false;
			x=MX;
			y=MY;
		}

		
	}
Asimov
----> Asimov
"You know no matter how much I think I have learnt. I always end up hitting brick walls"
http://www.asimoventerprises.co.uk

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: Double to int errors

Post by chili » July 5th, 2012, 2:50 am

Asimov wrote:Surely the compiler will give me errors about convertiing floats to int so I would be no better off, or am I wrong?
You're wrong.

A) Warnings, not errors. Warnings.
B) If you use explicit casting like I showed above, you will not get flagged with warnings.

About doubles and floats: it's not wrong to use doubles, just slower. The trig functions only work with double so I think that is why LuX chose them in this case.
Chili

User avatar
Asimov
Posts: 814
Joined: May 19th, 2012, 11:38 pm

Re: Double to int errors

Post by Asimov » July 5th, 2012, 2:54 am

Hi Chilli,

So I would use the casting just before calling Putpixel, drawdisc etc.

Will give that a go in a bit. Trying to get my Base Class to work heh heh. That will store all the information about my bases. Whether they exist and stuff. Lots of fun to be had.

Asimov
----> Asimov
"You know no matter how much I think I have learnt. I always end up hitting brick walls"
http://www.asimoventerprises.co.uk

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: Double to int errors

Post by chili » July 5th, 2012, 3:06 am

Yup, that's the idea. Good luck brah.
Chili

User avatar
LuX
Posts: 1492
Joined: April 22nd, 2012, 12:33 pm
Location: Finland

Re: Double to int errors

Post by LuX » July 5th, 2012, 1:17 pm

Doubles, because what chili said. But yeah, that made me used to doubles so I rarely use floats anymore, but I guess I should if they are faster.

To get rid of the warnings in your put pixel for example convert the doubles like this:
gfx.PutPixel((int) x, (int) y, r, g, b); // No conversion warnings, even if x and y are doubles

What the warnings mean is, since put pixel cant use a double 30.495869, it will convert it to 30, so the compiler tells you the new value might have lost some info in the conversion, but if you explicitly tell you are aware of the conversion, you wont get warning. Another tip would be to change the warning settings, so it wont inform you of this.
ʕ •ᴥ•ʔ

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: Double to int errors

Post by chili » July 5th, 2012, 1:55 pm

In many algorithms, memory access speed becomes the bottleneck. Doubles take up twice as much memory as floats so it stands to reason that in such algorithms, moving from double to float can give you as much as 200% of the double speed.
Chili

User avatar
Asimov
Posts: 814
Joined: May 19th, 2012, 11:38 pm

Re: Double to int errors

Post by Asimov » July 5th, 2012, 2:05 pm

Hi Lux & Chilli,

Thankyou for both suggestions. I have just got my bases to disappear when they are hit by missiles. Now I gotta do an explosion animation to make it look good.

Not sure whether to use my stock explosion footage or make my own in fumefx yet LOL.

Asimov
----> Asimov
"You know no matter how much I think I have learnt. I always end up hitting brick walls"
http://www.asimoventerprises.co.uk

User avatar
LuX
Posts: 1492
Joined: April 22nd, 2012, 12:33 pm
Location: Finland

Re: Double to int errors

Post by LuX » July 5th, 2012, 2:06 pm

Aw damn it. I'll have to convert all my games to this boost.

But thanks for informing. I'm trying to figure out ways to make the games as light as possible. With my current knowledge, that is.

Btw, I made this image to D3DCOLOR converter, so I could save an image in a file as strings of D3DCOLORs like "0xFF544437" would be one pixel. But why is this converted file 1.44 Mb, while the original image is just about 404 Kb?

Is there some other way to save the file, as hex or something? Or what is the reason for this huge size difference?

All I have in the converted file is height, width and list of pixels.

I also saw in your sped up alpha project you used _aligned_malloc, is it better in some way? I see you set it to 16 bits, or something, like hex?
ʕ •ᴥ•ʔ

Post Reply