比较操作,“ If Then”循环中的“> =”语句首先正确评估2-3次,然后错误地评估,然后在For Next外循环的最后一个循环中正确评估。当我逐步调试循环时,正在比较的变量位于数组中,并且具有正确的值。我很困惑为什么会这样。
曾想过更改数组变量的数据类型,但我认为我必须对Excel VBA数组使用“变量”。
%module logchar
%{
#include "logchar.h"
%}
%typemap(in) (const int dim_count, int *shape) {
if (!PyList_Check($input)) {
PyErr_SetString(PyExc_ValueError, "Expecting a list");
SWIG_fail;
}
$1 = PyList_Size($input);
$2 = new int[$1];
for (int i = 0; i < $1; i++) {
PyObject *s = PyList_GetItem($input,i);
if (!PyInt_Check(s)) {
delete[] $2;
PyErr_SetString(PyExc_ValueError, "List items must be integers");
SWIG_fail;
}
$2[i] = PyInt_AsLong(s);
}
}
%typemap(freearg) int *shape {
if ($1) {
delete[] $1;
}
}
%include "logchar.h"
%template(quarterTensor) logchar::Tensor<logchar::quarter>;
%template(floatTensor) logchar::Tensor<float>;
%template(doubleTensor) logchar::Tensor<double>;
%include exception.i
%exception {
try {
$function
} catch(const char* e) {
SWIG_exception(SWIG_RuntimeError, e);
}
}
%include "carrays.i"
%array_class(int, intArray);
答案 0 :(得分:0)
这可能是浮点错误,因为计算机无法在固定空间中准确表示数字。但是,通常该错误很小,不会造成问题。要了解更多信息,请访问:https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html
以下示例显示了浮点错误: 该子计算2的平方根,并用2种不同的方式计算总和。
sub FloatingPointError()
Dim x As Double, y As Double, n As Integer
x = 0
n = 100
y = n * (2 ^ 0.5) 'expected result is 1,4142135623731 * 100 = 141,42135623731
For i = 1 To n
x = x + (2 ^ 0.5) 'Calculate square root of 2 and add to the previous value
Next i
Debug.Print "value of x: "; x
Debug.Print "value of n * sqrt(2): "; n * (2 ^ 0.5)
If y - x = 0 Then
Debug.Print "They are equal"
Else
Debug.Print "They are not equal"
End If
End Sub
该子项的输出显示:
value of x: 141,421356237309
value of n * sqrt(2): 141,42135623731
They are not equal
但是,局部变量选项卡有时可以显示数字相等,因为它们是四舍五入的。