# HP Forums

Full Version: Base of Natural Logarithms
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

Is there programmatic way to calculate the base of natural logarithms (or Euler Number) if the function of "e" were not available. I know what the formula is for "e", but how would one pro grammatically find this on say a 35s, or other similar calculator?

Edited: 13 Aug 2007, 7:37 p.m.

Does the 35s not have e^x?

You could use a series expansion.

Do you want to have unlimited accuracy? or is this close enough?

```848456353
--------- = 2.71828182845904523(4757)
312129649
```

That's close enough for most calculations.

Or use a simple loop to find the sum of 1/n! as n goes from 0, 20. This gives 2.7182818284590452353(39784).

Edited: 13 Aug 2007, 8:41 p.m.

This is a pretty simple program to write, just a few lines. You can also compute it in the equation solver in several different ways. But it doesn't need to be computed at all on this machine, it's now in the constants menu.

On the 35s:

• use the solver with LN(X) = 1
• iterate the following steps a few times with initial value 1:
```ENTER
LN
2
x<>y
-
*
```
• calculate (1 + 1E-6)^1E6
• SINH(1)+COSH(1)
• ...

Just a few ideas.

Edited: 14 Aug 2007, 3:03 a.m.

Here's the problem I have with the programming required of some calculators. To do a loop from 1 to 20 of 1/n! stored in a register, this is the best I can come up with ...

```
S001 LBL S
S002 -1.02
S003 STO I
S004 ISG I
S005 GTO S007
S006 RTN
S007 RCL I
S008 INTG
S009 !
S010 1/x
S011 STO+ E
S012 GTO S004
```

This seems like a rediculous number of steps for a simple loop.

CHUCK

Erm, I'm not sure if I understand what you're asking here. You say that you know the formula for e, but you can't figure out how to write a program to compute that value?

e = 1 + 1/1 + 1/(1*2) + 1/(1*2*3) + 1/(1*2*3*4) + 1/(1*2*3*4*5) + ...

Hint: if you number the terms like 0, 1, 2, 3, etc., then for all terms numbered 1 and greater, term n is a factor n smaller than the one before. Just keep summing until adding a term no longer changes your result; thanks to the limited precision of most calculators, that won't take long.

If you're going to ask for an actual program, I'm going to start suspecting you're a student asking us to do your homework. ;-)

- Thomas

One step less by running the loop backwards.
Also better numerically.

```E001  LBL E
E002  20
E003  STO I
E004  1
E005  RCL I
E006  !
E007  1/x
E008  +
E009  DSE I
E010  GTO E005
E011  RTN
```

- Pauli

I now see that the STO+ was an unecessary step (since the values remain on the stack), but why would backwards be better numerically? Non comprendo.

It is more stable backwards because you're adding like sized terms the whole time and generally maintaining full precision as well.

Going forwards starts with a 1 term and by the time you're near the end, most of the significant digits are beyond the precision of the sum.

The difference will likely be at most a digit or two in the last place but it doesn't take any extra effort to work from the small terms to the large.

- Pauli

Use Horner's Method (p. 13-26 in the User's Guide) to avoid factorial:

```E001 LBL E
E002 STO I
E003 1
E004 RCL/ I
E005 1
E006 +
E007 DSE I
E008 GTO E004
E009 RTN
```

20 XEQ E001

Quote:
You can also compute it in the equation solver in several different ways.

I've tried to solve the following equation:

```ABS(E^(Pi*i)+1)=0
```

I would have expected to get:

```NO ROOT FND
```

```INVALID yx
```

Any ideas why?

It seems that somehow E becomes 0, though this case (i.e. 0 with complex exponent) isn't mentioned in the user's guide as a possible reason for that message.

What a pity you don't get more information, or is there a way to find out what was the last value of E in case of an error?

However it works if 0 is replaced in the equation by 1E-11.

I know it is on calculator, but I just curious how the program would be written.

But formula is lim n -> infinity (1+1/n)^n. What I don't understand is how you deal with infinity. I guess you could loop a couple hundred times to deliver accuracy, but how was this calculated in past, when e not available.

I guess reason I ask is I try and better understand programming on HP calculator, so I thought this would be a good test to see how something like infinity be handled.

Dealing with infinity can be tricky, and there is no one answer for all situations. In the case of the series for e, the key insight is that the terms get progressively smaller as n increases, and that in fact, they shrink so quickly that you only need to sum a few in order to reach maximum precision. 1/16! = 4.78e-14, which is too small to affect the value of e when working with 12-digit precision -- so after summing only 16 terms, you're done. If you're working with higher precision, you'll need more terms, of course.

You can work out in advance when the terms will become too small to affect the result any further, or you can simply compare each iteration's result to the previous one, and terminate the loop when there is no further change. And if you want the best possible accuracy, you can sum the series from smallest terms to largest (that requires working out which is the smallest term in advance), to minimize the effect of round-off error on the final result.

Note that the series for e (or generally speaking, for e^x, and the related series for sin and cos) have the nice property that the terms shrink very quickly, but this is not true for all series, like the notoriously useless tan(x) = 1 - x/3 + x/5 - x/7... In such cases you'll have to look for a more clever approach.

- Thomas

Okay, but how you write program to do this? I no student in school, but I am student of life, and want to learn how to do something like this.

I try some programming but I still very green as you say in America, and still trying to grasp concept of RPN programming.

If this was in C, cobol, or assembler, I would know what to do, but I baffled with programming on 35s. Maybe it too simple and that my issue.

Vincze, you need to get the museum DVD. It has the manuals of almost all HP calculators, and you need to read the programming sections of those manuals. That will teach you how to program in RPN.

But I have HP35s and its manual, but it not explain the programming and looping and loop testing, etc. If I want to loop something say 20 times, how would I control that in 35s and take final value and display? It talk about very little in manual. I have friend who has older version of CD's from here. What would be good manual to look at that explain this better than current 35s manual.

Vincze, how can you say the 35s manual does not discuss programming and looping? Part 2 of the manual is about nothing but programming. Loops are described on pages 14-16 to 14-18. You really need to read these sections and play around with it.

9242691/3400196 (within 7 digits, error < 9.4e-14)
this is also enough for 35s.

49171/18089 (within 16 bit, error < 1.02e-10)
may be used in a micro controller.

You also could use this:

```(IDIV(I,18)-1)/A = (1-IDIV(I,18))/A + 0*(E+1/I!STO E) + 0*(I+1STO I)
Solve for A
I=0 R/S
E=0 R/S
RCL E```

or still:

```(IDIV(I,18)-1)/A = (1-IDIV(I,18))/A + 0*(E/(20-I)+1STO E) + 0*(I+1STO I)
Solve for A
I=0 R/S
E=1 R/S
RCL E```

Just amazing! enjoy.

Miguel