Translating Java code to Python
Question:
I’m trying translate this Java code to Python:
double grades = -32.33f;
System.out.println("grades: "+grades);
double radians = Math.toRadians(grades);
System.out.println("rad: "+radians);
long radiansUp8 = 0xFFFF_FFFFL & (long)(radias * 100_000_000);
System.out.println("rad: "+radiansUp8);
String hex = Long.toHexString(radiansUp8);
System.out.println("signed to hex: "+hex);
I’ve done this:
import math
grades = -32.33
radians = math.radians(grades)
print radians
radians = radians * 100000000
print radians
hexValue = hex(int(radians))
print hexValue
But I’m not getting the same result. The Java code works fine, but in Python not. This line in particular I’m not sure how translate to Python, maybe that is the problem:
long radiansUp8 = 0xFFFF_FFFFL & (long)(radianes * 100_000_000);
The (correct) output for the Java code is:
grades: -32.33000183105469
rad: -0.5642649791276998
rad: 4238540799
signed to hex: fca2ffff
The (incorrect) output for the Python code is:
grades: -32.33
radians: -0.56426494717
radians: -56426494.717
signed to hex: -0x35cfffe
Answers:
The translation for that line into Python is:
radians = 0xffffffff & long(radians * 100000000)
This gives (roughly!) the answer you expect:
>>> 0xffffffff & long(math.radians(-32.33) * 100000000)
4238540802L
What the line does is multiply the number of radians by 100,000,000 (giving -56426494.71697667
), convert the result to a long
integer (-56426494L
) then do a “bitwise and” (see e.g. https://wiki.python.org/moin/BitwiseOperators) between that and 0xffffffff
. Your current code only performs the first of these three steps.
For a simple example of what bitwise and does:
>>> '{:04b}'.format(int('1010', 2) & (int('0111', 2)))
'0010'
For each binary digit, the output digit is 1
only if both input digits are 1
.
This kind of code should be translatable by the AgileUML translator:
https://github.com/eclipse/agileuml/blob/master/translators.zip
It abstracts the language-specific aspects (like Java number formats) to a language-independent form before generating Python.
I’m trying translate this Java code to Python:
double grades = -32.33f;
System.out.println("grades: "+grades);
double radians = Math.toRadians(grades);
System.out.println("rad: "+radians);
long radiansUp8 = 0xFFFF_FFFFL & (long)(radias * 100_000_000);
System.out.println("rad: "+radiansUp8);
String hex = Long.toHexString(radiansUp8);
System.out.println("signed to hex: "+hex);
I’ve done this:
import math
grades = -32.33
radians = math.radians(grades)
print radians
radians = radians * 100000000
print radians
hexValue = hex(int(radians))
print hexValue
But I’m not getting the same result. The Java code works fine, but in Python not. This line in particular I’m not sure how translate to Python, maybe that is the problem:
long radiansUp8 = 0xFFFF_FFFFL & (long)(radianes * 100_000_000);
The (correct) output for the Java code is:
grades: -32.33000183105469
rad: -0.5642649791276998
rad: 4238540799
signed to hex: fca2ffff
The (incorrect) output for the Python code is:
grades: -32.33
radians: -0.56426494717
radians: -56426494.717
signed to hex: -0x35cfffe
The translation for that line into Python is:
radians = 0xffffffff & long(radians * 100000000)
This gives (roughly!) the answer you expect:
>>> 0xffffffff & long(math.radians(-32.33) * 100000000)
4238540802L
What the line does is multiply the number of radians by 100,000,000 (giving -56426494.71697667
), convert the result to a long
integer (-56426494L
) then do a “bitwise and” (see e.g. https://wiki.python.org/moin/BitwiseOperators) between that and 0xffffffff
. Your current code only performs the first of these three steps.
For a simple example of what bitwise and does:
>>> '{:04b}'.format(int('1010', 2) & (int('0111', 2)))
'0010'
For each binary digit, the output digit is 1
only if both input digits are 1
.
This kind of code should be translatable by the AgileUML translator:
https://github.com/eclipse/agileuml/blob/master/translators.zip
It abstracts the language-specific aspects (like Java number formats) to a language-independent form before generating Python.