[SOLVED] Programming a limits problem in C. Need some insight please.
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.
Programming a limits problem in C. Need some insight please.
Ok so i know your going to think "why". I admit now im a total freak. So consider you want to go from 0 to 1. You take one half it then move to that point. now you are at .5. then you take half of the remainder (.25) and add that, now you are at .75.
Do you see where im going with this. Long story short you will never actually get to one. I wrote a C program to do this and calculate the answer give a limit (one in my example) and number of iterations. (Told you i was a freak) Here is the code:
#include <stdio.h>
//Function Declarations
int main()
{
// Local Declarations
int numTries;
int mark;
long double start = 0;
long double sum = 0;
long double remainder;
Ok so the code works. Notice i print out the sum and the remainder formated to 150 decimal places. If you use 1 as the mark and 1 iteration you will get .5 and 149 zeros for both sum and remainder.
However; for some reason if you go above 64 iterations, the sum will be 1.(150 zeros) and the remainder will be all zeros. I ran it with one billion iterations and got the same answer.
I know there is some kind of rounding thing going on not letting me get the level of accuracy i want. is it my formating? seems like i have plenty of space left on 64 iterations, but at 65 it breaks down.
anyone know someting i dont? Please let me know.
Thanx in advance.
i compiled it in ubuntu using gcc with the command
I know, just was wondering if you had any different output based out of that. Hard to explain why I asked that, I have a hard time explaining things sometimes....
To my knowledge, and after doing some reading to get my mind in programming mode, it seems 64 is the max, unless someone else knows if a way to extend this?
Yeah my fault, I didn't explain in full. I was thinking out loud as a matter of fact - Anyways, it IS possible to use long long double, but since you are running a 32 bit system, you wouldn't be able to keep going. You would be able to get more precision on a higher architecture.
i see. well at least i know. what architecture are you running? can you get more precision than me? if you don't mind, would you try it and see. Im just curious.
and thanx for the help
My laptop took a crap, and I'm waiting on my alienware to get done in production; It should be here by next weekend. Plus I'm at work and don't have access to linux right now - Maybe someone else can help in that department for now....... And when I do get it, I will have a 64 bit system, so when the time comes, I can definitely mess with the code and I will get it to work farther for you.
To return to your original question, what you're demonstrating is that you're using a digital computer using a FPU.
Obviously, a digital computer's hardware is limited to the (finite) set of numbers that can be represented in (some multiple of) the CPU's word size.
If you want to expend the precision of your computation there are several "arbitrary precision" libraries (try gmp) that you can use. Note, however, that you have to set the "arbitrary precision" that you want to use, and, after you reach the limit of your selected precision, the sum will be indistinguishable from the limit because the amount you're adding will be less than the smallest number that can be represented in you chosen precision. (I.e. - It's a "digital" zero.)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.