Java Question Converting a routine to Java

alfcen

Well-Known Member
Licensed User
Longtime User
Hello,

This B4A routine returns a correct value,

B4X:
'Returns the equation of time in minutes decimal.
'x = Date in Ticks, such as DateTme.Now
Public Sub GetEOT(x As Long) As Double
   'ref [url=http://en.wikipedia.org/wiki/Equation_of_time]Equation of time - Wikipedia, the free encyclopedia[/url]
   Dim w, a, b, c As Double
   Dim n As Int
   n = DateTime.GetDayOfYear(x)
   w = 360 / 365.242199
   a = w * (n + 10)
   b = a + 1.9149659 * SinD(w * (n-2)) '1.91... = 360 / cPI * 0.0167... ecc of earth
   c = (a - ATanD(TanD(b) / CosD(23.43755447))) / 180
   Return 720 * (c - Round(c))
End Sub

however, the equivalent routine in Java does not

B4X:
/**
 * Returns the equation of time in minutes decimal.
 * @param x Ticks
 * @return EOT
 */
public double GetEOT(long x) {
   //ref [url=http://en.wikipedia.org/wiki/Equation_of_time]Equation of time - Wikipedia, the free encyclopedia[/url]
   int n = DateTime.GetDayOfYear(x);   //returned day of year is correct
   double w = 360 / 365.242199;
   double a = w * (n + 10);
   double b = a + 1.9149659 * Math.sin((w/Rad) * (n-2));   // Rad = 57.3
   double c = (a - Math.atan(Math.tan(b/Rad) / Math.cos(23.43755447/Rad))) / 180;
   double eot =  720 * (c - Math.round(c));
   return eot;
}

Question being, am I totally mad? Can't see any difference, though.
If someone has got a minute...

Thanks & Cheers
 

alfcen

Well-Known Member
Licensed User
Longtime User
I am writing a library in Java with many other routines.

The same problem with this routine

B4X:
'Returns the density in km/s.
'p_m = planet mass in EU
'p_r = planet radius in EU
Public Sub PlanetDensity(p_m As Double, p_r As Double) As Double
    Dim vol As Double
    p_m = mass_earth * p_m
    p_r = radius_earth * p_r
    vol = (4 / 3) * cPI * Power(p_r,3)
    Return p_m / vol / Power(10,12)
End Sub

B4X:
/**
 * Returns the overall density in gr / cubic centimeter.
 * @param pMass planet mass in Earth units.
 * @param pRadius planet radius in Earth units.
 * @return Density
 */
public double PlanetDensity(double pMass, double pRadius) {
   pMass = CONST_MASS_EARTH_KG * pMass;
   pRadius = CONST_RADIUS_EARTH * pRadius;
   double vol = (4 / 3) * Math.PI * Math.pow(pRadius,3);
   return (pMass / vol) / Math.pow(10,12);
}
 

alfcen

Well-Known Member
Licensed User
Longtime User
To Informatix

You have uncovered a bloody beginner with Java.
Your advice fixed the second routine, PlanetDensity.

Thanks very much!!! I will take it as a habit from now on.
 

alfcen

Well-Known Member
Licensed User
Longtime User
To thedesolatesoul

Yes, Java has no SinD or CosD, in that I converted the degree values to radians by division by 57.3 (180/Math.PI).
 

alfcen

Well-Known Member
Licensed User
Longtime User
To Erel

With all these details you have to watch out when coding in Java,
we should consider ourselves extremely fortunate to have your B4A!!!
 

alfcen

Well-Known Member
Licensed User
Longtime User
Out of curiosity

Normally, that is what I am doing, Erel,

Your Simple Library Compiler made me curious and downloaded a copy of Eclipse and played with it because I like to learn and catch up with things. All to the sudden, I converted my B4A-made library to Java.

Comparing the libraries, I do not notice any difference in performance.
B4A is not error-prone, in that the B4A made library is more robust and can employ complex routines with far less code.
 
Top