Numerical Methods Question

Just starting out? Need help? Post your questions and find answers here.
glennj.0158
User
User
Posts: 21
Joined: Tue Mar 31, 2020 4:43 pm
Location: Lawrenceville, NJ, USA

Numerical Methods Question

Post 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.
glennj.0158
User
User
Posts: 21
Joined: Tue Mar 31, 2020 4:43 pm
Location: Lawrenceville, NJ, USA

Re: Numerical Methods Question

Post 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.
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Numerical Methods Question

Post 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
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
BarryG
Addict
Addict
Posts: 4136
Joined: Thu Apr 18, 2019 8:17 am

Re: Numerical Methods Question

Post 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
glennj.0158
User
User
Posts: 21
Joined: Tue Mar 31, 2020 4:43 pm
Location: Lawrenceville, NJ, USA

Re: Numerical Methods Question

Post 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
BarryG
Addict
Addict
Posts: 4136
Joined: Thu Apr 18, 2019 8:17 am

Re: Numerical Methods Question

Post 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
Olli
Addict
Addict
Posts: 1202
Joined: Wed May 27, 2020 12:26 pm

Re: Numerical Methods Question

Post 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 ?
Post Reply