- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我在 Django 应用程序中使用 Decimal 对象,发现了这个奇怪的错误:
ipdb> decimal.Decimal(10) % 1
Decimal('0')
ipdb> decimal.Decimal(100) % 1
*** decimal.InvalidOperation: [<class 'decimal.DivisionImpossible'>]
ipdb> decimal.Decimal(150) % 1
*** decimal.InvalidOperation: [<class 'decimal.DivisionImpossible'>]
ipdb> decimal.Decimal(79) % 1
Decimal('0')
ipdb> decimal.Decimal(100.1) % 2
Decimal('0.10')
ipdb> decimal.Decimal(1000) % 2
*** decimal.InvalidOperation: [<class 'decimal.DivisionImpossible'>]
更神秘的是,在数字变得非常大之前,这不会发生在 ipython 中:
In [23]: decimal.Decimal(10**27) % 1
Out[23]: Decimal('0')
In [24]: decimal.Decimal(10**28) % 1
---------------------------------------------------------------------------
InvalidOperation Traceback (most recent call last)
<ipython-input-24-6ceaef82d283> in <module>()
----> 1 decimal.Decimal(10**28) % 1
InvalidOperation: [<class 'decimal.DivisionImpossible'>]
请注意,错误不仅限于 ipdb:我发现这个是因为 Decimal(380) % 1 破坏了我的 django 应用程序。
documentation描述这个错误说:
Division impossible
This occurs and signals invalid-operation if the integer result of a divide-integer or remainder operation had too many digits (would be longer than precision). The result is [0,qNaN].
有什么想法吗?
最佳答案
我想我明白了。
查看 source code ,我发现了这个:
# catch most cases of large or small quotient
expdiff = self.adjusted() - other.adjusted()
if expdiff >= context.prec + 1:
# expdiff >= prec+1 => abs(self/other) > 10**prec
return context._raise_error(DivisionImpossible)
if expdiff <= -2:
# expdiff <= -2 => abs(self/other) < 0.1
ans = self._rescale(ideal_exponent, context.rounding)
return ans._fix(context)
在我的 django 应用程序中,对 prec 进行了调整:
decimal.getcontext().prec = 2
这对我来说还是有点不对,因为:
In [39]: decimal.getcontext().prec + 1
Out[39]: 3
In [40]: decimal.Decimal(100).adjusted() - decimal.Decimal(0).adjusted()
Out[40]: 2
所以看起来 100 仍然在它检查的范围内(即 2 < 3
),但我非常有信心这就是问题的根源。如果有人能为我阐明为什么图书馆这样做,我很乐意更好地理解它。
关于python - decimal.Decimal(n) % 1 返回 InvalidOperation, DivisionImpossible for all n >= 100,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38924369/
我是 Python 编程的新手,最近我一直在学习数据类型的某些方面。正如我所读,decimal.Decimal() 方法创建了一个小数对象; decimal.Decimal.from_float()
这是我今天在 filmmaster.com: 遇到的错误 PicklingError: Can't pickle : it's not the same object as decimal.Decim
简单的问题 - 为什么 Decimal 类型定义这些常量?何必呢? 我正在寻找一个原因,为什么这是由语言定义的,而不是可能的用途或对编译器的影响。为什么首先把它放在那里?编译器可以像 Decimal.
我在用 Python 编码为 JSON 时遇到问题,特别是 decimal.Decimal 值。我正在使用它为 Google App Engine 应用程序输出 JSON。 为了避免 Python 中
我在 Django 项目中有一些 python 代码曾经可以正常工作。托管该项目的服务器丢失了,我不得不将代码复制到新服务器上。现在,我收到一个似乎毫无意义的错误。 我的一个 python 文件中包含
我发现生成具有特定数量 的 decimal.Decimal 数字的最佳方法 significant figures 是用来初始化下面的变量 kluge 的: import decimal THIS_I
我在应用空合并运算符时遇到以下错误。 private decimal _currentImpulseId; // ... later on used in public property getter
我想使用 decimal.Decimal 对转换后的数字字符串进行算术运算。无论转换后的数字字符串有多少小数,我都希望返回有两位小数。 在这种特殊情况下,为什么两位小数精度返回一位小数,为什么三位小数
我想使用 decimal.Decimal 对转换后的数字字符串进行算术运算。无论转换后的数字字符串有多少小数,我都希望返回有两位小数。 在这种特殊情况下,为什么两位小数精度返回一位小数,为什么三位小数
是什么导致 sum 始终为 null? object[] data = new object[] { 2.2M, 3m, 1, "string", true,
我似乎失去了很多 float 的精度。 例如我需要求解一个矩阵: 4.0x -2.0y 1.0z =11.0 1.0x +5.0y -3.0z =-6.0 2.0x +2.0y +5.0z =7.0
这个问题在这里已经有了答案: Why python decimal.Decimal precision differs with equable args? (2 个答案) 关闭 5 年前。 我知道
当我尝试转换 Task 时出现此错误至 decimal : Could not load file or assembly 'EntityFramework, Version=5.0.0.0, Cul
我听到有人说在 C# 中,大写十进制比小写十进制使用更多的内存,因为小写十进制被解析为小写十进制并且需要内存。 这是真的吗? 最佳答案 没有。 decimal 只是 System.Decimal 的别
我有一个对象,它是 dict、list、常规数据类型和 decimal.Decimal 的嵌套组合。我想用 PyMongo 将这个对象插入到 MongoDB 中。 PyMongo 拒绝插入 Decim
sdr 是我的 sqldatareader,我想检查小数类型的 curPrice 值是否为空。 inrec.curPrice = sdr.IsDBNull(7) ? (十进制?)空:sdr.GetDe
变量 'a' 的类型可以是 - int/float/decimal.Decimal(但不是字符串) 我想检查它是否是 decimal.Decimal 类型。 以下作品: import decimal
我正在使用一个在函数调用中使用一些十进制类型参数的库。但是我有 numpy.int32 类型变量。当我将它传递给函数调用时,出现以下错误: TypeError: conversion from num
考虑以下代码: public class DecimalWrapper { public static implicit operator DecimalWrapper
考虑以下代码: public class DecimalWrapper { public static implicit operator DecimalWrapper
我是一名优秀的程序员,十分优秀!