# Problem with cosine function

Hi everyone,

I’m trying to formulate constraints involving sine and cosine functions applied to the difference of angles in buses of electrical power systems. I identified a problem with calculating the cosine of the difference between two angles when they correspond to the same variable.

I reduced the model to a simple formulation in which trying to calculate `y=cos(t-t)` leads to y=0, instead of y=1. However, using `y=cos(t-t*0.999999)` leads to the correct value y=1.

I’m using AMPL Version 20230228 with Artelys Knitro 13.2.0 and CONOPT 3.17A. Please find an example of this behavior in the model below. Alternatives 1 and 2 produce y=0, while alternative 3 results in y=1.

Do you have any idea of why this is happening?

``````reset;
option solver conopt;
option solver knitro;
option knitro_options 'feastol=1e-15';

var t;
var y;

minimize calculation: y^2;

subject to def:
y = cos(t-t);                       # alternative 1
# y = cos(t-t*0.99999999999999999);   # alternative 2
# y = cos(t-t*0.9999999999999999);    # alternative 3

solve calculation;
display y;
``````

Thanks for your example, which has been reported. It might show a rare bug. Terms like `cos(bus_angle[k]-bus_angle[m])` are common in the power flow models that we’ve seen, but those models are constructed in a way that avoids the incorrect result that that your model encounters.

Note that, using the fixed precision of the computer, 1 – 1e-17 cannot be distinguished from 1:

``````ampl: print 0.99999999999999999;
1

ampl: print 0.9999999999999999;
0.9999999999999999
``````

Thus alternative 2 generates the same problem instance as alternative 1, while alternative 3 is different.