The Museum of HP Calculators

HP Forum Archive 17

[ Return to Index | Top of Index ]

Base of Natural Logarithms
Message #1 Posted by Vincze on 13 Aug 2007, 7:35 p.m.

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.

      
Re: Base of Natural Logarithms
Message #2 Posted by Brad Davis on 13 Aug 2007, 8:10 p.m.,
in response to message #1 by Vincze

Does the 35s not have e^x?

You could use a series expansion.

      
Re: Base of Natural Logarithms
Message #3 Posted by Chuck on 13 Aug 2007, 8:34 p.m.,
in response to message #1 by Vincze

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.

            
Re: Base of Natural Logarithms
Message #4 Posted by Lyuka on 14 Aug 2007, 2:02 p.m.,
in response to message #3 by Chuck

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.

      
Re: Base of Natural Logarithms
Message #5 Posted by Katie Wasserman on 13 Aug 2007, 8:39 p.m.,
in response to message #1 by Vincze

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.

            
Re: Base of Natural Logarithms (OT)
Message #6 Posted by Chuck on 13 Aug 2007, 8:58 p.m.,
in response to message #5 by Katie Wasserman

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

                  
Re: Base of Natural Logarithms (OT)
Message #7 Posted by Paul Dale on 13 Aug 2007, 9:57 p.m.,
in response to message #6 by Chuck

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

                        
Re: Base of Natural Logarithms (OT)
Message #8 Posted by Chuck on 13 Aug 2007, 10:06 p.m.,
in response to message #7 by Paul Dale

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.

                              
Re: Base of Natural Logarithms (OT)
Message #9 Posted by Paul Dale on 13 Aug 2007, 10:57 p.m.,
in response to message #8 by Chuck

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

                        
Re: Base of Natural Logarithms (OT)
Message #10 Posted by Thomas Klemm on 14 Aug 2007, 3:27 a.m.,
in response to message #7 by Paul Dale

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

            
Re: Base of Natural Logarithms
Message #11 Posted by Thomas Klemm on 14 Aug 2007, 5:23 a.m.,
in response to message #5 by Katie Wasserman

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

Instead I got:

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.

            
Re: Base of Natural Logarithms
Message #12 Posted by Vincze on 14 Aug 2007, 8:33 a.m.,
in response to message #5 by Katie Wasserman

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

      
Re: Base of Natural Logarithms
Message #13 Posted by Thomas Klemm on 13 Aug 2007, 8:50 p.m.,
in response to message #1 by Vincze

On the 35s:

  • use the constants menu
  • 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.

      
Re: Base of Natural Logarithms
Message #14 Posted by Thomas Okken on 13 Aug 2007, 9:29 p.m.,
in response to message #1 by Vincze

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

            
Re: Base of Natural Logarithms
Message #15 Posted by Vincze on 14 Aug 2007, 8:41 a.m.,
in response to message #14 by Thomas Okken

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.

                  
Re: Base of Natural Logarithms
Message #16 Posted by Thomas Okken on 14 Aug 2007, 9:34 a.m.,
in response to message #15 by Vincze

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

                        
Re: Base of Natural Logarithms
Message #17 Posted by Vincze on 14 Aug 2007, 10:02 a.m.,
in response to message #16 by Thomas Okken

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.

                              
Re: Base of Natural Logarithms
Message #18 Posted by Don Shepherd on 14 Aug 2007, 10:11 a.m.,
in response to message #17 by Vincze

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.

                                    
Re: Base of Natural Logarithms
Message #19 Posted by Vincze on 14 Aug 2007, 11:15 a.m.,
in response to message #18 by Don Shepherd

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.

                                          
Re: Base of Natural Logarithms
Message #20 Posted by Don Shepherd on 14 Aug 2007, 12:48 p.m.,
in response to message #19 by Vincze

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.

      
Katie Wasserman's "buggy little beast" sigma function
Message #21 Posted by Miguel Toro on 14 Aug 2007, 8:00 p.m.,
in response to message #1 by Vincze

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


[ Return to Index | Top of Index ]

Go back to the main exhibit hall