|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Please Explain!equivalent (it gets fired when an OptionButton is checked): Function Dec2Bin(IntegerNumber As Long) Dim iNum Dim BinValue, TempValue iNum = IntegerNumber Do Until iNum = 0 TempValue = iNum Mod 2 BinValue = CStr(TempValue) + BinValue MsgBox (iNum) iNum = iNum \ 2 Loop txtOutput.Text = BinValue End Function As such, the above function does the needful but there's one thing which I couldn't follow. Suppose I enter the decimal number 10 & then check the OptionButton that fires the above function. When I do so, iNum becomes equal to 10 & the message box first shows 10.....that's OK, then the message box shows 5....that's OK as well but after this, the message box shows 2! This is what I couldn't understand. How come the message box is showing 2? Shouldn't it be 2.5 instead because when the variable iNum is 5, then the line just before the Loop line divides 5 by 2 which is 2.5; so why is the message box showing 2 instead of 2.5? Can someone please explain me this? Thanks, Regards, Arpan
Show quote
Hide quote
"Arpan" <arpan***@hotmail.com> wrote in message "iNum = iNum / 2" would be floating point division and would return 2.5;news:1129923391.138103.217460@o13g2000cwo.googlegroups.com > The following function converts a decimal number into its binary > equivalent (it gets fired when an OptionButton is checked): > > Function Dec2Bin(IntegerNumber As Long) > Dim iNum > Dim BinValue, TempValue > iNum = IntegerNumber > Do Until iNum = 0 > TempValue = iNum Mod 2 > BinValue = CStr(TempValue) + BinValue > MsgBox (iNum) > iNum = iNum \ 2 > Loop > > txtOutput.Text = BinValue > End Function > > As such, the above function does the needful but there's one thing > which I couldn't follow. Suppose I enter the decimal number 10 & then > check the OptionButton that fires the above function. When I do so, > iNum becomes equal to 10 & the message box first shows 10.....that's > OK, then the message box shows 5....that's OK as well but after this, > the message box shows 2! This is what I couldn't understand. How come > the message box is showing 2? Shouldn't it be 2.5 instead because when > the variable iNum is 5, then the line just before the Loop line > divides 5 by 2 which is 2.5; so why is the message box showing 2 > instead of 2.5? Can someone please explain me this? since iNum is decalred as a Variant it would store that value "iNum = iNum \ 2" is integer division; note the slash is backwards... 5\2 = 2 -- Reply to the group so all can participate VB.Net: "Fool me once..." Thanks Bob for your help. Didn't notice the "\" which wasn't doing the
normal division! Thanks once again, Regards, Arpan / divides and returns floating-point result.
\ does integer division and returns Integer/Long. This executes faster than the above. Reference: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbenlr98/html/vaoprintegerdivide.asp Show quoteHide quote "Arpan" <arpan***@hotmail.com> wrote in message news:1129923391.138103.217460@o13g2000cwo.googlegroups.com... > The following function converts a decimal number into its binary > equivalent (it gets fired when an OptionButton is checked): > > Function Dec2Bin(IntegerNumber As Long) > Dim iNum > Dim BinValue, TempValue > iNum = IntegerNumber > Do Until iNum = 0 > TempValue = iNum Mod 2 > BinValue = CStr(TempValue) + BinValue > MsgBox (iNum) > iNum = iNum \ 2 > Loop > > txtOutput.Text = BinValue > End Function > > As such, the above function does the needful but there's one thing > which I couldn't follow. Suppose I enter the decimal number 10 & then > check the OptionButton that fires the above function. When I do so, > iNum becomes equal to 10 & the message box first shows 10.....that's > OK, then the message box shows 5....that's OK as well but after this, > the message box shows 2! This is what I couldn't understand. How come > the message box is showing 2? Shouldn't it be 2.5 instead because when > the variable iNum is 5, then the line just before the Loop line divides > 5 by 2 which is 2.5; so why is the message box showing 2 instead of > 2.5? Can someone please explain me this? > > Thanks, > > Regards, > > Arpan > > Thanks Someone for your input. Just to add a little more detail to the explanation of the "\"operator... A \ B is equivalent to this... Int(Round(A) / Round(B)) I mention this because there is a tendency to assume the equivalence is this instead... Int(A / B) and that is simply not the case. Anyway, considering the correct equivalence above, if your A and B values are Integers or Longs, the Round parts are immaterial. However, if either or both are floating point values, then Round is important. For example, 12.5 \ 3.5 will evaluate to 3 because the Round functions (in the equivalences) use Banker's Rounding as its method of rounding (2.5 rounds down to 12 but 3.5 rounds up to 4). Rick
Intergating Multiple VB6.0 Projects into one: Project Grouping
Parsing UNIX text files Return an error code/message from a VB6 app Is this possible ? How to access related documents in VB through source code? Lost_Focus event of controls doesn't fire Intercept the Min/Max button Press Error 453: Can't find DLL entry point (VB6) Launching App MSDN Library on network location |
|||||||||||||||||||||||