Sign in to follow this  

truncation of constant value

This topic is 4729 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm not sure why this is happening but it seems that any constant value over 127 can't be passed to a char parameter without giving me a truncation error. ex:
	cMaterial::cMaterial()
	{
		// Set a default material (white)
		ZeroMemory(&m_material, sizeof(D3DMATERIAL9));
		SetDiffuseColor(255,255,255); // Line 3 truncation errors
	}

	bool cMaterial::SetDiffuseColor(char Red, char Green, char Blue)
	{
		m_material.Diffuse.r = 1.0f / 255.0f * (float)Red;
		m_material.Diffuse.g = 1.0f / 255.0f * (float)Green;
		m_material.Diffuse.b = 1.0f / 255.0f * (float)Blue;

		return true;
	}


error: c:\Documents and Settings\Dave\My Documents\Lyradis\include\cGraphics.cpp(639) : warning C4309: 'argument' : truncation of constant value c:\Documents and Settings\Dave\My Documents\Lyradis\include\cGraphics.cpp(639) : warning C4309: 'argument' : truncation of constant value c:\Documents and Settings\Dave\My Documents\Lyradis\include\cGraphics.cpp(639) : warning C4309: 'argument' : truncation of constant value Any ideas why it's doing this?

Share this post


Link to post
Share on other sites
the problem is there are too few bits in a char (1 for sign, and 7 for the number). You can use *unsigned* chars which will let you go from 0 to 255.


bool cMaterial::SetDiffuseColor(unsigned char Red, unsigned char Green, unsigned char Blue)

Share this post


Link to post
Share on other sites
It might be thinking char is signed, which would mean its range is from -128 to 127. unsigned char should work as expected.

@Onemind: Negative numbers are two's complement, not sign + number.

Share this post


Link to post
Share on other sites
It'd probably be safer and faster to use ints and perform the truncation yourself.

See, int is by convention defined as the natural size of a "word" on a given arcitecture. I know technically its just char <= short <= int <= long, but the general convention is int is 32 bits signed. Anyways, when you work in ints, everything speeds up by a bit, since the code doesn't have to go truncating everything all over the place.

Share this post


Link to post
Share on other sites

This topic is 4729 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this