LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-15-2005, 01:39 AM   #1
pirate_pete
LQ Newbie
 
Registered: Sep 2005
Posts: 3

Rep: Reputation: 0
Converting int to float?


Hey all,

The following code is meant to convert an int to a float in C, but its not working!!! I need to use ints because of the mod (%) operation, but i need the results as a float.

{
int tempx;
int tempz;

float fx;
float fz;

/* sizex, sizez and resolution are all predetermined int values
rand initialised using srand(time(NULL));
*/
tempx = ((rand())%(sizex * resolution)) - (sizex * resolution / 2);
tempz = ((rand())%(sizez * resolution)) - (sizez * resolution / 2);
fx = tempx;
fz = tempz;
}

I've tried using:

fx = (float)tempx;
fx = *((float*)&tempx);

But nothing seems to work, i get valid tempx and tempz values, but they go crazy when converted to float.

Ie. tempx = 9865, tempz = 5806, converts to fx = 0, fz = 1086538880

Anyone able to help me?
 
Old 09-15-2005, 01:48 AM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
Have you tried this:
Code:
fx = tempx * 1.0;
fz = tempz * 1.0;
Typecasting an int to a float is surely not what you want. That tells the computer to interpret the bits in the int as a float; those two data types most assuredly do not use the same format. Though, I will say I'm surprised the original "fx = tempx;" didn't work. I could swear I've done something like that before. I'll need to reboot into Linux to double-check.
 
Old 09-15-2005, 01:49 AM   #3
fraca7
LQ Newbie
 
Registered: Jan 2004
Location: France
Distribution: Slackware
Posts: 9

Rep: Reputation: 0
fx = (float)tempx;

This should work.

fx = *((float*)&tempx);

This will produce utter garbage. The binary representations of floats and ints are not interchangeable.

Besides, the code you posted (just assigning fx = tempx) works fine for me.
 
Old 09-15-2005, 01:50 AM   #4
pirate_pete
LQ Newbie
 
Registered: Sep 2005
Posts: 3

Original Poster
Rep: Reputation: 0
Must be my compiler...
 
Old 09-15-2005, 01:54 AM   #5
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
By the way, how do you know what the values of fx and fz are? Through a debugger, or do you display them with a printf()? If printf(), make sure you're using the right conversion character. If you try to print a floating point as an int, you'll get crazy results.
 
Old 09-15-2005, 02:00 AM   #6
pirate_pete
LQ Newbie
 
Registered: Sep 2005
Posts: 3

Original Poster
Rep: Reputation: 0
Haha now thats embarrasing, yeah i was using fprintf("%d",fx);

Sorry guys, thanks for your help!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
converting a int to string irfanhab Programming 6 07-30-2005 09:40 PM
count digits of a float || convert float to string nadroj Programming 6 07-11-2005 04:52 PM
invalid types int[int] for array subscript scuzzman Programming 2 11-16-2004 09:34 PM
Converting int value to char liguorir Programming 8 05-23-2004 07:21 PM
switch statement converting struct char to struct int oceaneyes2 Programming 2 12-10-2003 04:30 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:07 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration