View Full Version : Floating point question, exactness of integers

08-02-2018, 07:03 AM
This should be simple if you know F.P. but at the moment I seem to be unable to form simple thoughts so here's your big moment:

If you have a double dVal, and it is known that these "conditions" apply
- dVal has been assigned an integer (negative, zero, or positive);
dVal has NOT been assigned 11.00000000000002 or such
- The absolute value of dval is 1e15 or less
- No potential perversion has been introduced by addition or subtraction, ROUND(), MOD(), or anything else that operated on dVal, subsequent to its original assignment from a string, e.g.

dim dVal as double, sStr as string
sStr = "11.0" ' or "-1000" or "0" or "1000000"
dVal = sStr 'That's all, no further assignments to dVal

then the question is, is it assured that dVal is an exact integer? That is, irrespective of Microsoft's mysterious "'kind' help with F.P. slop" or not, that debug.print 11=dval gives True?

Then if so (that integral assignment is always exact under the "conditions" above) can this be extended to rely upon, that as long as the range (-1e15, 1e15) is not breached, that integer addition, subtraction and multiplication will still be exact? E.g.,

dVal = 11
dVal = dVal - 1e14
dVal = dVal + 2e14
dVal = dVal * 2
debug.print 2e14 + 22 = dVal 'True?

I know that this is clouded by our protective mommy Microsoft providing slop correction so that you may [or may not] get "numeric comparison equality" when in fact the F.P. is not exact [e.g. does 1 ?= 3*(1/3)}, but can I assume that with integers, that the representation is always exact, in a Double variable, given the "conditions" above; or further than that, with nothing more than addition and subtraction and multiplication with other integers, as long as range (-1e15, 1e15) is not breached?

P.S. I may have grazed straying slightly from VBA here, if any answers need to be qualified as to scope.

P.P.S. MOD() was "damaged" in the 2010 version; I would contend that some developer didn't understand F.P., and the review process lamely greenlit a harmful change. I can elaborate if asked.