August 13, 2024, 02:03:18 AM

## News:

IWBasic runs in Windows 11!

## Limits and Nan Problems

Started by GWS, October 10, 2012, 12:36:41 PM

0 Members and 1 Guest are viewing this topic.

#### GWS

##### October 10, 2012, 12:36:41 PM
Hi folks,

Something came up on the IWB posts which is worth filing under the manana section ..

It's to do with what happens if some calculation you are doing, encounters a limit of what the computer can deal with.

Suppose you are dividing two numbers as follows:

openconsole
cls

setprecision 15

def a,b,y:double

a = 0
b = 0

'if b = 0 then b = 1.e-12
y = a/b

print y

do:until inkey\$<>""
closeconsole
end

The program will crash with an error message "Divide by zero on Line 12".

And why is this ?   The form 0/0 is mathematically "indeterminate" - it has no valid answer.

Good grief! .. that's terrible.  But wait, it's not just Creative Basic  - every computer language will do the same.

This is the nightmare territory of Limits - both mathematical, and computer limits imposed by the number of bits allocated to variables (8,16,32,64 etc.).

It gets worse - if you'd tried the program above in some other language, you may well have got the answer
y = -1.#IND   .. the IND meaning an indeterminate number.

The program wouldn't crash, but I don't find much comfort in that ..  Like, where do you go next?

Any division you do might encounter this problem - it all depends on the data you're processing.

What can be done about it ?  It depends on your application and the algorithm you're using.  In real world applications it doesn't happen often - I've only encountered it once in 10 years.  You're more likely to come across it during testing a new program, where a variable divisor has not been initialised and is zero.

But what is zero?  In practical problems, it's a very small value.  So the problem above can be solved by testing whather the divisor is zero, and substituting a small value such as 1.e-6 (a millionth of something).

Then you will get the answer y = 0.
Which since anything multiplied by zero is zero, is a good practical result.
Try it by removing the comment on the line: if b = 0 then b = 1.e-6

Here's another example.

You are for some reason we won't go into, investigating the expression in this code:

openconsole
cls

setprecision 15

def a,b,d,x,y:double

'd = 1.e-6
x = 1 + d

a = x^3 - x^2 + 3*x - 3
b = x^2 + x - 2

y = a/b

print y

do:until inkey\$<>""
closeconsole
end

We are testing the ratio of (x^3 - x^2 + 3*x - 3) divided by (x^2 + x - 2), as the value of 'x' moves towards a limit of 1.0

If you run it, you will get the dreaded Divison by zero again.

Solution ?  Add a small value 'd' = 1.e-6 to 'x'.

This makes no practical difference to 'x', but the result will now be y = 1.3333333, which is the correct answer.  Try it by removing the comment from the line d = 1.e-6.

One last example, because these things can come at you from all directions ..

openconsole
cls

setprecision 20

def a,b,d,x,y:double

d = 1.e-6
x = 0 + d

a = x - sin(x)
b = x - tan(x)

y = a/b

print y

do:until inkey\$<>""
closeconsole
end

Same thing = we hit a limit at x = 0. and there is the "Divide by Zero" again.
Same solution - just add a small delta value:  d = 1.e-6 to the variable 'x'.

If you run it now, you will get the correct answer:  y = -0.5

So although Creative Basic has no magic way out of mathematical limit problems - the good thing is, there's always another way.

In my opinion, it's not worth worrying about.  Continue developing your applications until the unlikely event occurs when you meet some such problem, and only then investigate your algorithm for a practical solution.

Creative is a robust language which will normally give you a sound result.

NaN's (Not a number) is a can of worms which could complicate your code considerably.

http://en.wikipedia.org/wiki/NaN

Probably best left to Fortran and the mathematics gurus.

all the best,

Graham

Tomorrow may be too late ..