# Floating point question

## Recommended Posts

jrmiller84    118
Hey everyone, I'm having some trouble with a small bit of code. I'm sure I'm probebly doing something stuipd, I just don't see it yet so maybe you guys can help me see the light. I am developing a side scroller and I am developing the animation of the main character right now. I am having trouble with dividing floating point numbers. I am getting a crazy negative value that I shouldnt be getting. Here's the code:
bool CPrimitive::SetTexture(const char *TexturePath)
{
D3DSURFACE_DESC Dimen;

if (FAILED(D3DXCreateTextureFromFileEx (myD3DDevice, TexturePath, 0, 0, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, D3DX_FILTER_NONE, D3DX_DEFAULT, NULL, NULL, NULL, &myTexture))) { return false; }

myTexture->GetLevelDesc(0, &Dimen);

Texture_DPC = (float)Dimen.Width / Texture_Cols;
Texture_DPR = (float)Dimen.Height / Texture_Rows;

return true;
}

What I am trying to do is get the dimensions of the texture and divide it by the number of colums and rows in the image (The images on the texture are divided into rows and columns to make it easy to reference each image on the texture) as to get the size of each "cell," or individual image within the texture. When I do the division I am getting a value of -4.31602e+008. This should not be the case. I was using a large image as a texture (I know, not a good idea) that was 1024 x 1024 and the texture had 14 colums and 14 rows (196 total sprites if you will). The math should work out to be around 73.0f for both, but I get that outrageous number. I've checked debug as well as my variable types and can't find a solution. I hope this makes sense and thank you ahead of time!

##### Share on other sites
Shannon Barber    1681
It's not clear where the trouble is,

try
Texture_DPC = (float)Dimen.Width / (float)Texture_Cols;

##### Share on other sites
JohnBolton    1372
Please tell us what the values of Dimen.Width, Dimen.Height, Texture_Cols, and Texture_Rows are. My guess is that some of them are uninitialized.

##### Share on other sites
jrmiller84    118
Sorry about that, I had a feeling it was kind of confusing. Texture_DPC is the width in pixels of each cell and Texture_DPRis the height in pixels of each cell. They should technically always be the same, but for safety I did them both. Dimen.width and Dimen.height hold the dimensions of the texture (1024x1024 in this case). Basically my only problem is that these are not dividing correctly. I am getting an outrageous number (-4.31602e+008) where I should be getting something a little more modest, in the case of my current dimensions(a 1024x1024 texture with 14 rows by 14 columns) it should be roughly 73.0f pixels per width and height of each "cell." Im not sure why dividing 1024.0f by 14.0f is giving me that large number. All the variables have been initialized. It seems like something isnt getting set correctly and it's jumping to that large number. The reason I ask is because it's causing the textures to not be applied correctly when i get this outrageous number for my texture coordinate. Oh, and it is happening in both equations of Texture_DPC and Texture_DPR.

##### Share on other sites
JohnBolton    1372
Your description is clear, but there is nothing in the code that you posted that is obviously wrong (except that Texture_Cols and Texture_Rows are not defined and initialized).

If you step through the code in the debugger, and look at the values involved in the operations, you may discover the source of the problem. Since the result of the division in not 73 as you expect, it is likely that one of the operands has the wrong value. That's why I asked what their values are. Once you find out which operands are wrong you can look for the reason that they are wrong.

Perhaps, D3DXCreateTextureFromFileEx is failing so the values of Texture_DPC and Texture_DPR are never getting set and are just garbage.

##### Share on other sites
jrmiller84    118
I seem to have figured it out. I took the values of Texture_DPC and Texture_DPR and set them to 0.0f in the initialization of the class and for some reason that seemed to clear it up. Thanks a lot guys, you rock.

##### Share on other sites
fearyourself    122
If you really only did set Texture_DPC and Texture_DPR to 0.0 in the initialization and that fixed your problem, I'd be careful...

There is no reason that would settle your problem (at least directly)... You're probably experiencing side-effects...

I'd agree with the others, debug or just print out the four values

Dimen.Width , Texture_Cols , Dimen.Height , Texture_Rows

before the calculation, I'd also bet (like the others) that some of the values aren't initialized...

Also are both Texture_DPR and Texture_DPC at that value or just one of the two?

If you don't take care of this bug before continuing, it'll probably come back later on when you have totally forgotten it...

## Create an account

Register a new account