OK. LETS START REAL PROGRAMMING IN C FOR PROBLEMS!!!

8

88888 Dihedral

See my blog article http://pitcher.digitalfreehold.ca/code/natlog
"This program computes the value of e to 5,000 decimal places, and
was inspired by
the article "The Impossible Dream: Computing e to 116,000 places
with a Personal
Computer" by Stephen Wozniak, published in Byte Magazine Vol 6,
Issue 6 (June 1981,
pp392)."

# This function computes the Euler's number by the long integer in Python.
#
# formual e= 1+1/1!+1/2!+ 1/3!+ 1/4!+... 1/n!
# = ( ( 1/n +1)/(n-1) +1) ......)/2 +1)/1 +1
#

def EulerNum(digits=60, fterms=15):
x1=10L**digits
x2=x1
for n in xrange(fterms,1,-1):
x1=x1/n
x1=x1+x2
print x1

return x1+x2

#Easy in C if the division could be done efficiently

I post my way!
 
S

Stefan Ram

Ben Bacarisse said:
The location of you reply suggests a connection between the article and
the series directly above it but I can't see a direct connection. The
last thing this thread needs is more confusion between e and gamma!

char const * ec( unsigned const n /* 0 <= n <= 30 */ )
{ static char result[ 33 ];
strncpy( result, "2.718281828459045235360287471352", n + 2 );
result[ n + 2 ]= 0;
return result; }

(Not thread save. Not locale aware.).
 
B

Ben Bacarisse

Ben Bacarisse said:
The location of you reply suggests a connection between the article and
the series directly above it but I can't see a direct connection. The
last thing this thread needs is more confusion between e and gamma!

char const * ec( unsigned const n /* 0 <= n <= 30 */ )
{ static char result[ 33 ];
strncpy( result, "2.718281828459045235360287471352", n + 2 );
result[ n + 2 ]= 0;
return result; }

Good one! You reply to a post about the potential for confusion between
the posted series and e by posting a function to compute e.

Since this seems to be a solution to the OP, could you not have replied
to that message?

(Also, I think it's good to mark where you cut text out. Better still
is to cut anything not needed for context. You cut all of Lew's text,
to which you code *does* relate, but you left mine and Jacob's to which it
does not.)
 
N

Nick Keighley

If you're actually interested in particating here, then please
(a) format your text in lines shorter than 80 columns, preferably
around 72 and (b) quote some context from the article to which you're
replying (see 99% of the followups here for examples; Google Groups
can make this difficult, but the "old" interface isn't as bad as
the new one).

in what way does google groups make proper quoting difficult?
 
K

Kleuskes & Moos

It's evolved almost quite far enough, and perhaps too far in some
directions. I think that's one of its beauties, it's still recognisable
as the language I first saw in the 80s.

I was reading about a <ahem> very closely related language recently and
was delighted to see that a feature that was standardized after I last
seriously used the full language, about 10 years ago, is now deprecated.
The "designers", and I use the term very loosely - they're clearly
bodgers who can't think things through thoroughly enough, of that
language should be taken outside and whipped. And their language derided
for the mess that it is.

I think i can guess which language...
Oh - do you not think that a 15-line long .sig is a bit excessive
attached to a post with only two lines of original text?

Provided for your amusement only. The argument of conserving bandwidth
may have been valid 20 years ago, nowadays it's hardly valid.

-------------------------------------------------------------------------------
______________________________
< Inside, I'm already SOBBING! >
------------------------------
\
\
___
{~._.~}
( Y )
()~*~()
(_)-(_)
-------------------------------------------------------------------------------
 
8

88888 Dihedral

[email protected] (Stefan Ram) said:
Ben Bacarisse said:
Le 20/11/11 20:12, 88888 Dihedral a écrit :
PROBLEM 1: COMPUTE THE EULER'S NUMBER UP TO 30 DIGITS AFTER 1.
THE DIGIT REQUIRED SHOULD BE PARAMETER BY THE CALLER!
Euler's constant is the limit of the sum when N goes to infinity of:
1 + 1/2 + 1/3 + 1/4 + ... 1/N - ln(n)
The location of you reply suggests a connection between the article and
the series directly above it but I can't see a direct connection. The
last thing this thread needs is more confusion between e and gamma!

char const * ec( unsigned const n /* 0 <= n <= 30 */ )
{ static char result[ 33 ];
strncpy( result, "2.718281828459045235360287471352", n + 2 );
result[ n + 2 ]= 0;
return result; }

Good one! You reply to a post about the potential for confusion between
the posted series and e by posting a function to compute e.

Since this seems to be a solution to the OP, could you not have replied
to that message?

(Also, I think it's good to mark where you cut text out. Better still
is to cut anything not needed for context. You cut all of Lew's text,
to which you code *does* relate, but you left mine and Jacob's to which it
does not.)

Oh, this is just one way to compute e. Of course there is some other way.
 
K

Keith Thompson

Nick Keighley said:
in what way does google groups make proper quoting difficult?

They've introduced a new user interface that's much worse than the
older one, at least for purposes of posting to Usenet. I don't
remember the exact details. Last time I checked, you don't get the
new interface unless you request it, and you can still go back to
the old one.
 
K

Keith Thompson

Kleuskes & Moos said:
Provided for your amusement only. The argument of conserving bandwidth
may have been valid 20 years ago, nowadays it's hardly valid.

Bandwidth isn't the only issue. It takes up space on everyone's screen
that could be used to show more useful information. (And frankly they
aren't all that amusing.)

At least use a proper signature delimiter (a line consisting of "-- ").
 
N

Nick Keighley

They've introduced a new user interface that's much worse than the
older one, at least for purposes of posting to Usenet.  I don't
remember the exact details.  Last time I checked, you don't get the
new interface unless you request it, and you can still go back to
the old one.

ah. I think I looked at it, failed to find anything compelling and
stayed with the old interface.
 
K

Kleuske

Bandwidth isn't the only issue. It takes up space on everyone's screen
that could be used to show more useful information. (And frankly they
aren't all that amusing.)

At least use a proper signature delimiter (a line consisting of "-- ").

Aye, aye. Sir!
 
M

Markus Wichmann

above loop end with exaustation of memory
without return one result

That's why I said that filling out the blanks is left as an exercise to
the reader. Plus, where does that loop exhaust memory? It will never
terminate, but mainly because I neglected to do everything for the OP.
That's why there are no types in there, too.

At some point, someone should terminate the execution. At that point, e
should be pretty close to, well, e.

Ciao,
Markus
 
L

Lew Pitcher

yes but i'm a little ot because it is C++...

So, let's try something /on-topic/, then, shall we?

/*
** This program was inspired by the article "The Impossible Dream:
** Computing e to 116,000 places with a Personal Computer" by
** Stephen Wozniak, published in Byte Magazine Vol 6, Issue 6
** (June 1981, pp392).
**
** Essentially, the following program implements a partial expansion
** of the equation: e = 1 + (1/1!) + (1/2!) + (1/3!) + ... + (1/n!)
** by inverting the equation, such that it reads...
** e = (((((...(((((1/n) + 1) / (n-1)) + 1) ... / 3) + 1) / 2) + 1) /
1) + 1
**
** I originally wrote this program in Z80 Assembler code, to run under
** CP/M (Dec 1986), and subsequently have recoded the program in
** 370 Assembler to run under MVS and VSE, and C to run under MSDOS
** and Linux
**
** Lew Pitcher, 1999
*/
#include <stdio.h>

#define I 1 /* number of bytes in integer portion */
#define X 2078 /* number of bytes in fraction portion */
#define N 1776 /* number of divide iterations */
#define P 5000 /* number of multiply iterations */

char e[I+X]; /* holds fixed point binary value of e */

int main(void)
{
void divide(char *, unsigned, unsigned),
multiply(char *, unsigned, unsigned);

unsigned n, /* divisor for calculation loop */
p; /* precision for print loop */

e[0] = 1; /* start the series at 1.anything */
for (n = N; n > 0; --n)
{
divide(e,n,I+X);
e[0] += 1;
}

fputs(" e == ",stdout); fputc(e[0]+'0',stdout); fputc('.',stdout);
for (p = 0; p < P; ++p)
{
e[0] = 0;
multiply(e,10,I+X);
fputc(e[0]+'0',stdout);
if (p%5 == 4)
if (p%50 == 49)
fputs("\n\t",stdout);
else
fputc(' ',stdout);
}
fputc('\n',stdout);

return 0;
}

void divide(char *dvd, unsigned dvr, unsigned prc)
{
char *Wk_q;
unsigned bit, rmdr;

rmdr = 0;
for (Wk_q = dvd; Wk_q < dvd+prc; ++Wk_q)
{
for(bit = 0; bit < 8; ++bit)
{
rmdr *= 2;
if (*Wk_q & 0200) rmdr += 1;
*Wk_q <<= 1;
if (rmdr >= dvr)
{
*Wk_q += 1;
rmdr -= dvr;
}
}
}
}

void multiply(char *mcd, unsigned mpy, unsigned prc)
{
char *Wk_r;
unsigned bit, ovflo;

ovflo = 0;
for (Wk_r = mcd+prc-1; Wk_r >= mcd; --Wk_r)
{
for (bit = 8; bit > 0; --bit)
{
if (*Wk_r & 1) ovflo += mpy;
*Wk_r >>= 1; *Wk_r &= 0177;
if (ovflo & 1) *Wk_r = *Wk_r | 0200;
ovflo /= 2;
}
}
}

=========================== output ============================
e == 2.71828 18284 59045 23536 02874 71352 66249 77572 47093 69995
95749 66967 62772 40766 30353 54759 45713 82178 52516 64274
27466 39193 20030 59921 81741 35966 29043 57290 03342 95260
59563 07381 32328 62794 34907 63233 82988 07531 95251 01901
15738 34187 93070 21540 89149 93488 41675 09244 76146 06680
82264 80016 84774 11853 74234 54424 37107 53907 77449 92069
55170 27618 38606 26133 13845 83000 75204 49338 26560 29760
67371 13200 70932 87091 27443 74704 72306 96977 20931 01416
92836 81902 55151 08657 46377 21112 52389 78442 50569 53696
77078 54499 69967 94686 44549 05987 93163 68892 30098 79312
77361 78215 42499 92295 76351 48220 82698 95193 66803 31825
28869 39849 64651 05820 93923 98294 88793 32036 25094 43117
30123 81970 68416 14039 70198 37679 32068 32823 76464 80429
53118 02328 78250 98194 55815 30175 67173 61332 06981 12509
96181 88159 30416 90351 59888 85193 45807 27386 67385 89422
87922 84998 92086 80582 57492 79610 48419 84443 63463 24496
84875 60233 62482 70419 78623 20900 21609 90235 30436 99418
49146 31409 34317 38143 64054 62531 52096 18369 08887 07016
76839 64243 78140 59271 45635 49061 30310 72085 10383 75051
01157 47704 17189 86106 87396 96552 12671 54688 95703 50354
02123 40784 98193 34321 06817 01210 05627 88023 51930 33224
74501 58539 04730 41995 77770 93503 66041 69973 29725 08868
76966 40355 57071 62268 44716 25607 98826 51787 13419 51246
65201 03059 21236 67719 43252 78675 39855 89448 96970 96409
75459 18569 56380 23637 01621 12047 74272 28364 89613 42251
64450 78182 44235 29486 36372 14174 02388 93441 24796 35743
70263 75529 44483 37998 01612 54922 78509 25778 25620 92622
64832 62779 33386 56648 16277 25164 01910 59004 91644 99828
93150 56604 72580 27786 31864 15519 56532 44258 69829 46959
30801 91529 87211 72556 34754 63964 47910 14590 40905 86298
49679 12874 06870 50489 58586 71747 98546 67757 57320 56812
88459 20541 33405 39220 00113 78630 09455 60688 16674 00169
84205 58040 33637 95376 45203 04024 32256 61352 78369 51177
88386 38744 39662 53224 98506 54995 88623 42818 99707 73327
61717 83928 03494 65014 34558 89707 19425 86398 77275 47109
62953 74152 11151 36835 06275 26023 26484 72870 39207 64310
05958 41166 12054 52970 30236 47254 92966 69381 15137 32275
36450 98889 03136 02057 24817 65851 18063 03644 28123 14965
50704 75102 54465 01172 72115 55194 86685 08003 68532 28183
15219 60037 35625 27944 95158 28418 82947 87610 85263 98139
55990 06737 64829 22443 75287 18462 45780 36192 98197 13991
47564 48826 26039 03381 44182 32625 15097 48279 87779 96437
30899 70388 86778 22713 83605 77297 88241 25611 90717 66394
65070 63304 52795 46618 55096 66618 56647 09711 34447 40160
70462 62156 80717 48187 78443 71436 98821 85596 70959 10259
68620 02353 71858 87485 69652 20005 03117 34392 07321 13908
03293 63447 97273 55955 27734 90717 83793 42163 70120 50054
51326 38354 40001 86323 99149 07054 79778 05669 78533 58048
96690 62951 19432 47309 95876 55236 81285 90413 83241 16072
26029 98330 53537 08761 38939 63917 79574 54016 13722 36187
89365 26053 81558 41587 18692 55386 06164 77983 40254 35128
43961 29460 35291 33259 42794 90433 72990 85731 58029 09586
31382 68329 14771 16396 33709 24003 16894 58636 06064 58459
25126 99465 57248 39186 56420 97526 85082 30754 42545 99376
91704 19777 80085 36273 09417 10163 43490 76964 23722 29435
23661 25572 50881 47792 23151 97477 80605 69672 53801 71807
76360 34624 59278 77846 58506 56050 78084 42115 29697 52189
08740 19660 90665 18035 16501 79250 46195 01366 58543 66327
12549 63990 85491 44200 01457 47608 19302 21206 60243 30096
41270 48943 90397 17719 51806 99086 99860 66365 83232 27870
93765 02260 14929 10115 17177 63594 46020 23249 30028 04018
67723 91028 80978 66605 65118 32600 43688 50881 71572 38669
84224 22010 24950 55188 16948 03221 00251 54264 94639 81287
36776 58927 68816 35983 12477 88652 01411 74110 91360 11649
95076 62907 79436 46005 85194 19985 60162 64790 76153 21038
72755 71269 92518 27568 79893 02761 76114 61625 49356 49590
37980 45838 18232 33686 12016 24373 65698 46703 78585 33052
75833 33793 99075 21660 69238 05336 98879 56513 72855 93883
49989 47074 16181 55012 53970 64648 17194 67083 48197 21448
88987 90676 50379 59036 69672 49499 25452 79033 72963 61626
58976 03949 85767 41397 35944 10237 44329 70935 54779 82629
61459 14429 36451 42861 71585 87339 74679 18975 71211 95618
73857 83644 75844 84235 55581 05002 56114 92391 51889 30994
63428 41393 60803 83091 66281 88115 03715 28496 70597 41625
62823 60921 68075 15017 77253 87402 56425 34708 79089 13729
17228 28611 51591 56837 25241 63077 22544 06337 87593 10598
26760 94420 32619 24285 31701 87817 72960 23541 30606 72136
04600 03896 61093 64709 51414 17185 77701 41806 06443 63681
54644 40053 31608 77831 43174 44081 19494 22975 59931 40118
88683 31483 28027 06553 83300 46932 90115 74414 75631 39997
22170 38046 17092 89457 90962 71662 26074 07187 49975 35921
27560 84414 73782 33032 70330 16823 71936 48002 17328 57349
35947 56433 41299 43024 85023 57322 14597 84328 26414 21684
87872 16733 67010 61509 42434 56984 40187 33128 10107 94512
72237 37886 12605 81656 68053 71439 61278 88732 52737 38903
92890 50686 53241 38062 79602 59303 87727 69778 37928 68409
32536 58807 33988 45721 87460 21005 31148 33513 23850 04782
71693 76218 00490 47955 97959 29059 16554 70505 77751 43081
75112 69898 51884 08718 56402 60353 05583 73783 24229 24185
62564 42550 22672 15598 02740 12617 97192 80471 39600 68916
38286 65277 00975 27670 69777 03643 92602 24372 84184 08832
51848 77047 26384 40379 53016 69054 65937 46161 93238 40363
89313 13643 27137 68884 10268 11219 89127 52230 56256 75625
47017 25086 34976 53672 88605 96675 27408 68627 40791 28565
76996 31378 97530 34660 61666 98042 18267 72456 05306 60773
89962 42183 40859 88207 18646 82623 21508 02882 86359 74683
96543 58856 68550 37731 31296 58797 58105 01214 91620 76567
69950 65971 53447 63470 32085 32156 03674 82860 83786 56803
07306 26576 33469 77429 56346 43716 70939 71930 60876 96349
53288 46833 61303 88294 31040 80029 68738 69117 06666 61468
 
8

88888 Dihedral

yes but i'm a little ot because it is C++...

So, let's try something /on-topic/, then, shall we?

/*
** This program was inspired by the article "The Impossible Dream:
** Computing e to 116,000 places with a Personal Computer" by
** Stephen Wozniak, published in Byte Magazine Vol 6, Issue 6
** (June 1981, pp392).
**
** Essentially, the following program implements a partial expansion
** of the equation: e = 1 + (1/1!) + (1/2!) + (1/3!) + ... + (1/n!)
** by inverting the equation, such that it reads...
** e = (((((...(((((1/n) + 1) / (n-1)) + 1) ... / 3) + 1) / 2) + 1) /
1) + 1
**
** I originally wrote this program in Z80 Assembler code, to run under
** CP/M (Dec 1986), and subsequently have recoded the program in
** 370 Assembler to run under MVS and VSE, and C to run under MSDOS
** and Linux
**
** Lew Pitcher, 1999
*/
#include <stdio.h>

#define I 1 /* number of bytes in integer portion */
#define X 2078 /* number of bytes in fraction portion */
#define N 1776 /* number of divide iterations */
#define P 5000 /* number of multiply iterations */

char e[I+X]; /* holds fixed point binary value of e */

int main(void)
{
void divide(char *, unsigned, unsigned),
multiply(char *, unsigned, unsigned);

unsigned n, /* divisor for calculation loop */
p; /* precision for print loop */

e[0] = 1; /* start the series at 1.anything */
for (n = N; n > 0; --n)
{
divide(e,n,I+X);
e[0] += 1;
}

fputs(" e == ",stdout); fputc(e[0]+'0',stdout); fputc('.',stdout);
for (p = 0; p < P; ++p)
{
e[0] = 0;
multiply(e,10,I+X);
fputc(e[0]+'0',stdout);
if (p%5 == 4)
if (p%50 == 49)
fputs("\n\t",stdout);
else
fputc(' ',stdout);
}
fputc('\n',stdout);

return 0;
}

void divide(char *dvd, unsigned dvr, unsigned prc)
{
char *Wk_q;
unsigned bit, rmdr;

rmdr = 0;
for (Wk_q = dvd; Wk_q < dvd+prc; ++Wk_q)
{
for(bit = 0; bit < 8; ++bit)
{
rmdr *= 2;
if (*Wk_q & 0200) rmdr += 1;
*Wk_q <<= 1;
if (rmdr >= dvr)
{
*Wk_q += 1;
rmdr -= dvr;
}
}
}
}

A division of a long integer with a known length of bytes by an integer of 8 to 1bits in a digit by digit division which that every digit is at least 8 bits is still missing.

This is trivial in C, but I'll see if we have to start a c programming race here.
 
L

Lew Pitcher

A division of a long integer with a known length of bytes by an integer of 8 to
1bits  in a digit by digit  division which that every digit is at least 8 bits  is
still missing.

This is trivial in C, but I'll see if we have  to start a c programmingrace here.

Since I haven't seen one line of /your/ C code, I suggest that you
tackle this "trivial" problem yourself. I'm looking forward to your
solution.
 
8

88888 Dihedral

I know that a division in C in some compiler in most CPUs is really slow. I write that in assembly.
 
I

Ian Collins

On 11/26/11 07:43 PM, 88888 Dihedral wrote:

If you are going to post to Usenet, for goodness sake quote the context
you are relying to!
I know that a division in C in some compiler in most CPUs is really slow. I write that in assembly.

Examples and evidence please.
 
L

Lew Pitcher

Lew Pitcher ha scritto: [snip]
for (p = 0; p < P; ++p)
{
e[0] = 0;
multiply(e,10,I+X);
fputc(e[0]+'0',stdout);
if (p%5 == 4)
if (p%50 == 49)


here my compiler says:

temp.c: In function ‘main’:
temp.c:50: warning: suggest explicit braces to avoid ambiguous ‘else’

The C standard is pretty clear about the association of <<else>> to the
appropriate <<if>>For instance, in IEC 9899-1999, section 6.8.4.1 ("Theif
statement"), point 3, it says:
"An else is associated with the lexically nearest preceding if that is
allowed by the syntax."

That doesn't seem very ambiguous to me.

I suggest that your compiler's warning level is wound up too tight. I know
that compilers are permitted to emit warnings for anything they want, from
noting potential syntax errors to baying at a full moon. That doesn't mean
that the compiler doesn't know what to do with the code, or that the code
violates the C standard in any way. It just means that the compiler writer
thought that the programmer would like to know something.

Now, had my intent be to have the <<else>> associate to the /first/ <<if>>
statement in the sequence, then the warning would be relevant. But, since I
intended (as actually occurred) that the <<else>> be associated to
the /second/ <<if>> statement, and the C standard permits the expression I
used, then your compiler's warning is just noise.

HTH
 
8

88888 Dihedral

Do you? Would you tell me what you know about division that the compiler
writer doesn't know?

I contribute a c function to divide a pointer to an unsigned char array with a known length that produces a remainder of the long real number.

int longreal_divide_mod5( char *longnum, int length)
{
int i;
unsinged long remainder; // 32 or 64 bits, I'll show the 32 bit version
tmp=0;

for( i=0, remainder=0;i<length;i++)
remainder+=longnum; // mod 255 done in the first pass

// assume remainder won't over flow
tmp+= (remainder>>24); //top 8 bits
tmp+= ((remainder)<<8)>>24 ; // another 8 bits
tmp+= ((remainder)<<16)>>24 ; // another 8 bits
tmp+= remainder & 0xff;// final 8 bits
//4 *255<=1020 in 10 bits

while (i=tmp&512) if i { tmp^=512; tmp++};
while (i=tmp&256) if i { tmp^=256; tmp++};
// tmp in a byte now
return tmp%5; //there is another way to avoid the mod
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,083
Messages
2,570,591
Members
47,212
Latest member
RobynWiley

Latest Threads

Top