LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 09-19-2011, 07:33 PM   #1
jahobjafwar
Member
 
Registered: Aug 2010
Posts: 31

Rep: Reputation: 0
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;

// Statements
printf("Enter target number:\n");
scanf("%d", &mark);

printf("Enter number of iterations:\n");
scanf("%d", &numTries);

for(int i = 0; i < numTries; i++)
{
sum += (mark - start)/2;
start += (mark - start)/2;
}

remainder = mark - sum;

printf("%.150Lf\n", sum);
printf("%.150Lf\n", remainder);

return 0;


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

gcc -W -Wall -Werror -std=c99 -g limits.c -o limits
 
Old 09-19-2011, 07:37 PM   #2
corp769
LQ Guru
 
Registered: Apr 2005
Location: /dev/null
Posts: 5,818

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
Instead of using long double, have you tried long long?
 
Old 09-19-2011, 07:58 PM   #3
jahobjafwar
Member
 
Registered: Aug 2010
Posts: 31

Original Poster
Rep: Reputation: 0
long long refers to an integer value. As far as i know long double is the largest of the real number types in C.
 
Old 09-19-2011, 08:10 PM   #4
corp769
LQ Guru
 
Registered: Apr 2005
Location: /dev/null
Posts: 5,818

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
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....
 
Old 09-19-2011, 08:13 PM   #5
jahobjafwar
Member
 
Registered: Aug 2010
Posts: 31

Original Poster
Rep: Reputation: 0
no worries. i actually did try it. i thought maby there was a long long double type but i got a compile error.
 
Old 09-19-2011, 08:15 PM   #6
corp769
LQ Guru
 
Registered: Apr 2005
Location: /dev/null
Posts: 5,818

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
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?
 
Old 09-19-2011, 08:17 PM   #7
corp769
LQ Guru
 
Registered: Apr 2005
Location: /dev/null
Posts: 5,818

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
What kind of architecture are you running currently? I know you can use long long double, but that depends on the architecture of your CPU.
 
Old 09-19-2011, 08:18 PM   #8
jahobjafwar
Member
 
Registered: Aug 2010
Posts: 31

Original Poster
Rep: Reputation: 0
i686
 
Old 09-19-2011, 08:20 PM   #9
jahobjafwar
Member
 
Registered: Aug 2010
Posts: 31

Original Poster
Rep: Reputation: 0
when you say that 64 is the max are you meaning decimal places. in my example 64 was just the max number of iterations before it rounded to 1.0
 
Old 09-19-2011, 08:22 PM   #10
corp769
LQ Guru
 
Registered: Apr 2005
Location: /dev/null
Posts: 5,818

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
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.
 
Old 09-19-2011, 08:25 PM   #11
jahobjafwar
Member
 
Registered: Aug 2010
Posts: 31

Original Poster
Rep: Reputation: 0
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
 
Old 09-19-2011, 08:39 PM   #12
corp769
LQ Guru
 
Registered: Apr 2005
Location: /dev/null
Posts: 5,818

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
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.
 
Old 09-19-2011, 08:43 PM   #13
jahobjafwar
Member
 
Registered: Aug 2010
Posts: 31

Original Poster
Rep: Reputation: 0
Awesome!!! I look forward to hearing from you again.
Once again I thank you for your help.
 
Old 09-19-2011, 08:48 PM   #14
corp769
LQ Guru
 
Registered: Apr 2005
Location: /dev/null
Posts: 5,818

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
Not a problem! Send me an email to remind me if you can!
 
Old 09-19-2011, 09:18 PM   #15
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
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.)
 
  


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
Problem with source insight gopalarao Linux - Software 1 07-02-2011 04:22 AM
Some insight on whats going wrong [Not a coding problem, but concept problem] imoracle Linux - Newbie 5 11-13-2008 11:49 PM
Accelrys - Insight II-startup problem bcramer Linux - Newbie 1 04-26-2007 01:02 PM
limits are not working (limits.conf) PkerC Red Hat 3 06-22-2006 10:14 AM
Problem with Insight II - version GLIBC_2.2 not defined in file libc.so.6... belga Linux - Software 3 08-18-2004 01:01 PM

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

All times are GMT -5. The time now is 07:46 PM.

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