2012-11-18 15 views
8

デフォルト浮動小数点数の精度を別のものに変更するにはどうすればよいですか?私は計算が必要なので、これが必要です。浮動小数点のデフォルト精度を上げる必要があります。浮動小数点数の精度を高める

+1

Pythonとnumpyはデフォルトで倍精度浮動小数点を使用します。不十分な「正確さ」を持っていると思わせる原因は何ですか? – talonmies

+1

あなたはhttp://docs.python.org/2/library/decimal.htmlを見ましたか? –

+1

あなたの計算に 'decimal'を使うことができますが、' numpy'のパフォーマンス上の利点を失います。私の知る限りでは、このトレードオフから抜け出せません。プロセッサがネイティブで動作できる最も正確なデータ型は、numpyが使用するものです。 – millimoose

答えて

7

私は最近この問題に対処しなければならず、mpmathは完璧でした。これはBSDライセンスのもとでは純粋なPythonです。

Mpmath is a pure-Python library for multiprecision floating-point arithmetic. It provides an extensive set of transcendental functions, unlimited exponent sizes, complex numbers, interval arithmetic, numerical integration and differentiation, root-finding, linear algebra, and much more. Almost any calculation can be performed just as well at 10-digit or 1000-digit precision, and in many cases mpmath implements asymptotically fast algorithms that scale well for extremely high precision work

それはあまりにも遅くはない、とインストールされている場合、それは(C-coded Python extension modules that support fast multiple-precision arithmeticを)gmpyライブラリを活用することができます。

+0

scipy.integrate.odeintを使用する「TypeError:配列を必要な型に安全にキャストできません」=(scipy.integrate.odeintがすべてを浮動小数点に変換するようです。 – user983302

+1

しかし、それは独自のodeソルバーです - [odefun](http ://mpmath.googlecode.com/svn/trunk/doc/build/calculus/odes.html)OK、これを解決策にしましょう。 – user983302

+0

[unlimitedness]に関するテキストを更新しました。(http://mpmath.org/doc/ current/technical.html#representation-of-numbers): "Mpmathは仮数と指数の両方に任意の精度の整数を使用するため、数値はコンピュータのメモリで許容される大きさになる可能性があります。非常に大きな数字。 –

2

もっと正確にしたいのであれば、bigfloatパッケージを使用することをお勧めします(これはそのためです)。また、Decimalクラスを参照することもできます。

+0

scipy.integrateとbigfloatをお互いに扱うことはできますか?現在、私は "TypeError:配列は安全に必要な型にキャストすることはできません" scipy.integrate.odeintにBigFloat引数を渡すとき – user983302

+0

@ user983302多分[this](http://stackoverflow.com/questions/7770870/numpy-array-with -dtype-decimal)が役に立ちます。 – arshajii

関連する問題