-3

浮動小数点数を使用してPythonで除算を試みましたが、浮動小数点数を丸めようとしても間違った結果が得られましたが、うまくいきませんでした。Pythonが大きな浮動小数点数の除算を行う方法は他にありますか?

>>> div = 1.45751734864e+15/30933
>>> print div
47118525478.9

Javaでも同じ

>>> double div = 1.45751734864e+15/30933;
>>> System.out.println(div);
4.711852547893835E10
4

3 に答える 3

2

Python と Java の両方の結果は正しいです。

パイソン:

47118525478.9

ジャワ

4.711852547893835E10

ただし、Java では、数値は指数表記形式で出力されます。したがって、次と同等です。

4.711852547893835 * 10 ^10 = 47118525478.9835

Python の出力を指数表記形式で出力したい場合は、String formatを使用します。

>>> div = 1.45751734864e+15/30933
>>> print '{:e}'.format(float(div))
4.711853e+10
于 2013-04-15T11:39:49.893 に答える
1

decimalモジュールを使用して精度を上げることができます。

>>> from decimal import *
>>> getcontext().prec = 30
>>> Decimal(1.45751734864e+15) / Decimal(30933)
Decimal('47118525478.9383506287783273527')

読む: http://docs.python.org/2/tutorial/floatingpoint.htmlおよびhttp://docs.python.org/2/library/decimal.html

于 2013-04-15T11:29:41.697 に答える
0

Python と Java によって返される数値は同じです。

Python 2.7.3 を使用した IPython:

In [1]: 1.45751734864e+15/30933
Out[1]: 47118525478.93835

In [2]: 1.45751734864e+15/30933.0
Out[2]: 47118525478.93835

In [3]: 1.45751734864e+15/30933.0-4.711852547893835E10
Out[3]: 0.0

パイソン 3.3:

Python 3.3.0 (default, Mar 22 2013, 20:14:41) 
[GCC 4.2.1 Compatible FreeBSD Clang 3.1 ((branches/release_31 156863))] on freebsd9
Type "help", "copyright", "credits" or "license" for more information.
>>> 1.45751734864e+15/30933
47118525478.93835
>>> 1.45751734864e+15/30933-4.711852547893835E10
0.0
>>> 
于 2013-04-15T11:25:51.127 に答える