Everything was defined as Single. Stupid VBA tutorials that always tell you to conserve memory.
Agreed: "stupid"!
Arguably, there is a performance issue, too.
In modern computers, single floating-point and double floating-point arithmetic operations take the same amount of time. The fact is: Intel-compatible CPUs use the same
80-bit floating-point arithmetic in both cases. The difference comes only when the result is stored into memory.
But there might be more "cache misses", resulting in more "real memory" accesses, which are much slower.
Even though that could result in noticeable performance consequences in some algorithm (e.g.
billions of calculations), it is not worth the arithmetic inaccuracy, IMHO.
(+0.0001) didn't cause issues.
It certainly can. Adding 0.0001 might cause unintended rounding up.
Arguably, theoretically, it might make some sense to add a
relatively infinitesimal small value; for example, 2^-52 for values in the range of 1.52.
The reason is: since 0.005 cannot be represented exactly in binary, we might wonder if 1.515 would be rounded to 1.52, since 1.515 - 1.51 is actually less than 0.005 (about 0.00499999999999989) [3]. (But it is rounded up, as intended!
[3] That is the 64-bit floating-point difference. It might be more than 0.005 in the 80-bit floating-point representation. I do not have tools for determining that.
But the problem is: determining what is "relative infinitesimally small". It varies with the magnitude of the value. It's certainly doable; but it is not worth the effort, IMHO.
About that, what is real difference between round(), and WorksheetFunction.Round ? I understand that second comes from Excel itself, and first is VBA function, that exists outside Excel. But is there any downside/consequence using WorksheetFunction.Round?
As I said before, the "real difference" between the two is: VBA Round rounds "half to even" [1], whereas Excel ROUND rounds "half away from zero" [2].
[1] "Round half to even" is also called banker's rounding, which is a misnomer because most modern banks now use "round half away from zero", in my experience.
[2] I was wrong to write "round half up" before. The difference is with negative numbers: Excel ROUND(-1.525,2) results in -1.53 (away from zero), not -1.52 ("up", meaning: next higher integer).
Some government and industry standards might require the use of "round half to even". On the other hand, some uses with Excel spreadsheet require that we round the same way as Excel ("round away from zero"). And most people expect the latter, IMHO.
Presumably, WorksheetFunction.Round is implemented in machine language, just like VBA Round. In particular, WorksheetFunction.Round does
not perform the operation in the Excel thread.
However, WorksheetFunction.Round does take about 70 times longer than VBA Round on my computer (YMMV).
Nevertheless, again, I would make the choice based on which yields the "correct" (intended) result, not based on performance. There is no benefit to getting the "wrong" answer fast, IMHO.
[EDIT] FYI, I "always" use WorksheetFunction.Round, except when I'm too lazy to type the extra characters.
If there is not I can finally drop clean <acronym title="visual basic for applications">VBA</acronym> functions and start using WorksheetFunction since 90% of my <acronym title="visual basic for applications">VBA</acronym> coding is done in Excel.
Oh, I see your point now: Round in Excel v. some other Office(?) application. I never considered that since I cannot imagine being able to use the same macro in both contexts. For example, surely Range(...)=... applies only to Excel and perhaps similar spreadsheet applications.