B4J Question B4J vs Java Calculation

Johan Schoeman

Expert
Licensed User
Longtime User
I have this string in a text file:

B4X:
448.12024391174316,262.125

A Java project that I am trying to convert to B4J as well as my B4J project both logs this correctly (let call it an X and Y value separated by a ","

In both the B4J and Java projects the following applies:
B4X:
    width = 1280   'declared as an int
    height = 720   'declared as an int
    minusheight = height / 1.65    'declared as Double
    minuswidth = width / 2.5    'declared as Double
    devide = height / 300    'declared as Double

The confusing part is this:

B4J code:
        Dim c As ComplexNumber
        c.Initialize((s.substring2(0, comaindex) - minuswidth) / devide, _
        (s.substring2(comaindex + 1, s.length()) - minusheight) / devide)
        X.add(c)

        Log("c.re = " & c.re)
        Log("c.im = " & c.im)

Java code:
            ComplexNumber c = new ComplexNumber((Double.valueOf(s.substring(0, comaindex)) - minuswidth) / devide,
                    (Double.valueOf(s.substring(comaindex + 1, s.length())) - minusheight) / devide);
            X.add(c);
            BA.Log("c.re = " + (Double.valueOf(s.substring(0, comaindex)) - minuswidth) / devide);
            BA.Log("c.im = " + (Double.valueOf(s.substring(comaindex + 1, s.length())) - minusheight) / devide);

With the same initial value (x = 448.12024391174316 and y = 262.125) the calculation results are as follows:
B4J re value = -26.616565036773686
B4J im value = -72.59943181818183

Java re value = -31.93987804412842
Java im value = -87.11931818181819

With my calculator I calculate the results of re and im to be the same as that of the B4J code.

What is happening here? Why the difference?
 
Solution
the B4J code and inline java code yields different answers for Re and Im. The inline java code yields the correct answers. The B4J code does not.

You can make the B4J results match the Java results by forcing emulating integer division using Floor() :
B4X:
width = 1280
height = 720
minusheight = height / 1.65
minuswidth = width / 2.5
devide = Floor(height / 300)
  
time = 0.0f
Log output:
Waiting for debugger to connect...
Program started.
s = 448.12024391174316,262.125
B4J Re = -31.93987804412842
B4J Im = -87.11931818181819
Java Re = -31.93987804412842
Java Im = -87.11931818181819

agraham

Expert
Licensed User
Longtime User
I still don't understand how the Americans managed (and dared) to fly to the moon and back again with an on-board computer that was not much more than a "pocket calculator" of our days
Read the book "Digital Apollo" by David A. Mindell. It goes into detail about the multi-layered backups they had if anything failed. Most orbital calculations were done on the ground by an IBM mainframe and manually entered into the flight computer. If that failed they could do some calculations in the spacecraft them selves and the final backup was something like a wax crayon and etched lines on one of the perspex windows! Well worth a read if you are interested in early computers.
 
Last edited:
Upvote 0

agraham

Expert
Licensed User
Longtime User
Computers in Spaceflight by James Tomayko is available on Kindle. I've had it a while but your post reminded me to read it as I have a full boring day later this week having scans in hospital.

Another interesting book, also available on Kindle, is
The History of Spacecraft Computers from the V-2 to the Space Station by Patrick Stakem
 
Upvote 0
Top