因为oct文件是经过编译的函数,所以它们打开了通过不小心的函数调用或内存故障刷新Octave的可能性。非常重要的是,每个函数都有足够的参数检查级别,以确保Octave表现良好。
如前所述,最低要求是在使用输入参数之前检查输入参数的数量,以避免引用不存在的参数。然而,在某些情况下,这可能还不够,因为底层代码施加了进一步的约束。例如,如果输入参数不是整数,或者其中一个参数为零,或者输入是复杂的并且需要实数,则外部函数调用可能是未定义的。因此,oct文件通常需要额外的输入参数检查。
Octave中有几个函数可用于参数检查。其中包括的方法octave_value类is_real_matrix, is_numeric_type,等等(详见ov.h). 通常,通过了解Octave m文件语言,您可以猜测相应的C++子程序会是什么。此外,还有一些更专业的输入验证函数,下面将演示其中的一些函数。
#include <octave/oct.h>
DEFUN_DLD (paramdemo, args, nargout, "Parameter Check Demo")
{
if (args.length () != 1)
print_usage ();
NDArray m = args(0).array_value ();
double min_val = -10.0;
double max_val = 10.0;
octave_stdout << "Properties of input array:\n";
if (m.any_element_is_negative ())
octave_stdout << " includes negative values\n";
if (m.any_element_is_inf_or_nan ())
octave_stdout << " includes Inf or NaN values\n";
if (m.any_element_not_one_or_zero ())
octave_stdout << " includes other values than 1 and 0\n";
if (m.all_elements_are_int_or_inf_or_nan ())
octave_stdout << " includes only int, Inf or NaN values\n";
if (m.all_integers (min_val, max_val))
octave_stdout << " includes only integers in [-10,10]\n";
return octave_value_list ();
}
其使用示例如下:
paramdemo ([1, 2, NaN, Inf])
⇒ Properties of input array:
includes Inf or NaN values
includes other values than 1 and 0
includes only int, Inf or NaN values
版权所有 © 2024-2025 Octave中文网
ICP备案/许可证号:黑ICP备2024030411号-2