converting int to float gives nan (int value is definitely non-nan)

G

googlegroups11

I am modifying somebody's program. There is a function that returns an
int. I printf this and it is some value always less than 2500 -- it is
limited by this value so no overflow is happenning. Now when I assign
this value to a float i.e.,

float myfloat = this_integer;
or
float myfloat = (float) this_integer;

myfloat is always nan.

I am on 32-bit machine. As I said, I can see the int value, it is what
I expect, but when I assign it to a float it becomes nan.

Can someone give any pointers?
Thanks
 
G

googlegroups11

How do you know? It is likely that your code contains a bug, but we
can't tell without seeing the code.

I know because I printf it and it is nan (also shows itself from other
operation done on myfloat).

Code? I am editing some open source thing. My addition is nothing
more than
int myint = thefunction();
printf("%d",myint) --> I see what I expect
float myfloat = myint
printf("%f",myint) --> nan
 
M

mark.bluemel

On 19 Mar, 03:27, (e-mail address removed) wrote:
....
int myint = thefunction(); ....
printf("%f",myint) --> nan

"If you lie to the computer, it will take its revenge".
 
G

Guest

I know because I printf it and it is nan (also shows itself from other
operation done on myfloat).

Code? I am editing some open source thing. My addition is  nothing
more than
int myint = thefunction();
printf("%d",myint) --> I see what I expect
float myfloat = myint
printf("%f",myint) --> nan

I've never understood this reluctance to post code. Do you think
we'll laugh at your code or something?

Does this code have the problem you describe. If not why not?

#include <stdio.h>

int geti (void)
{
return 2499;
}

int main (void)
{
int i;
float f;
i = geti();
printf ("i is %d\n", i);
f = geti();
printf ("f is %f\n", f);
return 0;
}

--
 
D

Dik T. Winter

> I know because I printf it and it is nan (also shows itself from other
> operation done on myfloat).
>
> Code? I am editing some open source thing. My addition is nothing
> more than
> int myint = thefunction();
> printf("%d",myint) --> I see what I expect
> float myfloat = myint
> printf("%f",myint) --> nan

%f expects a double as argument, not an int, as you give here.
 
M

Martin Ambuhl

int myint = thefunction();
printf("%d",myint) --> I see what I expect
float myfloat = myint
printf("%f",myint) --> nan


As C the above would look something like

#include <stdio.h>

int thefunction(void) { return 42; }

int main(void)
{
int myint = thefunction();
float myfloat; /* moved for C90 */
printf("%d ", myint); /* added a space for minimal
sanity, at least. */
myfloat = myint;
printf("%f", myint); /* ERROR! myint is an int,
%f is the specifier for
a double (or float
promoted to a double) */
putchar('\n'): /* let's at least try
to have portably defined
behavior */
return 0;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,111
Latest member
VetaMcRae
Top