I am working on a spreadsheet that is performing calculations on very small numbers, on the order of 1E-14. I had a draft of my process that leverages the spreadsheet and does not store any data in VBA variables. I do not like that approach and wanted to take the entire process behind the scenes into VBA.
What I am noticing is I cannot get the two approaches to match. Same operations different results. I am working with Excel 2013
An example in a cell
=B42*COS(2*PI()*E$41*A42)
or if I plug in values taking all figures reported i get the following
0.156434465*Cos(2*3.14159265358979*0.012*0.05)
this results in
1.56433353400222E-01
If I do the same thing in VBA
gsum = Round(data(i + 1, 1), 10) * Cos(2 * pi * Freq * i * dt)
or
0.156434465*Cos(2*3.14159265358979*0.012*1*0.05)
i get a different answer
Watch:Round(data(i + 1, 1), 10) * Cos(2 * Pi * Freq * i * dt):
0.156433353359992 : Variant/Double : Calculation.realFT
I am not sure how to combat this. Somehow I lose a little each time. Everything on the VBA side is declared of type double except for I.
Any ideas on how i can get VBA to match the spreadsheets precision?
I know the differences are small but I am doing a lot of operations and they add up. Also I am confined to Excel for the application.
UPDATE
So I was looking at this some more and i have narrowed that the problem to the muliplication step between the Cosine output and the cell value. In both the excel and Excel VBA versions I can see that they are both the same but I am stuck with the following
In Excel
0.999992893893247000000*0.156434465= 1.56433353400222E-01
In Excel VBA
0.999992893893247000000*0.156434465= 1.56433353359991861557855E-01
who is right?