I am trying to migrate a java calculation to python, where I see the difference in the float value calculation results
for example: in Java
public class Main
{
public static void main(String[] args) {
long lg_val = Long.parseLong("16191753860");
float fg_val = Float.valueOf(lg_val);
System.out.println(fg_val);
}
}
result - 1.61917542E10 (prints the double value)
but in python float() method returns same value 16191753860.0
How to get the double-precision calculation in python to get the same results
CodePudding user response:
The python float
is a double precision floating point number. It is in fact your Java example that is using the single precision float
, which is why it formats in scientific notation (as it has gone above integer precision).
If you want to print out the scientific notation of a float in python regardless of if it's crossed over the precision boundary, use the e
format.
f'{lg_val:e}'
-> '1.619175e 10'
Or to use a double precision number in Java instead, use
double fg_val = Double.valueOf(lg_val);
CodePudding user response:
You can use numpy.float64()
. In your example,
import numpy as np
np.float64(lg_val)