Interesting Question

This is a discussion on Interesting Question within the A Brief History of Cprogramming.com forums, part of the Community Boards category; Hi all, I've got a question which I didn't know how to answer, and where else to post it. I ...

  1. #1
    Apprentice Swordsman's Avatar
    Join Date
    Apr 2007
    Posts
    38

    Interesting Question

    Hi all,

    I've got a question which I didn't know how to answer, and where else to post it.

    I program pretty much everyday and recently, my 5 year-old nephew has been sitting with me and asking questions (pretty simple ones really, but it's great that he's interested at such a young age!).

    Yesterday as I was writing a very simple addition program for him to think about, he asked a question which I couldn't answer, so I thought I could ask the guys here if they can think of an answer that I could tell him.

    I said that the computer takes the two variables and adds them together, so he asked "So, how do you know that the computer is right then?"

    Can anyone think of a way in which I could explain to him without going in to too much detail, or giving the answer "it just is" please?

    Thanks for the help,

    SM

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    The reliability of the computer has been tested by many other people, and made in such a way as to (almost) always be right. I would stress the fact that the computer is made as a machine that listens to instructions, and has been made to specification so that it always works when built properly, just like any other machine he uses. Once people that make computers are sure it's right, then they sell them.

    Naturally, I'd simplify it a little bit more, but the concept of the computer being built to spec as a machine is important since he's smart to ask the question about reliability. If the computer isn't consistent, there is no reason to use it as we do.
    Last edited by MacGyver; 06-29-2007 at 12:20 AM.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    > so he asked "So, how do you know that the computer is right then?"
    Perhaps by showing that the computer isn't right all the time, and the programmer has a big part to play in getting the "right" answer.

    Code:
    #include <stdlib.h>
    int main ( void ) {
        unsigned char u1, u2, u3;
        signed char   s1, s2, s3;
        u1 = u2 = 100;
        u3 = u1 + u2;
        s1 = s2 = 100;
        s3 = s1 + s2;
        printf("%d\n", u3 );
        printf("%d\n", s3 );
        return 0;
    }
    
    $ gcc foo.c
    $ ./a.exe
    200
    -56
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    Tell him there are thousands of miniature Indians inside the computer, and that when the computer needs to add numbers together, the Indians add them up on their fingers. They can count really, really fast, and you know they're never wrong, because they're Indians.

    At least this is what my dad told me...
    There are 10 types of people in this world, those who cringed when reading the beginning of this sentence and those who salivated to how superior they are for understanding something as simple as binary.

  5. #5
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    There is not a simple way to explain why the computer is right. The actual reason the computer can calculate values is actually quite complex and involves voltages and circuits.

    http://www.flexbeta.net/main/article...showarticle=15


    So any voltage over a set amount in a computer system will results in a 1 and any voltage below that will result in a 0. Transistors are the main component that represent the one's and zero's since they are either on or off. Transistors come in many flavors but that is not critical to the discussion here. We know an unsigned char as 8 bits which comprises what we know as the byte. So 1 byte really resolves to 8 separate voltages being interpreted high or low to create 1's or 0's. These 1's and 0's or highs and lows can have other operations performed on them as well. Once this has been resolved the computer then interprets the final value into a character we can see on screen. Even numbers are characters as it relates to this discussion but they are merely for the end user to understand what is going on and not vital to the operation of the underlying circuits. If the computer did not interpret the final values into characters we can see and understand, the calculations and operations would still work but we would have know way of knowing they did without having some type of measuring device to determine the output.

    Now how does the computer know how we want to interpret the data? How does it know that we want the value 65 instead of a capital A? That is accomplished through data types. This is why when you open an EXE file inside of Notepad you get nothing but gibberish. Notepad is expecting to interpret the values into characters but what it is reading are actually hexadecimal opcode values that resolve into machine language instructions. Since Notepad is not a disassembler, it uses the hexadecimal value as a lookup index into the character table or into the Windows character table. A disassembler would know how to interpret the opcodes into the english-like mnuemonics like mov rep add, etc. A disassembler would also know based on the platform and CPU in the system how to correctly interpret which values are literal and which ones are opcodes. The disassembler programmer gets this information right from the Intel or AMD tech refs which explain in great detail how the instruction opcodes work, what to expect when and where, and how to correctly display the result on screen.

    You have probably experienced this when your printf went nutso and started printing randomly from memory due to a wild pointer or a lack of a null terminator. The only way printf() knows how to interpret the data is by the tokens and flags you use in the printf() format string. If your data type does not match the format you specified, you get gibberish because it is not translated correctly for the given data type.

    But in the end it comes down to voltages of high's and low's on transistors in the CPU. The actual instruction implementation of add, subtract, multiply, divide, etc in the CPU is quite complex and gets heavily into electrical engineering which is not my forte.

  6. #6
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    I'd go with Salem's answer.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    I'd go with MacGyver's answer. The computer is a machine. It does the things it does in the same way every time. People have tried out the computer and have checked that it is right when adding numbers, so you know it will always be right when adding.

    Salem's answer involves integer ranges, and I think that's too complex. Besides, it doesn't answer the question.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    Computers do break down. Usually when they break nothing works. Anything will eventually wear out or break. I always thought that the computer was built to catch any errors that might happen for some odd reason or another. When we write code we often handle such exceptions. It is just with programs, we can usually handle it gracefully and the computer doesn't start to smoke. With hardware your computer just beeps unless it is a fatal exception and then your computer will just sit there with a blank screen until you buy a new one or fix it. Usually protective circuits are built in to chips.

    If you google for "when computer chips fail", you will get a lot of interesting articles about what happens when they do.
    http://www.thefreelibrary.com/Sick+c...y...-a04030992

    If you go further you might get stuff about the old TV show. Whatever happend to Poncho?

    I would just tell him that the computer is right as long as he can verify the answer the computer provided is correct. He will be a math wiz trying to prove the computer wrong.

    [edit]
    actually that might not be the best idea since we already have built in rounding errors due to the limitations of the data representation
    Last edited by manofsteel972; 06-29-2007 at 09:29 AM.
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  9. #9
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    Quote Originally Posted by Rashakil Fol View Post
    They can count really, really fast, and you know they're never wrong, because they're Indians.
    Well... that depends, sometimes the Big Indians have fights with the smaller ones and then all hell breaks loose.

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    01 00 00 00 is little indian format
    00 00 00 01 is big indian format
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  11. #11
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Salem's answer involves integer ranges, and I think that's too complex. Besides, it doesn't answer the question.
    I was never so much for the coding part, but rather the fact that he mentions that you can't be sure that the computer adds correctly, and therefore, in essence, the programmer has to convince himself that the output is expected.

    Even with an operation as trivial as addition, the computer is no replacement for the human brain. Only the programmer can know what is right and what is wrong.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  12. #12
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    Quote Originally Posted by Salem View Post
    01 00 00 00 is little indian format
    00 00 00 01 is big indian format
    They seem so far away from one another.

  13. #13
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Quote Originally Posted by Happy_Reaper View Post
    you can't be sure that the computer adds correctly, and therefore, in essence, the programmer has to convince himself that the output is expected.
    But that's misleading. I think the original question was really whether the computer could make basic mistakes like adding 1 and 1 and getting 3. It was a 5-year-old asking the question.

    The programmer can do only so much. He can check that no overflow will occur, but for the actual calculation he has to trust the CPU. What will he do? assert() that a+b == a+b?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  14. #14
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    To be honest I'd frankly tell him to try it and see if the computer gets it wrong. And if that doesn't convince him, to tell him to keep trying until he either finds the computer making a mistake, or is convinced it won't make a mistake.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  15. #15
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    Quote Originally Posted by Swordsman View Post
    I said that the computer takes the two variables and adds them together, so he asked "So, how do you know that the computer is right then?"

    Can anyone think of a way in which I could explain to him without going in to too much detail, or giving the answer "it just is" please?
    Tell him you don't know it's right. See Ken Thompson's Turing Award lecture.
    There are 10 types of people in this world, those who cringed when reading the beginning of this sentence and those who salivated to how superior they are for understanding something as simple as binary.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  2. A question about an interesting Makefile
    By meili100 in forum Tech Board
    Replies: 2
    Last Post: 08-12-2008, 03:56 PM
  3. interesting question about new and nothrow new
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 01-29-2008, 02:53 AM
  4. Design layer question
    By mdoland in forum C# Programming
    Replies: 0
    Last Post: 10-19-2007, 04:22 AM
  5. Question type program for beginners
    By Kirdra in forum C++ Programming
    Replies: 7
    Last Post: 09-15-2002, 05:10 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21