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.
Numerical Methods Question
-
- User
- Posts: 21
- Joined: Tue Mar 31, 2020 4:43 pm
- Location: Lawrenceville, NJ, USA
-
- User
- Posts: 21
- Joined: Tue Mar 31, 2020 4:43 pm
- Location: Lawrenceville, NJ, USA
Re: Numerical Methods Question
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.
The same code snippet multiplying by 1 gives correct results across the board.
Re: Numerical Methods Question
What is your question?
Remember to use code blocks and EnableExplicit.
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
Re: Numerical Methods Question
Code: Select all
f1 = 325.0
f5.d = -1 * f1 + 1.0 / 8.0
Yes, that's correct. Microsoft Excel confirms it:

Visual Basic confirms it:

Windows Calculator confirms it:

-
- User
- Posts: 21
- Joined: Tue Mar 31, 2020 4:43 pm
- Location: Lawrenceville, NJ, USA
Re: Numerical Methods Question
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
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
Also known as Rubber Duck debugging -> https://rubberduckdebugging.com
Re: Numerical Methods Question
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 ?
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 ?