Sign in to follow this  
Dave84311

[.net] Conversion UINT' to 'FLOAT

Recommended Posts

You should be able to do regular typecasting:


// method 1
unsigned int value = 25;
float myNumber = (float)value;

// method 2
unsigned int value = 25;
float myNumber = static_cast<float>(value);


I mean if you are going from UINT to FLOAT, you are just adding on a .0000000 because an UINT is a whole number. That's what the typecasting does for you.

Quote:
Original post by Del Snd of Thndr
You might want to check out:
System.Convert.ToSingle()


Thanks for pointing that out. I don't like the renaming convention that takes place in .Net [headshake]. Couldn't find any references to float.

Share this post


Link to post
Share on other sites
I am simply getting some info from directx about a image, taking its height then the direct3d surface and calculating the x and y values for a direct3d vector, that way I can set scale for my images. I had issues with scaling and thought of fixing it this way.

&m_Scale.x = static_cast<float>(imageInfo.Width / desc.Width);
&m_Scale.y = static_cast<float>(imageInfo.Height / desc.Height);

I know about the pointer and such, but when I did it I got the error. Thats the problem it has to be a pointer and make refrences else all my objects will be the same shape. Removing the pointer makes it work but only one sprite shows.

Share this post


Link to post
Share on other sites
Ahh that sounds like a DirectX problem as for why not both sprites are showing. As for the code:

m_Scale.x = static_cast<float>(imageInfo.Width / desc.Width);
m_Scale.y = static_cast<float>(imageInfo.Height / desc.Height);

Should be what you need if imageInfo, desc, and m_Scale are not pointers. What are the variable declarations of all those variables that you use? Can you list m_Scale, m_Scale.x, m_Scale.y, imageInfo, imageInfo.Width, desc, desc.Width, imageInfo.Height, and desc.Height.

Share this post


Link to post
Share on other sites
As mentioned above:

Ones a surface, anothers a vector and anothers a imageinfo for dx.

D3DXIMAGE_INFO imageInfo;
D3DSURFACE_DESC desc;
D3DXVECTOR2 m_vScale;

The problems with others not showing is related. When I nop out that code it seems to work fine just deformed. Without it being a pointer I believe it is causing the problems.

Dancin_Fool I was getting errors when doing that. Odly...

Share this post


Link to post
Share on other sites
By the way, there is a bug in the JIT that make conversion from uint to either float or double slow as hell. Instead you have to convert first to int to speed iy up.


uint i = 256;
double t;
t = (double)i; // very slow
t = (double)(int)i; // faster

Share this post


Link to post
Share on other sites
Quote:
Original post by Lenolian
By the way, there is a bug in the JIT that make conversion from uint to either float or double slow as hell. Instead you have to convert first to int to speed iy up.


uint i = 256;
double t;
t = (double)i; // very slow
t = (double)(int)i; // faster



Won't you lose data if you're working with values large enough, though? I mean, someone probably decided to start with a uint for a reason...

At any rate, if Dave is using managed C++, he can use the functions that Drew_Benton and I suggested (will the bug rear it's head when using those functions?). If he's not using managed C++, he won't have to worry about bugs in the JIT.

Share this post


Link to post
Share on other sites

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