A
A
aab1372020-03-17 18:39:13
Visual Basic
aab137, 2020-03-17 18:39:13

I rewrote the code to BASIC from python, why does it work incorrectly?

I have a python code

def f(x):
    return (100-2*x)*(75-2*x)*x


eps = 0.00001

l = 0
r = 75/2

while r-l > eps:
    m = (r+l)/2
    if (f(l)-f(l+eps)) * (f(m)-f(m+eps)) > 0:
        l = m
    else:
        r = m
print((r+l)/2)

Gives out about 14.14, that's right
Rewrote to BASIC
Function f(ByVal x As Single) As Single
    f = (100 - x * 2) * (75 - x * 2) * x
End Function
Sub program()
    Dim l As Single, m As Single, eps As Single, r As Single
    eps = 0.00001
    l = 0
    r = 75 / 2
    Do While r - l > eps
    m = (r + l) / 2
    If (f(l) - f(l + eps)) * (f(m) - f(m + eps)) > 0 Then
        l = m
    Else
        r = m
    End If
    Loop
    Cells(1, 1).Value = (r + l) / 2
End Sub

Gives 13.47569942, which is incorrect. What did I write wrong?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question