HP Forums

Full Version: Bug in MATH ROM of the TI-95
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have been tinkering with the TI-95 and its MATH and STAT ROMs. I discovered that the Newtons Root method (option NTN in option ZRO) has a bug that causes the solution FOR ANY FUNCTION to jump to huge numbers and then overflow. I tested my fx(x)=exp(x)-3*x^2 and fx(x)=x^2-5*x+6 functions and both bombed out. I inserted a pause (PAU) command in my function code to trace the values used by NTN and found that it does the following:

1) Evaluate fx at you initial guess (call it X).
2) Evaluate fx at X plus a small increment.
3) Evaluate fx at X minus a small increment.
4) Evaluate fx at a large number.
5) Repeat step 4 with different large numbers until the calculations overflow.

I also noticed that the TI-95 emulator by HrastProgrammer has the same bug in his port of the MATH ROM! I guess his ported the binary image of the MATH ROM.

Anyone else out there has seen this bug?

The Bisection method in the MATH ROM works fine!

Namir
Weird! The ROM listing is available here via the "TI-95 Mathematics Module" link if anybody wants to reverse engineer what's going on:

http://www.rskey.org/CMS/index.php/the-library/12
Based on the source code of NTN starting at line 683A I wrote these functions in Python:
Code:
from math import exp, hypot

ε = 1e-9

def df(f, x):
    return (
        (f(ε) - f(-ε)) / ε / 2
        if x == 0
        else (f(x * (1 + ε)) - f(x * (1 - ε))) / ε / 2 / x
    )

def ntn(f, x_0, δ, n):
    x = x_0
    for i in range(n):
        fx = f(x)
        dx = fx / df(f, x)
        if hypot(dx, fx) <= δ:
            break
        x -= dx
    else:
        print("#it REACHED")
    return x

def f(x):
    return exp(x) - 3 * x**2

def g(x):
    return x**2 - 5 * x + 6

This is the mapping of the variables to the registers:
  • 0000: \(x\)
  • 0001: \(\varepsilon = 10^{-9}\)
  • 0005: \(x_0\)
  • 0006: \(f(x)\)
  • 0009: \(\delta\)
  • 0010: \(i\)

Examples

\(
\begin{align}
f(x) &= e^x - 3x^2 \\
x &= -0.458962 \\
x &= 0.910008 \\
x &= 3.73308 \\
\end{align}
\)

Code:
ntn(f, -1, 1e-10, 10)

-0.45896226753694863

Code:
ntn(f, 1, 1e-10, 10)

0.9100075724887092

Code:
ntn(f, 4, 1e-10, 10)

3.7330790286328144


\(
\begin{align}
g(x) &= x^2 - 5 x + 6 \\
x &= 2 \\
x &= 3 \\
\end{align}
\)

Code:
ntn(g, 0, 1e-10, 10)

1.9999999999942588

Code:
ntn(g, 4, 1e-10, 10)

3.0000000000000013


To me the implementation in the MATH ROM looks solid.
However, I was able to get nonsensical results if the starting value was close to a critical point where the derivative is 0.

Examples

Code:
ntn(f, 2.833, 1e-10, 5)

#it REACHED
-271.9295080855029

Code:
ntn(g, 2.5, 1e-10, 10)

#it REACHED
-2746.2796055906856


Though I haven't tried it in an emulator, I assume that both the accuracy \(\delta\) and the maximal number of iterations \(n\) have to be stored in registers 0009 and 0010 respectively.
Also if the value in register 0010 is 0, the default value 10 is used instead.

Maybe you can check if the value in register 0009 is not 0?
It should be a fairly small value, like maybe \(10^{-5}\).

But I'm not sure if that helps.
Thanks Thomas.

I used the PoketEmul simulator on my iPhone torun the TI-95 emulator and loaeded the MATH rom. I GOT THE SAME OVERFLOW ERROR WITH THE NEWTON METHOD!

My conclusion is that while the listing you provided me is correct (and the Python code your provided runs correctly), there may be a last minute bug introduced in the ROM (kind of remind us with the last minute code change for the HP-15LE firmware that caused the famous and annoying PAUSE bug!).

I recieved another MATH rom pack yesterday. So I will test that ROM on my TI-95.

Namir
I used the TI-95E MicroCode Emulator by HrastProgrammer and followed the instructions of the TI-95 MATHEMATICS LIBRARY GUIDE BOOK to create the following programm:
Code:
LBL fx ( RCL A INV LN -3* RCL A x^2 ) RTN

I've set:

x0 = 1
err = 1EE-5
#it = 10

And then got the following result:

[Image: attachment.php?aid=11830]

It also works for the other initial values.
Thank you Thomas for solving the riddle. I was NOT enclosing the expression for fx in parentheses!! When I coded fx like you did on the TI-95 emulator and a real TI-95, it worked in both cases!!!

Thank you for helping solve the mystery!!!

Namir
Reference URL's