There are two (like the same) algorithms with this common block:

double U_i = 1043.928480752930; double Am0 = 0; double Am1 = 0; double per_koef_usilen=48.4; qDebug()<< "Тут расчет"; 

Option 1:

 double Y = pow((double)10.0,(double)(per_koef_usilen/20.0)); qDebug("%.14f",Y); Am0 = (double)(U_i/Y); qDebug("%.14f",Am0); Am1 = U_i/pow(10.0,(per_koef_usilen/20.0)); qDebug("%.14f",Am1); 

I get the result Am0 = 3.96890538899297 Am1 = 3.96890538899297
There are two of them (Am0, Am1) to check Y.
But for some reason, if you use the Y array, the result is different:

Option 2:

 int K; double *Am0 = 0; if(Am0) delete []Am0; Am0 = new double[obm->stor->K]; memset(Am0, 0, obm->stor->K*sizeof(double)); Am0[k] = obm->stor->U_i[k]/pow(10.0,(obm->stor->per_koef_usilen/20.0)); qDebug("%.14f",Am0[k]); 

Result: Am0[1] = 3.96537540362345 . The problem is that the divergence is already on the third decimal place , if at least on the fifth , it would be normal, but it looks like a completely different number.

My goal is to understand why Am0 and Am0 are not equal to Am0[1] and why using the same variables (the only difference is that Am0 is an array) I got a different result.

Closed due to the fact that off-topic participants Abyx , insolor , aleksandr barakin , avp , D-side 30 Mar '16 at 14:50 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reasons:

  • "The question is caused by a problem that is no longer reproduced or typed . Although similar questions may be relevant on this site, solving this question is unlikely to help future visitors. You can usually avoid similar questions by writing and researching a minimum program to reproduce the problem before publishing the question. " - aleksandr barakin, D-side
  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - Abyx, insolor
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • Sorry, you do not bother to describe the code and the desired behavior? It is difficult to answer questions like "I have a code <some confusing code without explanation>, it does not work, help!" - AivanF.
  • But I described there is the first code "the result of Am0 3.96890538899297 Am1 3.96890538899297" and the second "sad Am0 [1] = 3.96537540362345." . Am0 and Am0 [1] do not converge although they are the same. - timob256
  • one
    such code is double *Am0; if(Am0) delete []Am0; double *Am0; if(Am0) delete []Am0; - this is a scary ub. Collect the minimum, compiled example and then it will be possible to discuss. - KoVadim
  • What is UB ??? - timob256
  • one
    You have already been told about the terrible mistakes in working with memory. But, nevertheless, print obm->stor->per_koef_usilen and obm->stor->U_i[k] before the calculations and make sure that there are the same numbers as when calculating Am0 and Am1 - avp

1 answer 1

Sorry guys, the code is completely working just in my code, somewhere I work with OpenGL, so it cuts. and so the code is fine, arrays work fine

 #include <QCoreApplication> // тут библиотеки СИ языка #include <iostream> // для оператора cout #include <cmath> // библ отвеч за квадрат в СИ #include <math.h> // библ отвеч за квадрат в СИ #include <stdio.h> #include <QDebug> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); double U_i = 1043.928480752930; double Am0 = 0; double Am1 = 0; double per_koef_usilen=48.4; qDebug()<< "Тут расчет"; double Y = pow((double)10.0,(double)(per_koef_usilen/20.0)); qDebug("%.14f",Y); Am0 = (double)(U_i/Y); qDebug("%.14f",Am0); Am1 = U_i/pow(10.0,(per_koef_usilen/20.0)); qDebug("%.14f",Am1); int k=10; double *U_i_t = 0; if(U_i_t) delete []U_i_t; U_i_t = new double[k]; memset(U_i_t, 0, k*sizeof(double)); double *Am3 = 0; if(Am3) delete []Am3; Am3 = new double[k]; memset(Am3, 0, k*sizeof(double)); FILE *fid=fopen("P1Am.txt","rt"); if(fid) { double val=0; for(int i=0; i<k; i++) { fscanf(fid,"%lf",&val); U_i_t[i] = val; qDebug("%.14f",U_i_t[i]); } } for (int i =0; i< k; i++) { Am3[i] = U_i_t[i]/pow(10.0,(per_koef_usilen/20.0)); qDebug("%.14f",Am3[i]); } return a.exec(); } 

There are 10 variables in the P1Am.txt file

 1043.928480752930 1023.487233880130 1014.657596748670 1132.028858770790 1012.557090294320 1006.203887562610 1041.219702992490 1032.005532206760 1113.350002788500 1203.141773433670 

In general, I was wasted on arrays.

  • Checks and freeing memory in these and similar lines are not needed double *U_i_t = 0; if(U_i_t) delete []U_i_t; U_i_t = new double[k]; double *U_i_t = 0; if(U_i_t) delete []U_i_t; U_i_t = new double[k]; . you have not yet allocated a memory, therefore there is nothing to free - gil9red