LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-06-2007, 08:38 PM   #1
vijraj
LQ Newbie
 
Registered: Oct 2006
Posts: 14

Rep: Reputation: 0
floating point number division


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;
        
}
 
Old 03-06-2007, 09:17 PM   #2
PatrickNew
Senior Member
 
Registered: Jan 2006
Location: Charleston, SC, USA
Distribution: Debian, Gentoo, Ubuntu, RHEL
Posts: 1,148
Blog Entries: 1

Rep: Reputation: 48
Floating point arithmetic inherently involves errors, so there are really two numbers you are interested in. One is how many decimal places out a floating point number can go, and the other is how many decimal places should be trusted. The first is pretty easy to find, just output 1/3 and count the decimal places. The second is rather difficult to measure, which is what you're observing with this output. Dividing by 10 should always just shift a decimal place, but it doesn't due to floating point error.

On a related note, you might get better precision by multiplying times .1. It's a weird discrete math thing, but multiplication produces less error than division.
 
Old 03-06-2007, 09:22 PM   #3
vijraj
LQ Newbie
 
Registered: Oct 2006
Posts: 14

Original Poster
Rep: Reputation: 0
Thumbs up

Thanks Patrick.But I am getting the same result.
The first time I am getting 0.1.But from next time I am not getting 0.01 but something like 0.009998.etc...
 
Old 03-06-2007, 09:25 PM   #4
PatrickNew
Senior Member
 
Registered: Jan 2006
Location: Charleston, SC, USA
Distribution: Debian, Gentoo, Ubuntu, RHEL
Posts: 1,148
Blog Entries: 1

Rep: Reputation: 48
Do you need to get a certain precision, or is this just curiosity? There are special arbitrary-precision libraries that let you calculate with... well, arbitrary precision. Unfortunately, you might never get more precise than that with standard floating point arithmetic.
 
Old 03-06-2007, 09:38 PM   #5
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
If you are just trying to find the smallest number try division by 2 rather than 10, you should find the results are more to your liking. This is because the numbers are held internally in binary.
 
Old 03-06-2007, 09:52 PM   #6
vijraj
LQ Newbie
 
Registered: Oct 2006
Posts: 14

Original Poster
Rep: Reputation: 0
Thumbs up

Hi Patrick and graemef !
Actually I have to display the number that is >0 but the smallest number in floating point representation.
So actuallyit has the sign bit set to 1 and the exponent bit set to 00000001 and the rest 23 bits are zeroes.That is the representation of the smallest number internally.So I want to know that number that can be represented in floating point arithmetic.
so I am going in a loop and dividing 1.0 by 10 to find the smallest number >0 that can be represented by float.
any suggetions?Thanks.
 
Old 03-06-2007, 10:07 PM   #7
v.tieubao
LQ Newbie
 
Registered: May 2006
Posts: 7

Rep: Reputation: 0
Quote:
Originally Posted by vijraj
Hi Patrick and graemef !
Actually I have to display the number that is >0 but the smallest number in floating point representation.
So actuallyit has the sign bit set to 1 and the exponent bit set to 00000001 and the rest 23 bits are zeroes.That is the representation of the smallest number internally.So I want to know that number that can be represented in floating point arithmetic.
so I am going in a loop and dividing 1.0 by 10 to find the smallest number >0 that can be represented by float.
any suggetions?Thanks.
Think about "Double-Precision Floating-Point Representation"

Cheers,
VTB.
 
Old 03-06-2007, 10:12 PM   #8
vijraj
LQ Newbie
 
Registered: Oct 2006
Posts: 14

Original Poster
Rep: Reputation: 0
Thumbs up

Thanks everybody!
I got it.I divided by 2 as you said and I got it.YEAHHHHHHHHHHHHHHHHHHHH!
 
Old 03-06-2007, 10:14 PM   #9
PatrickNew
Senior Member
 
Registered: Jan 2006
Location: Charleston, SC, USA
Distribution: Debian, Gentoo, Ubuntu, RHEL
Posts: 1,148
Blog Entries: 1

Rep: Reputation: 48
This can actually be solved on paper for languages, like C, that use IEEE floats. The lowest exponent that can be stored is -127 (because IEEE float exponents are the binary value stored - 127). The lowest mantissa is 0, corresponding to a value of 1.0. so, the lowest float value that can be stored is 1.0x2^-127, or 2^-127. Crappy MS calculator tells me this is 5.8774717541114375398436826861112e-39, so it just depends if you trust it.
 
Old 03-06-2007, 10:36 PM   #10
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 735

Rep: Reputation: 76
Hi.

You can compare your results with the code at http://homepages.cwi.nl/~steven/enquire.html
I've been using enquire for years; very useful whenever I get to a new hardware platform ... cheers, makyo

Last edited by makyo; 03-06-2007 at 10:46 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
floating point number vijraj Programming 2 03-07-2007 04:17 PM
floating-point in script mitsos Programming 6 08-31-2006 07:14 AM
Floating point exception hemk76 Linux - Software 1 05-06-2005 11:49 PM
floating point multiplication irfanhab Programming 0 12-23-2004 10:13 PM
managing floating point vince_2x Linux - General 2 09-21-2004 09:05 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:04 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration