Here is the solution for integers (represented as floating arrays, if you want to do floating point arithmetic, of course). The variable replacement_value represents whatever value you think is appropriate in the circumstances. Something like 1e-6 often works fine.
IDL> c = a / (b + replacement_value * (b EQ 0))
For floating point values, where the number zero cannot be assumed to be exactly zero, Jean Hasban suggests this variation,
IDL> c = a / (b + replacement_value * (ABS(b) LT epsilon))
where epsilon is usually taken to be something like the smallest number that can be represented on your computer, or:
IDL> epsilon = (Machar()).eps
IDL> Print, epsilon
1.19209e-007
Other users just prefer to get the error. Then, at least they know what is going on.
本文介绍了一种在IDL编程中处理除数为零的情况的方法。通过使用替代值或基于浮点数精度的检查来避免运行时错误。这种方法确保了程序的稳定性和准确性。
171万+

被折叠的 条评论
为什么被折叠?



