Page 1 of 1

Numerical Methods Question

Posted: Tue Feb 15, 2022 3:28 am
by glennj.0158
I've got a really odd numerical result that I can't explain. Look at the following code snippet -- you can copy and run and you will get the MesageRequester message to see the results.
f1.d = 325.0
f2.d = 0.125
f3.d = f1 + f2
f4.d = f1 + 1.0 / 8.0
f5.d = -1 * f1 + 1.0 / 8.0
f6.d = -1.0 * f1 + (1.0 / 8.0)
MessageRequester ("Values", "f1 = " + f1 + ~"\n" + "f2 = " + f2+ ~"\n" +"f3 = " + f3 + ~"\n" + "f4 = " + f4 + ~"\n" +
"f5 = " + f5+ ~"\n" + "f6 = " + f6 )

The results are F1 = 325, F2 = .0125, F3 and F4 = 325.125 BUT F5 and F6 = -324.875

The calculation should all be in FLOAT and an error of 0.5 is both huge and unacceptable. As an old FORTRAN coder I understand that precision decreases with magnitude and that you have to be careful to keep fractional calculations in FLOAT or DOUBLE to avoid truncation
to integer (Hence 1.0 / 8.0 rather than 1/8 which could be zero). Any thoughts would be helpful. Any helpful thoughts would be appreciated.

Re: Numerical Methods Question

Posted: Tue Feb 15, 2022 3:33 am
by glennj.0158
Did some more poking around and the issue is multiplication by -1
The same code snippet multiplying by 1 gives correct results across the board.

Re: Numerical Methods Question

Posted: Tue Feb 15, 2022 4:09 am
by skywalk
What is your question?
Remember to use code blocks and EnableExplicit.

Code: Select all

EnableExplicit
Define.d f1,f2,f3,f4,f5,f6
f1 = 325.0
f2 = 0.125
f3 = f1 + f2
f4 = f1 + 1 / 8
f5 = -1 * f1 + 1 / 8
f6 = -1 * f1 + (1 / 8)
Debug "Values"
Debug "f1 = " + f1
Debug "f2 = " + f2
Debug "f3 = " + f3
Debug "f4 = " + f4
Debug "f5 = " + f5
Debug "f6 = " + f6

Re: Numerical Methods Question

Posted: Tue Feb 15, 2022 5:05 am
by BarryG

Code: Select all

f1 = 325.0
f5.d = -1 * f1 + 1.0 / 8.0
glennj.0158 wrote: Tue Feb 15, 2022 3:28 amBUT F5 and F6 = -324.875
Yes, that's correct. Microsoft Excel confirms it:

Image

Visual Basic confirms it:

Image

Windows Calculator confirms it:

Image

Re: Numerical Methods Question

Posted: Tue Feb 15, 2022 1:07 pm
by glennj.0158
And I figured out which tyro mistake I was making -- -1 * A + B <> -1 * (A + B)

This is a demonstration of the "Coat Rack" effect. The process of describing the problem to others forces one to think clearly about the problem in order to articulate it. I doesn't matter if the "others" understand the issues involved to the point that describing them to your coat rack can often provide a fix.

Thanks

Re: Numerical Methods Question

Posted: Tue Feb 15, 2022 1:09 pm
by BarryG
glennj.0158 wrote: Tue Feb 15, 2022 1:07 pmThis is a demonstration of the "Coat Rack" effect
Also known as Rubber Duck debugging -> https://rubberduckdebugging.com

Re: Numerical Methods Question

Posted: Tue Feb 15, 2022 1:46 pm
by Olli
Ok... On mind... 325 Ok
1/8... 0.25/2 = 0.125 Ok

-325+0.125 = -(325-0.125) = -(324.9 - 0.025)
= -(324.88 - 0.005)
= -324.875
Ok

What would be the question I would not do having known in my mind ?