如何在Python中获得双重精度值等于Java Float.valueof()
我正在尝试将Java计算迁移到Python,在那里我看到浮点值计算结果的差异
:例如:在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);
}
}
结果-1.61917542E10(打印双重值),
但在Python Float()中,在Python float()方法中返回相同的值16191753860.0
如何 返回。在Python中获取双重精确计算以获得相同的结果
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Python
float
是双精度浮点号。实际上,您的Java示例使用了单个Precisionfloat
,这就是为什么它以科学符号格式化(因为它已经超过了整数精度)。如果您想打印出python中浮子的科学符号,无论它是否越过精确边界,请使用
e
格式。f'{lg_val:e}'
- >'1.619175E+10'
或使用Java中的双精度号,请使用
The python
float
is a double precision floating point number. It is in fact your Java example that is using the single precisionfloat
, 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
您可以使用
numpy.float64()
。在您的示例中,You can use
numpy.float64()
. In your example,