Board logo

标题: [MC源码] How to prevent division by zero errors [打印本页]

作者: 龙听    时间: 2020-11-28 07:48     标题: How to prevent division by zero errors

This example shows how to correct division by zero errors in Tradestation Easy language or MultiCharts

Division by zero is a frequent problem experienced in programming. The answer is always infinity, so we have to prevent anything getting divided by zero in the first place.

There are two methods of doing this.

Method 1

If value1 = 0 then value1=value1+0.0000000001;

So we simply add a tiny number to it, which is so tiny it will not make too much difference to the outputs.

Method 2

If value1 <> 0 then value2 = value3 / value1

This forces the computer to ask if the value1 is 0 or not before doing its calculations. If it is 0 it will return the default value that was assigned to value1 in the variables when you created it.

Voila.




欢迎光临 龙听期货论坛 (http://www.qhlt.cn/) Powered by Discuz! 7.2