![]() |
| | #1 |
| Registered User Join Date: Nov 2009
Posts: 14
| Help Code: #include <stdio.h>
#include <stdlib.h>
int main(void) {
double x; int i;
scanf("%lg",&x);
for (i = 1; i <= 4; i++) {
x *= 10;
printf("x = %lg\n",x);
printf("int(x) = %d\n",int(x));
}
system("pause");
return 0;
}
If someone could run it with x = 0.186, my question will be obvious ![]() Thank you. |
| Junky is offline | |
| | #2 |
| +++ OK NO CARRIER Join Date: Oct 2001
Posts: 10,640
| Why don't you just ask us your question instead of hoping someone comes along and compiles your code, runs it with the example input, and guesses as to what you think it's supposed to do? Quzah.
__________________ Hundreds of thousands of dipshits can't be wrong. Are you up for the suck? |
| quzah is offline | |
| | #3 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 11,340
| Quote:
![]() I suggest that you explicitly state your question anyway.
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way | |
| laserlight is online now | |
| | #4 |
| Registered User Join Date: Nov 2009
Posts: 14
| Ok, sorry, i didn't mean to be rude. I have x = 0.168, and I multiply it with 1000 (x *= 1000), and I get x = 186, but int(x) = 185. But it compiles to me. I don't understand... Last edited by Junky; 11-22-2009 at 08:27 AM. |
| Junky is offline | |
| | #5 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 11,340
| Quote:
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way | |
| laserlight is online now | |
| | #6 |
| Registered User Join Date: Nov 2009
Posts: 14
| I just saw that if I put x as float instead of double (and scanf("%lg",&x) -> scanf("%f",&x)) it works fine. But doesn't double have more precision than normal float? Tnx for the explanation. Last edited by Junky; 11-22-2009 at 08:45 AM. |
| Junky is offline | |
| | #7 | ||
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 11,340
| Quote:
Quote:
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way | ||
| laserlight is online now | |
| | #8 |
| Registered User Join Date: Nov 2009
Posts: 14
| I mostly understood what you said, tnx for explaining ![]() Do you know if perhaps there is a way to do this correct? Is there a way to get a correct number with digits "abcd" from "0.abcd" (a way to bypass that error of not enough accuracy)? |
| Junky is offline | |
| | #9 |
| Registered User Join Date: Sep 2006
Posts: 3,151
| yes, of course. You use two integers to represent the two halves of a float or double number. Then you do your computations, on each int, as you want. Now every integer value can be accurately represented. |
| Adak is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|