ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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;
}
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.
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...
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.
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.
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.
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"
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.
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.