r/visualbasic • • Feb 01 '21

VBA - Could someone please explain the following please (Backslash dividing and floats/doubles)

So, the problem i have is that i had a piece of code that would divide purchase units from stock units.

732ml was the incoming stock, each purchasing unit is 146.4ml.

So i applied the following formula to qualifying incoming purchase qty:-

int(stkQty/purUnits)

i.e.

int(732/146.4) = Expecting a '5'

This doesn't return a 5 as expected, but a 4. I can understand that casting it as an Integer would round down to a 4 (If the returned value was any less than a 5) but i cannot see how it's happening as in the immediates window i can't see any sign of a float value being returned below 5.

i.e.:-

debug.Print int(732.0/146.4) = 4

debug.Print format((732.0/146.4),"Scientific") = 5.00E+00

debug.Print format((732.0/146.4),"0.000000") = 5.000000

But if i apply a backslash (Which i've never come across before):-

debug.Print int(732.0\146.4) = 5

debug.Print format((732.0\146.4),"Scientific") = 5.00E+00

debug.Print format((732.0\146.4),"0.000000") = 5.000000

The crazy thing is that placing a formula in excel 'int(732/146.4)' returns a 5, but if you do the same via a vb script in excel it gives you the 4.

Anyone explain?

6 Upvotes

7 comments sorted by

3

u/MaxObjFn Feb 01 '21

You don't usually see this happen, but it is always a possibility because computers handle floats as approximations. Casting to an integer drops everything behind the decimal so your example is being approximated as some value less than 5. I'd recommend using Math.round.

2

u/skijumptoes Feb 01 '21

Yeah i've used a round as the fix as opposed to going the backslash route - as i've never ever come across backslash as a division operator before. And your conclusion is what i arrived at too, that basically there's some float voodoo going on behind the scenes.

It's just that from a debugging point of view, even outputting the result to a scientific format doesn't show any kind of float value being returned - which had me in circles.

Then i read about this 'backslash' division method which is backed up in mathematics, and it blew my mind that i'd never heard of it before.

It leaves me a little uncomfortable that other code may exist that uses this same division casting to an integer, it's cast to an integer as the value is passed to a SQL server expecting INT value.

So, replacing each instance with a round was a fix in this instance, it may cause other issues elsewhere if i roll it out - i clearly need to review it all. :(

I guess the question is, 'HOW' can i reveal the float value which int() is processing? Any ideas? - Purely for debug purposes.

3

u/MaxObjFn Feb 01 '21

I'll have to look into backslash division because I'm not sure what you're talking about. As far as the float approximation goes, it has to do with how it is stored in computer memory. The value is most definitely being approximated as 4.999999999999 or something. You should be able to see it if you export the whole value to the log. As an example, I would bet cdbl(1) to the log wouldn't be exactly 1.

2

u/skijumptoes Feb 01 '21 edited Feb 01 '21

I tried casting it to cdbl before making the post, and it returns perfect round number, that's the thing which bugs me here - is the fact that you can't logically debug it.

i.e.:-

debug.Print Cdbl((732/146.4)) = 5

debug.Print Cint((732/146.4)) = 5

debug.Print int((732/146.4)) = 4

Yet if you apply a backslash divide operator they all resolve as '5'.

3

u/MaxObjFn Feb 01 '21

I just had to see this for myself so I popped open a blank app.

First, I didn't realize '\' was a quotient operator. That's pretty cool. Many languages don't have it so I'll probably avoid it to prevent confusion, but at least I'm aware now.

Second, I initially thought we were talking VB.net and not VBA. I cannot reproduce the error in VB.net so evidently this quirk is isolated to VBA.

Third, I did a bunch testing in VBA and here's my result: you are not going crazy, but I'm pretty sure you've stumbled onto a bug. Congrats... I think lol.

3

u/skijumptoes Feb 01 '21

Haha, that does help actually. :)

And yes the whole backslash thing is mad, i don't want to go near that thing as i'll completely forget about it in the future where it appear 'once' in code. *Unless i comment like crazy around it*

I'll take this being a bug as an answer, because it's the only thing that makes sense right now. Thing is, it's probably been like it for years but if they change it now it could really mess up a lot of legacy projects - so catch 22, as they say.

3

u/KarlDash Feb 01 '21

try using a type of Decimal instead of double for the values.