我在下面有这个代码,我在这行收到溢出错误: s = s +(x Mod 10)[Do循环中的第一行] 为什么?我声明x和s是Double类型。添加两个双打,为什么这不起作用? 谢谢你的帮助。
from sqlalchemy.orm import aliased
aliased_student = aliased(Student)
aliased_school = aliased(School)
query = (
session.query(aliased_student, aliased_school)
.select_from(Person)
.join(aliased_student, Person.student)
.join(aliased_school, Student.school)
.filter(Person.id == 1))
答案 0 :(得分:3)
问题是VBA mod
运算符强制其参数为整数(如果它们不是这样)。正是这种隐含的强制导致了溢出。请参阅此问题:Mod with Doubles
点击编辑: 根据您的评论,您希望能够将数字加在一个较大的整数中。以下功能可能有所帮助:
Function DigitSum(num As Variant) As Long
'Takes a variant which represents an integer type
'such as Integer, Long or Decimal
'and returns the sum of its digits
Dim sum As Long, i As Long, s As String
s = CStr(num)
For i = 1 To Len(s)
sum = sum + Val(Mid(s, i, 1))
Next i
DigitSum = sum
End Function
以下测试子显示如何使用 来正确获取999999 ^ 3中的数字总和:
Sub test()
Dim x As Variant, y As Variant
Debug.Print "Naive approach: " & DigitSum(999999 ^ 3)
y = CDec(999999)
x = y * y * y
Debug.Print "CDec approach: " & DigitSum(x)
End Sub
输出:
Naive approach: 63
CDec approach: 108
自999999 ^ 3 = 999997000002999999以来,只有第二个结果是准确的。第一个结果只是双精度999999 ^ 3 = 9.99997000003E + 17
的字符串表示中的数字之和