The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
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

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 03-06-2007
Registered User
 

Join Date: Nov 2006
Posts: 23
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
Reply With Quote
Forum Sponsor
  #2  
Old 03-06-2007
Technorati Master
 

Join Date: Mar 2005
Location: Large scale systems...
Posts: 2,609
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;
}
Reply With Quote
  #3  
Old 03-07-2007
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 4,269
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.
Reply With Quote
  #4  
Old 03-07-2007
Technorati Master
 

Join Date: Mar 2005
Location: Large scale systems...
Posts: 2,609
this might interest you,


check this out
Reply With Quote
  #5  
Old 03-08-2007
Hitori's Avatar
Registered User
 

Join Date: Jun 2006
Posts: 360
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.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 04:52 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0