What is the correct way to calculate the loop count of the optimizations?

Firstly, I've applied a four parameters Optimize() as below:

b1 = Optimize( "b1", 1.005, 0.995, 1.005, 0.0005 ); 
b2 = Optimize( "b2", 1.005, 0.995, 1.005, 0.0005 ); 
s1 = Optimize( "s1", 1.005, 0.995, 1.005, 0.0005 ); 
s2 = Optimize( "s2", 1.005, 0.995, 1.005, 0.0005 ); 

then I've used the following formula
to calculate the loop count for one Optimize():
(1.005-0.995)/0.0005+1 = 21;

So four parameters should be 212121*21 = 21^4 loops,
but AmiBroker showed me a warning message of "160000" loops,
and 160000 = 20^4 loops, not 21^4 loops.

Is there any mistake in my calculation above?

Thank you very much!
:smiley:

That is yet another case of people not understanding that decimal fractions are NOT used by binary computers.

Take Microsoft Excel and enter this
=1.005-0.995

into a cell, then RIGHT CLICK, select "Format" and change Numeric format to display at least 16 decimal digits.

You will see this:

image

If you divide that by 0.0005, you are not going to get 20, but 19.999999999

And if you add 1, you will get 20.9999999. Since maximum can't be exceeded, there are 20 steps, not 21 as you think.

Read this:

If you want exact number of steps you have to use integers like this:

b1 = 0.995 + Optimize( "b1", 0, 0, 20, 1 ); 

Alternatively you can add half of step amount to "max" value.

b1 = Optimize( "b1", 1.005, 0.995 + 0.0005/2, 1.005, 0.0005 ); 

(note that doing so is still subject to floating point rounding and values like 0.995 are NOT exact in floating point)

https://www.h-schmidt.net/FloatConverter/IEEE754.html

image

Also it happens that 1.005 is not exact and in fact true value is less than 1.005, see:

image

2 Likes

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.