![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Floating point error in C | Hara | High Level Programming | 2 | 06-18-2008 02:43 AM |
| Floating point exception !!! | ssk01 | Linux | 2 | 05-14-2008 02:58 AM |
| floating point addition | ravi raj kumar | Shell Programming and Scripting | 8 | 12-21-2006 11:47 PM |
| problem with floating point numbers in awk | kanagias | Shell Programming and Scripting | 7 | 06-24-2005 12:14 PM |
| Floating Point Division | gsatch | Shell Programming and Scripting | 1 | 07-25-2002 01:03 PM |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
#1
|
|||
|
|||
|
floating point problem
Hi all!
Hi all! I am working with a problem to find the smallest floating point number that can be represented. I am going in a loop ,stating with an initial value of 1.0 and then diving it by 10 each time thru the loop. So the first time I am getting o.1 which I wanted.But from the next iteration I am getting 0.0099998.But this is not I want. I want a result like this.First time I am 0.1. Then next iteration should give me 0.01. Next iteration should give me 0.001 and so on. I don't know how to achieve this ..I am posting a piece of code here for u to look. Please suggest.Thanks in advance. Code:
for(;;)
{
i=i*10;
small=(1.0)/i; /*here I am getting 0.0099998 on the second iteration. I need 0.01 on 2nd iteration
and 3rd iteration should be 0.001etc.*/
ip=(int*)&small;
sprintf(sztemp,"%08x",*ip);
if(strcmp(sztemp,szSmall)==0) break;
}
Last edited by Perderabo; 03-07-2007 at 12:46 AM. Reason: Add code tags and disable smilies for readability |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
Code:
#include <stdio.h>
int main()
{
int i;
float f=1.0;
for( i=0; i<9; i++, f /= 10 ) {
printf("%10.8f\n", f);
}
return 0;
}
|
|
#3
|
|||
|
|||
|
Two things:
floating point numbers do not always represent a given number exactly - which you are encountering - the 0.0099998 result from division. limits.h defines the limit of precision for each datatype, and the smallest number that can be represented. Implementations vary. FLT_DIG - the number of significant digits in a float is defined to be at least 6 for POSIX, FLT_MIN <= 10^-38. |
|
#4
|
|||
|
|||
|
#5
|
||||
|
||||
|
In memory floating point numbers (and other internal numbers excluding special cases like BCD) are stored in binary format (e.g. in Intel reverse order for integers on PC platforms), so 0.1, 0.01, ... cannot be represented exactly, only division by 2^x (not by 10 or any other number) can give you an exact number.
|
||||
| Google The UNIX and Linux Forums |
| Thread Tools | |
| Display Modes | |
|
|