C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-22-2009, 08:08 AM   #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;    
}
This is just a part from some program, but the problem is here.
If someone could run it with x = 0.186, my question will be obvious
Thank you.
Junky is offline   Reply With Quote
Old 11-22-2009, 08:21 AM   #2
+++ OK NO CARRIER
 
quzah's Avatar
 
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   Reply With Quote
Old 11-22-2009, 08:22 AM   #3
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 11,340
Quote:
Originally Posted by Junky
If someone could run it with x = 0.186, my question will be obvious
Well, your program cannot even compile, though that is easily fixed
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   Reply With Quote
Old 11-22-2009, 08:24 AM   #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   Reply With Quote
Old 11-22-2009, 08:27 AM   #5
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 11,340
Quote:
Originally Posted by Junky
I have x = 0.168, and I multiply it with 1000 (x *= 1000), and I get x = 186, but int(x) = 185.
That is because of floating point inaccuracy: although you entered 0.186, the actual value stored is a little less than that.
__________________
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   Reply With Quote
Old 11-22-2009, 08:29 AM   #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   Reply With Quote
Old 11-22-2009, 09:04 AM   #7
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 11,340
Quote:
Originally Posted by Junky
I just saw that if I put x as float instead of double (and scanf("%lg",&x) -> scanf("%f",&x)) it works fine.
For this particular value; maybe it does not work on others.

Quote:
Originally Posted by Junky
But doesn't double have more precision than normal float?
It is about accuracy, not precision. As long as you cannot accurately represent a given value, it means that you only have an approximation, thus different approximations can give you different results. Perhaps what you are observing is a case where with more precision, a closer approximation was used, but this approximation is less than the actual value, whereas with less precision, the approximation is greater than the actual value.
__________________
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   Reply With Quote
Old 11-29-2009, 08:26 AM   #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   Reply With Quote
Old 11-29-2009, 11:04 AM   #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   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump


All times are GMT -6. The time now is 02:13 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22