Bug? [Solved] something is not completed , only with pause

derez

Expert
Licensed User
Longtime User
The attached small app is scanning x axis to find points which near them a function changes value from positive to negative or vice versa. This is a preparation for an algorithm that find the roots (i.e where y = f(x) = 0 ). The poins are added to a list but the problem happens also with just logging the found points.
The function has 3 roots. When run in debug , without any pausing point defined, the program shows only two points. when run with a pause point, even on a comment, the list contains 3 points.
When run in release, the pause has no effect and there are only two values in the list.
B4j version 10, core version 9.8 (????).
 

Attachments

  • bisection.zip
    2.7 KB · Views: 31

Erel

B4X founder
Staff member
Licensed User
Longtime User
The "correct" result is the one that you get in release mode. It will be the same as the result when there are no breakpoints in the code.
There is an edge case here related to the way negative zero is handled by the debugger which causes it to be considered smaller than zero.

While there is a mismatch here, it is a mistake to assume that doubles will return precise results. So if 0 is the boundary then change the comparison to:
B4X:
If p * q < 0 - Epsilon Then

Where Epsilon is defined:
B4X:
Private Epsilon As Double = 0.000000001
 

klaus

Expert
Licensed User
Longtime User
With this code it works:
B4X:
If p * q < 0 Or q = 0 Then

With Erels code I get 4 items in the List.
Because when i = 1 then q = fx(1 + 1) = 0 and p * q < Epsilon
And when i = 2 then p = 0 and p * q < Epsilon.
 
Top