Menu

Division by zero

Help
MBB
2022-09-03
2022-09-03
  • MBB

    MBB - 2022-09-03

    I'm using Great Cow BASIC (0.99.01 2022-01-27 (Windows 64 bit) : Build 1073) with an 18F4550.

    I wrote this simple program to test how GCB responds to division by zero:

    dim AA as Byte
    dim BB as Word
    dim CC as Integer
    dim DD as Long

    AA = 57/0
      BB = 348/0
        CC = 2489/0
          DD = 158052/0
    
      locate 0, 0
      print AA
    
        locate 1, 0
        print BB
    
          locate 2, 0
          print CC
    
            locate 3, 0
            print DD
    

    '******

    The program compiled without any errors or warnings.

    I got 0 for AA, BB, CC, and DD.

    Since division by 0 is undefined:

    Shouldn't the compiler give a division by zero error?

    When the 18F4550 runs the program why do I get 0?

     
  • Chris Roper

    Chris Roper - 2022-09-03

    In this case you are dividing by a Zero Constant but even if you divided by a variable with a value of Zero how do you expect the compiler to know the contents of the variable?

    The compiler is doing what you told it to do and flagging it as an error in the compiler would only be detecting typing errors, it would do nothing to detect division by zero at runtime.

     
  • MBB

    MBB - 2022-09-03

    Isn't an answer of 0 the wrong result?

    If you did divisions and kept reducing the denominator, your answer would keep increasing.
    For example:
    10/1 = 10 10/0.1 = 100 10/0.01 = 1000 10/ 0.001 = 10000 10/0 = ? (infinity or undefined?

    An answer of zero seems completely wrong.

     
  • Chris Roper

    Chris Roper - 2022-09-03

    If GCBASIC used Floating Point Arithmetic you would be correct, but in this case you are performing Integer Arithmetic so Zero is the "Most" correct result.

     
  • Chris Roper

    Chris Roper - 2022-09-03

    And a far more eloquent answer from Scientific American

    The mathematical zero and the philosophical notion of nothingness are related but are not the same.

     
  • mkstevo

    mkstevo - 2022-09-03

    Personally, I try to check for cases of division where the result is likely to be "inaccurate", where I might be dividing 9 by 105 for example. While an answer of zero might not be correct, given integer variables, would the given result be more accurate than zero? My HP 16C (in integer mode) also returns zero for this calculation (9 / 109) with the carry flag set.

     
  • MBB

    MBB - 2022-09-03

    Thanks!

     

Log in to post a comment.