Results 1 to 8 of 8

Thread: Can r = n-Int(n/2)*2 be a Negative number?

  1. #1

    Thread Starter
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Can r = n-Int(n/2)*2 be a Negative number?

    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
    Last edited by anhn; Feb 28th, 2008 at 04:31 AM.
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  2. #2
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Can r = n-Int(n/2)*2 be a Negative number?

    Code:
    n = -11
    r = n - ((n \ 2) * 2)
    returns -1 in r
    as does
    Code:
    n = -11
    r = n - (Fix(n / 2) * 2)
    whereas
    Code:
    n = -11
    r = n - (Int(n / 2) * 2)
    returns 1 in r
    Quote Originally Posted by MSDN
    The difference between Int and Fix is that if number is negative, Int returns the first negative integer less than or equal to number, whereas Fix returns the first negative integer greater than or equal to number. For example, Int converts -8.4 to -9, and Fix converts -8.4 to -8.
    So
    (-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)
    Last edited by Doogle; Feb 28th, 2008 at 05:16 AM.

  3. #3

    Thread Starter
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Can r = n-Int(n/2)*2 be a Negative number?

    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.
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  4. #4
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Can r = n-Int(n/2)*2 be a Negative number?

    Well, if n is a positive integer, r can never be negative.

  5. #5

    Thread Starter
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Can r = n-Int(n/2)*2 be a Negative number?

    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
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  6. #6
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Can r = n-Int(n/2)*2 be a Negative number?

    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.
    Last edited by Doogle; Feb 28th, 2008 at 06:32 AM.

  7. #7

    Thread Starter
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Can r = n-Int(n/2)*2 be a Negative number?

    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.
    Last edited by anhn; Feb 28th, 2008 at 06:47 AM.
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  8. #8
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Can r = n-Int(n/2)*2 be a Negative number?

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width