Android Question src\b4a\example\calcs.java:329: error: unreachable statement

Monostatos

New Member
I have an application, written in C, working on a microprocessor with an LCD display. I am trying to convert it, using B4A, to run on a phone so that it can have a nice graphical display.

The particular problem that stumps me at present is that my square root routine, written to avoid using floating point arithmetic, fails to compile with this message:

If (work <= 1) Then Return retVal
src\b4a\example\calcs.java:329: error: unreachable statement

This is the code

Sub sqrti(inVal As Long) As Int
Private retVal = 1, work As Long
Do While (True)
retVal = (retVal + inVal / retVal) / 2
work = Abs((retVal * retVal) - inVal)
If (work <= 1) Then Return retVal
Loop
End Sub

Presumably the java compiler thinks that work can never be <=1. But a desk check shows it always converges to <=1. And the equivalent routine in C works fine.

I'd be grateful for any clues how to proceed. Where can I find the generated java code?
 

agraham

Expert
Licensed User
Longtime User
The generated Java code is under Objects/src/... in the project folder. I've looked at it and can't immediately see why it complains. However this works
B4X:
Sub sqrti(inVal As Long) As Int
    Dim retVal As Int = 1
    Dim work As Long = 2
    Do While work > 1
        retVal = (retVal + inVal / retVal) / 2
        work = Abs((retVal * retVal) - inVal)
    Loop
    Return retVal
End Sub
 
Upvote 0

Monostatos

New Member
Found the subroutine need modifying as follows

Sub sqrti(inVal As Long) As Int
Private retVal = 1 As Int
Private oldRv = 100 As Int
Do While Abs(retVal - oldRv) > 1
oldRv = retVal
retVal = (retVal + inVal / retVal) / 2
Loop
Return retVal
End Sub
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…