1

いくつかの計算から得られた非常に正確な数値を調べているコードがあり、そのコードは元の数値(計算前)に基づいて正しい精度を見つけようとします。次に、以下を使用して丸めを適用します。

with localcontext() as ctx:
    ctx.prec = 5 # simplification for the sake of this example
    my_figure = +my_figure

そして、my_figure がゼロに等しくない限り、すべてが素晴らしいです。これはゼロにはまったく影響しないため、以前と同じ精度で出力されます (この例の 5 ではありません)。

my_figure = Decimal('0.0000...') # 0E-30, it comes from some calculations, not assigned like that
with localcontext() as ctx:
    ctx.prec = 5 # simplification for the sake of this example
    my_figure = +my_figure
    print my_figure # I get 0E-30, no rounding applied, I was expecting 0.0000

ゼロにも影響を与える方法でこれを行う適切な方法はありますか?

4

1 に答える 1