Shop OBEX P1 Docs P2 Docs Learn Events
For Tracy Allen — Parallax Forums

For Tracy Allen

MichelBMichelB Posts: 154
edited 2009-08-25 15:51 in BASIC Stamp
Dear Tracy, herewith attached a code derived from your App-Notes, BinaryLongDivisionDemo.bs2. I've added two lines to fix the range of number entered but it does not work when I enter for example -33000 for numerator even for denominator. Please help, I'm a beginner in programming. Thank you in advance and best regards.

Comments

  • Tracy AllenTracy Allen Posts: 6,662
    edited 2009-08-25 12:55
    Bonjour Michel,
    The number must lie between -32768 and +32767, which is twos complement math for the BS2. So -33000 is out of range.
    Also, on the Stamp, you can't do
    IF (D < -32767) ....
    For most purposes the Stamp treats all numbers as positive integers in the range of 0 to 65535.

    The SDEC modifier in the debug statement allows the Stamp to accept a number like -1, but internally thosee numbers are represented as 65535.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • MichelBMichelB Posts: 154
    edited 2009-08-25 13:15
    Merci Tracy, but what I need in this code is that when N (or D) is out of range the program tells me "N (or D) must be between -32767 and +32767" and go to the line asking to enter the numerator (or the denominator).

    The following line IF (N < -32767) AND (N > 32767) THEN does not work when I enter -33000 for example. Try it.
  • Mike GreenMike Green Posts: 23,101
    edited 2009-08-25 14:52
    Michel,
    You can't do what you want. Because the Stamp only does 16-bit arithmetic, SDEC will always accept signed values in the range -32768 to +32767 which will be treated like unsigned values in the range 32768 to 65535 (for -32768 to -1) and 0 to 32767. The IF statement cannot test for values outside this range (because everything is done in 16 bit unsigned arithmetic). The -32767 ($8001) in the IF statement is treated as 32769 ($8001).

    The only way you can do what you want is to do your own conversion of characters ("0" to "9" and "-") to a number and include checking for invalid values. You can't use SDEC.
  • dev/nulldev/null Posts: 381
    edited 2009-08-25 14:55
    You should show the input to the user after he enters it, or you have to do some fancy coding (like Mike suggests) to get numbers larger than 32767 (and then they won't be numbers, but arrays, and you would have to parse them).

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Don't worry. Be happy
  • MichelBMichelB Posts: 154
    edited 2009-08-25 15:51
    OK, understood, thank you very much Mike and dev/null. Best regards.
Sign In or Register to comment.