Shop OBEX P1 Docs P2 Docs Learn Events
Divide operator — Parallax Forums

Divide operator

John AbshierJohn Abshier Posts: 1,116
edited 2010-03-03 22:06 in Propeller 1
I unintentionally divided an integer by zero in Spin and learned the following: x/0 returns 0.· I am undecided if this is a bug or feature.· Other options would be to·abort (If you don't guard for division by zero you shall be punished.) or POSX, NEGX depending on the sign of x.

John Abshier

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2010-03-03 18:36
    There is no "standard" result for x/0. Mathematically it's undefined. Practically speaking, none of the possibilities you suggested (abort, POSX, NEGX) really provide a reliable answer. There's no interrupt for divide by zero as exists in some microprocessors. Returning POSX or NEGX wouldn't provide any more meaningful answer if the program doesn't check for these. It's not like these are special propagating values as is found in some floating point implementations (NaN). You really have to check for a zero divisor or design your program so a zero divisor can't occur.
  • VIRANDVIRAND Posts: 656
    edited 2010-03-03 20:34
    And there's nothing to abort to. smile.gif

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    I should be typing in Spin now.
    Coming soon. My open Propeller Project Pages and favorite links index.
  • mparkmpark Posts: 1,305
    edited 2010-03-03 20:59
    Doesn't an abort with no "catch" just halt the Spin interpreter?
  • John AbshierJohn Abshier Posts: 1,116
    edited 2010-03-03 21:25
    Mike, I agree that checking and design are the way to go. It caught me a little by surprise because my background is with languages that produce an error on zero divide since it is mathematically undefined. Just brought back painful memories of long ago when my program would not work because +0 was not equal to -0.

    John Abshier
  • Dave HeinDave Hein Posts: 6,347
    edited 2010-03-03 22:06
    I think the 32 least significant bits of infinity are zero, so this result is correct.tongue.gif

    It might be nice to provide for a user defined exception handler.· This would be useful for floating point exceptions and asserts.
Sign In or Register to comment.