I've tried to do this:
But instead of 3/2 he returns 1.00000000...Code:#include <stdio.h> int main() { int a=3,b=2; float c; c = (float)(a/b); printf("%f\n",c); return 0; }
How can i convert Int to float?
Thanks!
I've tried to do this:
But instead of 3/2 he returns 1.00000000...Code:#include <stdio.h> int main() { int a=3,b=2; float c; c = (float)(a/b); printf("%f\n",c); return 0; }
How can i convert Int to float?
Thanks!
Make sure at least one of the operands is converted to float, and the expression will be evaluated properly:
What you have presently is an integer operation that's cast to float. 3/2 is 1 (note that both sides are integers), and converted to float it's 1.0.Code:c = (float)a/b;
My best code is written with the delete key.
is there any error here:
Code:xmax = (((float)x1 + ((float)wire_data[0] /2)) > xmax) ? ((float)x1 + ((float)wire_data[0] /2)) : xmax; xmin = (((float)x1 - ((float)wire_data[0] /2)) < xmin) ? ((float)x1 - ((float)wire_data[0] /2)) : xmin; ymax = (((float)y1 + ((float)wire_data[0] /2)) > ymax) ? ((float)y1 + ((float)wire_data[0] /2)) : ymax; ymin = (((float)y1 - ((float)wire_data[0] /2)) < ymin) ? ((float)y1 - ((float)wire_data[0] /2)) : ymin;
If wire_data[0] is an int, and you want the result of the divide by 2 to be a float, you could save a few keystrokes and simply:
In this case, the 2.0 is a floating point value (a double), but you could also codeCode:wire_data[0] /2.0
if you really wanted a float and not a double.Code:wire_data[0] /2.0f
Todd
Mainframe assembler programmer by trade. C coder when I can.
Hey guys, sorry to bring back an old thread but I'd rather do this than start a new one!
I'm basically having trouble converting an int to a float. I've tried the following code and ever single time floatadd = 1077280768
Code:floatadd = newadd;Code:floatadd = (double)newadd;I.e I've tried every option I've seen listed around here. Newadd is 22 and I need to convert it to a floating point to do some jazz on it, but all the above give 1077280768 as an answer. Any ideas?Code:floatadd = double(newadd);
I'm sorry if this is a noobie question, I am very new to C programming.
Thanks,
pgcrooks
"No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
"Have you tried turning it off and on again?" - The IT Crowd
Thanks Pete, I realise float and double are different, I just copied and pasted the same thing 3 times above. Found the problem, turns out I'm a complete idiot and used %d in the printf command. Oh well... Stared at the convert line for ages without ever looking at the printf. Sorry!
1. This probably SHOULD be a new thread.
2. You will need to give us a bit more code. One very likely scenario is that you are outputting a floating point number as an integer. The 1077280768 value when it turns to hex is 0x40360000, which looks suspiciously like a floating point number to me [yes, I know, I'm a geek by being able to "spot" floating point numbers in hex, but so be it].
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.