floating point problem


 
Thread Tools Search this Thread
Top Forums Programming floating point problem
# 1  
Old 03-06-2007
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 03:46 AM.. Reason: Add code tags and disable smilies for readability
# 2  
Old 03-07-2007
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  
Old 03-07-2007
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  
Old 03-07-2007
this might interest you,


check this out
# 5  
Old 03-08-2007
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Floating Point

Anyone help me i cant found the error of floating point if needed, i added the code complete #include <stdio.h> #include <string.h> #include <stdlib.h> #include <ctype.h> typedef struct { int hh; int mm; int ss; char nom; int punt; }cancion; typedef struct... (9 Replies)
Discussion started by: Slasho
9 Replies

2. Shell Programming and Scripting

problem with floating point number loops

Hey, I guess I am just to stupid and am not seeing the "wood for the trees", but I am always getting strange errors. I want to create a mesh with coordinates like: x y z 3.1 3.0 0.75 0 0 1 3.1 2.9 0.75 0 0 1 3.1 2.8 0.75 0 0 1 3.1 2.7 0.75 0 0 1 3.0 ... (10 Replies)
Discussion started by: ergy1983
10 Replies

3. Shell Programming and Scripting

floating point number problem

Hello folks I Hope everyone is fine. I am calculating number of bytes calculation from apache web log. awk '{ sum += $10 } END { print sum }' /var/httpd/log/mydomain.log 7.45557e+09 it show above number, what should i do it sow number like 7455, i mean if after decimal point above 5 it... (5 Replies)
Discussion started by: learnbash
5 Replies

4. Shell Programming and Scripting

floating point numbers in if

# if > then > echo "1" > else > echo "2" > fi -bash: How can i compare floating point numbers inside statement? (15 Replies)
Discussion started by: proactiveaditya
15 Replies

5. Shell Programming and Scripting

how to compare 2 floating point no.

Hi, Could any one tell me how to compare to floating point no. using test command. As -eq option works on only intergers. i=5.4 if then echo "equal" else echo "not equal" fi here output will be equal even though no. are unequal. Thanks, ravi (1 Reply)
Discussion started by: useless79
1 Replies

6. Programming

Floating point Emulator

what is floating point emulator(FPE)? where and why it is used? (1 Reply)
Discussion started by: pgmfourms
1 Replies

7. Linux

Floating Point Exception

Hi, I am compiling "HelloWorld" C progam on 32-bit CentOS and i want to execute it on 64-bit CentOS architecture. For that i copied the a.out file from 32-bit to 64-bit machine, but while executing a.out file on 64bit machine I am getting "Floating point exception error". But we can run... (3 Replies)
Discussion started by: Mandar123
3 Replies

8. Linux

Floating point exception !!!

Hi, I have linux fedora 4 ver., 2.6 kernal. And qmail & mysql & samba servers are already configured on this server. When I try to install any package like squidguard ,dansguardian,webmin,rsnapshots with command rpm -ivh . It is giving error as “Floating point exception" Snap View is... (3 Replies)
Discussion started by: ssk01
3 Replies

9. Shell Programming and Scripting

floating point addition

hi, :) I have a file like this 10.456 123.567 456.876 234.987 ........ ....... What i want to do is ia have to add all those numbers and put the result in some other file. Any help pls. cheers RRK (8 Replies)
Discussion started by: ravi raj kumar
8 Replies

10. Shell Programming and Scripting

problem with floating point numbers in awk

hi all, i have the following problem using awk in a script i want to read the values from a column with real numbers and calculate the mean.the problem is that when i use a statement such as this num = $4 i cant find a way to convert the variable from string to floating point to perform... (7 Replies)
Discussion started by: kanagias
7 Replies
Login or Register to Ask a Question