• Ken B. (unregistered) in reply to delta224
    delta224:
    I wonder if the person was getting paid by LOC.
    Nope. Note how the open-braces are on the same lines as the "if"s. Also, note the single-line ifs at the end.
  • Don't mind me, I'm just another nut (unregistered) in reply to Patrick
    Patrick:
    It's a B-Tree of ifs! Brilliant!
    Not brilliant! Those darn B's keep stinging me!!
  • blah (unregistered)

    A single equals sign doesn't make for a very useful comparison.

  • (cs) in reply to I really need to find my logon info again
    I really need to find my logon info again:
    Narrator: In today's episode, we find Boss's Nephew in charge of a hamster, a jar of vaseline, and a computer. His task: Ensure we have valid postal codes. Let's see how he can screw it up!

    I can see an episode of Myth Busters here.

  • Quirkafleeg (unregistered) in reply to blah
    blah:
    A single equals sign doesn't make for a very useful comparison.
    Depends on the language.
  • blah (unregistered) in reply to Quirkafleeg
    Quirkafleeg:
    blah:
    A single equals sign doesn't make for a very useful comparison.
    Depends on the language.
    This weird pseudolanguage where you only need semicolons some of the time. :)
  • I really need to find my logon info again (unregistered) in reply to Airhead
    Airhead:
    I really need to find my logon info again:
    Narrator: In today's episode, we find Boss's Nephew in charge of a hamster, a jar of vaseline, and a computer. His task: Ensure we have valid postal codes. Let's see how he can screw it up!

    I can see an episode of Myth Busters here.

    Wouldn't that be a gerbil? I stayed away from that intentionally. Unless you're talking about the great felching incident - fake, but funny to read. And I don't know if I'd watch that episode - you'd never be able to watch Buster again...

  • (cs) in reply to blah
    blah:
    Quirkafleeg:
    blah:
    A single equals sign doesn't make for a very useful comparison.
    Depends on the language.
    This weird pseudolanguage where you only need semicolons some of the time. :)

    Why do you call Rexx a pseudolanguage?

  • Bosshog (unregistered)

    This is quite bad. If you don't know how to take a modulo, then for a large range of problems you are effectively playing as Arnold J. Rimmer.

  • Matt (unregistered)

    Am I the first one to note that this has come up before?

    http://thedailywtf.com/Articles/Checking_Your_Digits.aspx

    Captcha: cogo (n) a suite of programs aimed at coordinate geometry problems in civil engineering.

  • (cs) in reply to Matt
    Matt:
    Am I the first one to note that this has come up before?

    http://thedailywtf.com/Articles/Checking_Your_Digits.aspx

    There's even a duplicate comment: http://thedailywtf.com/Comments/Checking_Your_Digits.aspx#128782

  • Massive Debt (unregistered) in reply to BentFranklin
    BentFranklin:
    How would string manipulation functions help calculate the check bit?

    ASCII digits are a linear shift from the real number. So, you can turn the letters into numbers quite easily in assembler.

  • Richard the Troll (unregistered) in reply to Quirkafleeg
    Quirkafleeg:
    Richard:
    A very impressive piece of coding. The programmer was aware that division and converting numbers to strings (and v.v.) for string manipulation are both more expensive operations than comparison and addition, so he/she took steps to avoid them, at the price of a little extra maintenance. Of course, the code could have been optimized further by eliminating the "3 + 4 + 2" sections -- that's a high price to pay for self-documenting code.
    All of those comparisons, all of those if statements, and (apparently) not an else to be seen. It seems to me that that cancels out the optimisations.

    Not that adding elses would help.

    Trolls 1, Posters 0. :-)

  • Hexadecima (unregistered) in reply to da Doctah
    da Doctah:
    blah:
    Quirkafleeg:
    blah:
    A single equals sign doesn't make for a very useful comparison.
    Depends on the language.
    This weird pseudolanguage where you only need semicolons some of the time. :)

    Why do you call Rexx a pseudolanguage?

    So, excuse me, but you only need semicolons in Rexx if a line is encased in exactly one line of braces?

  • Hexadecima (unregistered)

    ...and terminates a block? Clearly absurd.

    Or is it "the last element in a multi-line if block"? 'cause I guess that would make more sense, although you'd think that's what the closing brace was for. Syntactic salt from hell!

  • Someone who care (unregistered) in reply to Richard
    Richard:
    A very impressive piece of coding. The programmer was aware that division and converting numbers to strings (and v.v.) for string manipulation are both more expensive operations than comparison and addition, so he/she took steps to avoid them, at the price of a little extra maintenance. Of course, the code could have been optimized further by eliminating the "3 + 4 + 2" sections -- that's a high price to pay for self-documenting code.

    Does the compiler see 3 + 4 + 2 as operations to calculate or change it to the value of 9.

    It's more efficient to compile 9 because the result of 3 + 4 + 2 will never change anyway unless the code is changed then recompiled.

  • (cs) in reply to Quirkafleeg
    Quirkafleeg:
    There's also a check digit on the bar code, which is calculated by adding up all the digits of the barcode, modding the result by 10, and then subtracting from 10.
    Fail. If sum (digits) % 10 == 0, the result of the calculation is 10.

    Fine, fine...

    check = (10 - (sum % 10)) % 10;

  • (cs) in reply to Someone who care
    Someone who care:
    Does the compiler see 3 + 4 + 2 as operations to calculate or change it to the value of 9.

    Depends on the compiler - and for many compilers, depends on the flags you pass to the compiler.

    The benefit of "3 + 4 + 2" is it makes it clear where the "9" came from.

    This sample smacks of generated code.

  • Swedish tard (unregistered) in reply to da Doctah
    da Doctah:
    blah:
    Quirkafleeg:
    blah:
    A single equals sign doesn't make for a very useful comparison.
    Depends on the language.
    This weird pseudolanguage where you only need semicolons some of the time. :)

    Why do you call Rexx a pseudolanguage?

    Python? Not that you need semicolons anywhere in python, you just can put them at the end of lines if you feel like it.

    Not that I think that the above code is python.

  • Troll (unregistered) in reply to frits
    frits:
    Are you serious?

    See below for a simple mod if mod didn't exist.

    //Works for languages that "round down" the result of an integer division
    //Base cannot be 0
    uint Modulus(uint value,uint base)
    {    
        return value - ((value/base) * base);
        
    }

    FAIL! Let's try this:

    Modulus(12,10): return 12 - ((12/10) * 10); return 12 - ((1.2) * 10); return 12 - (12); return 0;

  • EmbeddedDork (unregistered) in reply to Troll
    Troll:
    frits:
    Are you serious?

    See below for a simple mod if mod didn't exist.

    //Works for languages that "round down" the result of an integer division
    //Base cannot be 0
    uint Modulus(uint value,uint base)
    {    
        return value - ((value/base) * base);
        
    }

    FAIL! Let's try this:

    Modulus(12,10): return 12 - ((12/10) * 10); return 12 - ((1.2) * 10); return 12 - (12); return 0;

    Umm, he said "works for languages that "round down" the result of an integer division...

    So 12/10 = 1, not 1.2, hence his calculations work with his documented assumptions...

    you fail!

  • Kenn (unregistered) in reply to Troll
    Troll:
    frits:
    Are you serious?

    See below for a simple mod if mod didn't exist.

    //Works for languages that "round down" the result of an integer division
    //Base cannot be 0
    uint Modulus(uint value,uint base)
    {    
        return value - ((value/base) * base);
        
    }

    FAIL! Let's try this:

    Modulus(12,10): return 12 - ((12/10) * 10); return 12 - ((1.2) * 10); return 12 - (12); return 0;

    actually, you failed to read the comment at the top of the code. it clearly says, this works for languages that round down after integer division. assuming that we get:

    Modulus(12,10): return 12 - ((12/10) * 10); return 12 - ((1) * 10); return 12 - 10; return 2;

  • James (unregistered)

    In python:

    -sum(int(x) for x in str(barCode)) % 10

  • (cs) in reply to frist
    frist:
    Looks like a pretty optimised piece of code to me. Why should I trust a compiler to unroll my loop?
    If those were "else if" statements it would be 100% optimized.
  • Paul Vouga (unregistered) in reply to Offtopic
    Offtopic:
    Can anyone explain the advantage ou using 10-(n mod 10) over simply using n mod 10?

    I've also seem a lot of check digits based on (n mod 11) that end up mapping two results to the same check digit. Never understood why.

    The problem with summing the digits and taking mod 10 is that it doesn't protect against transpositions of two adjacent digits, a very frequent kind of error: 123456789 and 123546789 both have the same check digit.

    One fix is to take the whole number (without summing digits) and mod it by 11. This is guaranteed to detect transposition errors:

    <math>

    The reason is that 10 mod 11 == -1 mod 11. So the check digit of "...dcba" is (a-b+c-d+...) mod 11.

    If we transpose two digits, say d and c, we have check("...dcba") - check("...cdba") == (a-b+c-d+...) mod 11 - (a-b+d-c+...) mod 11 == (2c-2d) mod 11 != 0 unless c=d.

    </math>
  • somedudenamedbob (unregistered)

    It's OK, he didn't need to code for extra zip codes. The talk is that the Postal Service is bleeding money, so they won't waste money adding new zip codes.

  • justsomedude (unregistered) in reply to @Deprecated

    [quote user="@Deprecated"][quote user="Anon"]On embedded systems without a file system...[/quote]

    you would recieve a sound thumping about the ears for wasting precious flash eeprom on code like this.

    [quote]

    Wouldn't this mess go into a PROM instead of an EPROM?

  • Quirkafleeg (unregistered) in reply to Richard the Troll
    Richard the Troll:
    Trolls 1, Posters 0. :-)
    Trolls ⊂ Posters ∴ Trolls 1, Trolls 0 is possible.
  • GW (unregistered) in reply to Marc B
    Marc B:
    It is possible that the code is a port from dBase III. That damned language didn't have a mod function (or many math functions at all).

    dbaseIII, maybe, but dbaseIII+ which was used by most people did, it was number % number

  • Crash Magnet (unregistered) in reply to justsomedude

    [quote user="justsomedude"][quote user="@Deprecated"][quote user="Anon"]On embedded systems without a file system...[/quote]

    you would recieve a sound thumping about the ears for wasting precious flash eeprom on code like this.

    [quote]

    Wouldn't this mess go into a PROM instead of an EPROM?

    [/quote]

    PROM technology (as a seperate product) disappeared decades ago. EPROM technology is on life support.

    The main memory technology in use today is FLASH, and EEPROM.

  • Dave (unregistered)

    Wow... just wow....

    Y'know, there used to be ways to address issues like this in a development team....usually it involved printing off code like this, wrapping it around a steel bat and beating the author to death with it in a timely fashion.

    In today's environmentally aware culture however, I would suggest just printing off their P45 and using that instead. You'll save a few tree's and still make your point =D

  • Herby (unregistered)

    Ob: Embedded and tables (and more, Oh My!). Yes, sometimes it is easier to use tables for parity and the like. On one embedded project I worked on, the author of the code (he had his name on the company) used 256 byte lookup tables (in an 8 bit micro no less) to lookup such things as:

    1. Parity on the byte (a 6502 doesn't have any direct way to calculate parity)
    2. The number of bits in a byte (no direct way here either). Yes, it WAS faster (a simple indexed load) but it wasted LOTS of space considering it was an 8 bit micro).

    Yes, it happens, but nothing like this example!

  • (cs)

    http://en.wikipedia.org/wiki/ZIP_code

    Zipcodes are not really numbers. They are more like numeric strings. Many of them begin with 0 (Zero).

    There are actually parts of the US that have three digit zipcodes. Think national parts. Some places I can remember include an area in Montana and most of the Everglades.

    FYI: Zipcodes may not be what you think. There are actually zipcodes for places that aren't a place. Zipcodes starting with 095xx is for the navy.
    Also, a city can have many zipcodes. Many cities can share a zipcode. Same goes with counties. State boundries are not even sacred as some zipcodes even cross state lines.

    And that's just in the U.S. of A.

    MArk B.

  • Tom Leys (unregistered) in reply to Ken B.

    It also ensures that the barcode always contains at least a single non-zero number.

  • Dave-Sir (unregistered) in reply to Herby
    Herby:
    Ob: Embedded and tables (and more, Oh My!). Yes, sometimes it is easier to use tables for parity and the like. On one embedded project I worked on, the author of the code (he had his name on the company) used 256 byte lookup tables (in an 8 bit micro no less) to lookup such things as: 1) Parity on the byte (a 6502 doesn't have any direct way to calculate parity) 2) The number of bits in a byte (no direct way here either). Yes, it WAS faster (a simple indexed load) but it wasted LOTS of space considering it was an 8 bit micro).

    Yes, it happens, but nothing like this example!

    I've used 256-byte lookup tables for CRC calculations before, but never for popcount or parity. For those it's easy to use two table lookups into a 16-byte (nybble-indexed) table. You can even use the same one, e.g.,

       Byte popcount(Byte data)
       {
          static Byte bits[16] =
          {0,1,1,2,  // 0,1,2,3,
           1,2,2,3,  // 4,5,6,7,
           1,2,2,3,  // 8,9,A,B,
           2,3,3,4}; // C,D,E,F
    
          return bits[data&0xF] + bits[data>>4];
       }
    
       Byte parity(Byte data)
       {
          return popcount(data) & 1;
       }
    
  • matt (unregistered) in reply to I really need to find my logon info again

    This is one of the funniest things i've read on this board. Takes me back to days of school camp and talent show skits. I just wish I could say I didn't have to deal with code this inane from our "cracker jack senior developer consultant".

  • fjf (unregistered) in reply to Matt
    Matt:
    Am I the first one to note that this has come up before?

    http://thedailywtf.com/Articles/Checking_Your_Digits.aspx

    No, you're not (to answer the rhetoric question ;-). I also thought of it, but couldn't find the other article again. Thanks for the link.

  • sdfasdf (unregistered)
  • Some Guy (unregistered) in reply to @Deprecated
    @Deprecated:
    you would recieve a sound thumping about the ears for wasting precious flash eeprom on code like this.

    Either that, or Alex needs to improve the "Prove you're not a bot" test.

    Nah, the test is too good. It's rejecting all the intelligent and worthwhile contributors, and letting the humans in.

  • keith (unregistered)

    Fun fact, if you deal with mail and relational databases. "State" is not functionally dependent on "Zipcode". Somewhere around 73xxx, there is a town, "Texhoma". One guess which states!

    Having encountered this in live data once, it's an easy way to ID a bad SQL tutorial.

  • Shriram (unregistered)

    lol...sounds like the kinda code my 8th grade bro would write when he s sleepy :D

  • z f k (unregistered) in reply to Ken B.
    Ken B.:
    Zylon:
    Pathetic. The enterprisey way to do this would be to have an SQL database containing the correct check digit for every possible postal code.
    ... which is kept on a computer at corporate HQ, so that any changes to the definition of "mod 10" can be rolled out quickly.

    Of course, the database iwll be behind a firewall, inside the VPN, to prevent "hackers" from getting access to the trade secrets kept in the database.

    I thought it was to prevent modification of data... In this case we must store the MD5 of the data elsewhere, to confirm the data integrity.

    CYA

  • Flaming Foobar (unregistered)

    In C:

    int calc_check(char *s,char flg)
    {
      if(*s) {
        if(flg)
          return (10-(*s-'0'+calc_check(s+1,0))%10)%10+'0';
        else
          return *s-'0'+calc_check(s+1,0);
      }
      return 0;
    }
    
    
  • Kempeth (unregistered) in reply to Chris Haas
    Chris Haas:
    I'd prefer a giant switch block

    You mean: switch (zipcode) { //snip case 19: code = code + 1; case 18: code = code + 1; case 17: code = code + 1; case 16: code = code + 1; case 15: code = code + 1; case 14: code = code + 1; case 13: code = code + 1; case 12: code = code + 1; case 10: code = code + 0; case 9: code = code + 1; case 8: code = code + 1; case 7: code = code + 1; case 6: code = code + 1; case 5: code = code + 1; case 4: code = code + 1; case 3: code = code + 1; case 2: code = code + 1; case 1: code = code + 1; case 0: code = code + 0; }

  • Flaming Foobar (unregistered) in reply to Flaming Foobar
    Flaming Foobar:
    int calc_check(char *s,char flg)
    

    I documented this poorly, usage:

    char check_num=calc_check("12345",1);

    There's probably a cleverer way to do the flag but it's been a while since I've written C code.

  • Mike (unregistered)
    1. Find a website with a check digit calculator that works, such as http://www.gs1.org/barcodes/support/check_digit_calculator (frist google result)

    2. Steal code from website

    3. ?????

    4. PROFIT

  • wooot (unregistered) in reply to Troll
    Troll:

    [...]

    FAIL! Let's try this:

    Modulus(12,10): return 12 - ((12/10) * 10); return 12 - ((1.2) * 10); return 12 - (12); return 0;

    Ahh, the new floating point int. That's what I call a powerful language. Maybe php?

    /* captcha: abbas , theres more of them now? */

  • (cs) in reply to Crash Magnet
    Crash Magnet:
    PROM technology (as a separate product) disappeared decades ago. EPROM technology is on life support.

    The main memory technology in use today is FLASH, and EEPROM.

    Yesterday, I had to burn two OTP PROMs for one product. Today, I am writing test reports for mods on another product which also uses OTP PROMs. You're right though, they're getting harder to purchase. (I think we need some newer products around here...).

  • rfoxmich (unregistered) in reply to Anon

    Yeah because they intended to embed this algorithm into their postscript printers.

    Anon:
    On embedded systems without a file system...
  • Cardin (unregistered)

    Well, at least he was studious in examining his sources thoroughly.

Leave a comment on “Check Digit Check”

Log In or post as a guest

Replying to comment #:

« Return to Article