Thread: Weird typo leads to interesting result... what does it mean?

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    83

    Weird typo leads to interesting result... what does it mean?

    Hi,
    While writing some C++ code, I made a typo, which produced a bizarre but in interesting result in program. However, I completely do not understand what it is doing. Can anyone make sense of this?

    Code:
    int a; // This can be any number from 0..255
    int b; // This can be any number from 0..255
    if(abs(a-b<2))
        return true;
    return false;
    I wrote it as a typo, but it is having an effect I can't otherwise duplicate. Can anyone understand this?
    Thanks

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by pollypocket4eva View Post
    I wrote it as a typo
    You wrote what as a typo?

    1) that code
    2) some part of that code
    3) some other part of that code
    4) your post
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    83
    The typo was:
    if(abs(a-b<2))
    instead of:
    if(abs(a-b)<2)

    Within the context of a much larger program, it's having a funny effect. But I was just wondering if anyone could help me understand what this line does.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by pollypocket4eva View Post
    it's having a funny effect.
    This "funny effect" is:

    1) ha-ha, ROFL funny
    2) kind of strange funny
    3) just plain funny!
    4) indescribably funny

    I'm being serious, BTW. You will get a quicker answer to your questions if you make some attempt to include relevant information.

    However, as a clue, this:
    Code:
    (a-b<2)
    will yield either 0 or 1, regardless of the value of a & b.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    Code:
    abs(a-b<2)
    1 if (a-b)<2, 0 otherwise.
    Code:
    abs(a-b)<2
    1 if abs(a-b)<2, 0 otherwise.

    abs(1) is 1, and abs(0) is 0.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inserting a swf file in a windows application
    By face_master in forum Windows Programming
    Replies: 12
    Last Post: 05-03-2009, 11:29 AM
  2. getting weird characters and numbers when searchin
    By starvinmarvin in forum C Programming
    Replies: 3
    Last Post: 12-03-2008, 03:45 AM
  3. simple program weird result
    By nav301 in forum C Programming
    Replies: 3
    Last Post: 12-20-2006, 02:07 PM
  4. weird computation result
    By axr0284 in forum C++ Programming
    Replies: 2
    Last Post: 02-17-2006, 11:34 PM
  5. Output problems with structures
    By Gkitty in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 05:27 AM