In VB6 or VBA, have you ever seen this formula
r = n - Int(n / 2) * 2
gives you the value of r as a negative number such as -1?
You may know that if n is Integer or Long, the above formula equivalent with
r = n Mod 2
Printable View
In VB6 or VBA, have you ever seen this formula
r = n - Int(n / 2) * 2
gives you the value of r as a negative number such as -1?
You may know that if n is Integer or Long, the above formula equivalent with
r = n Mod 2
returns -1 in rCode:n = -11
r = n - ((n \ 2) * 2)
as does
whereasCode:n = -11
r = n - (Fix(n / 2) * 2)
returns 1 in rCode:n = -11
r = n - (Int(n / 2) * 2)
SoQuote:
Originally Posted by MSDN
(-11/2) = -5.5, Int(-5.5) = -6 , (-6 * 2) = -12, (-11 --12) = (-11+12) = 1
and
(-11/2) = -5.5, Fix(-5.5) = -5, (-5 *2) = -10, (-11--10) = (-11+10) = -1
Because Int always "rounds down" negative numbers, r = n - (Int(n / 2) * 2) can't ever return a negative value, since Int(n/2) * 2, when n < 0, will always be equal to or less than n. Thus, when subrtacting it from n, the result will always be positive or zero.
I suppose that r = n - Fix(n / 2) * 2 is equivalent to r = n Mod 2 (for all Integer n) and that r = n - Int(n /2) * 2 is equivalent to r = Abs(n Mod 2) (for all Integer n)
Sorry, I didn't mention n must be a positive whole number.
You don't need to explain to me what is different between Int() and Fix(). I know these stuffs very clear.
Well, if n is a positive integer, r can never be negative.
Try this:
Code:Dim n As Variant, r1 As Variant, r2 As Variant
n = CDec("79228162514264337593543950331")
r1 = n - Int(n / 2) * 2
r2 = n - Fix(n / 2) * 2
Debug.Print "r1 = " & r1
Debug.Print "r2 = " & r2
In your case, strictly speaking, n is not held as a positive whole number. It's a Variant of type Decimal. Decimal is a base 10 floating point number held in 96 bits, similar to Double which is a base 2 floating point number. As such the FPU of the CPU will not be able to represent it exactly, in much the same way that single and double can't all be held exactly.
The word "Integer" you use for "Integer data type". I want to say for general integers (no capital i) so to avoid confuse I use the words "whole number" in Post#3.
As I mentioned in Post#1, I know it never happen for Integer or Long data type because that is equivalent to r = n Mod 2.
The expression r = n - Int(n/2)*2 is also always correct for Single or Double data type (again, provided that n as a positive whole number).
What I really want to say here is the subtype Decimal of Variant has some problems with mathematic calculations. I found many cases it is not accurate.
For n > 0 and n is a whole number (it doesn't matter what data type),
by definition we always have: Int(n/2) <= n/2, (your Quote from MSDN),
so: Int(n/2)*2 <= (n/2)*2
or: Int(n/2)*2 <= n
or: n - Int(n/2)*2 >= 0
But in the given example in Post#5, you have both r1 and r2 equal -1.
I see what you mean. In the example you gave, the Decimal subtype can't hold the exact value of 79,228,162,514,264,337,593,543,950,331 / 2 because it's run out of bits.
The division rounds it up to 39,614,081,257,132,168,796,771,975,166 (rather than 39,614,081,257,132,168,796,771,975,165.5), so when doubled it becomes greater than it was before and hence the (-1) result.