Shop OBEX P1 Docs P2 Docs Learn Events
Division with a Stamp — Parallax Forums

Division with a Stamp

NewzedNewzed Posts: 2,503
edited 2005-12-06 16:20 in BASIC Stamp
Post deleted until I can fix the error Tracy found.

Sid

Post Edited (Newzed) : 11/30/2005 9:24:00 PM GMT

Comments

  • Tracy AllenTracy Allen Posts: 6,658
    edited 2005-11-30 20:16
    Hi Sid,

    I'm here to to nit pick a bit! I think the last line in your program has to be refined. It can't be just,

    DEBUG "Quotient is ", DEC q, ".", DEC y, cr, cr

    Depending on the number of decimal places, (your variable z), it has to select one of these:

    DEBUG "Quotient is ", DEC q, ".", DEC3 y, cr, cr
    or
    DEBUG "Quotient is ", DEC q, ".", DEC2 y, cr, cr
    or
    DEBUG "Quotient is ", DEC q, ".", DEC1 y, cr, cr

    Otherwise you end up with something that should be, e.g., 5.001, being displayed as 5.1.

    It is possible to extend the result to more decimal places (an arbitrary number of decimal places in fact, for display purposes), by repeatedly taking both a quotient and a remainder, and at each step, the remainder is multiplied by the factor (1000,100, 10) and that then makes a new quotient and remainder.

    A note on terminology: What your are calling the "modulus" is usually called the "remainder". The notion of modulus is more synonymous with "divisor".

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • NewzedNewzed Posts: 2,503
    edited 2005-11-30 21:20
    You are, sadly and to my chagrin, correct.· I will delete the post until I can straighten it out.

    Oh, the agony of it all........................

    Sid
  • NewzedNewzed Posts: 2,503
    edited 2005-12-01 15:48
    Tracy, I received your comments. Pursuant to your suggestion, I am returning to the Forum for further discussion.

    After studying about it for a bit, I agree that the use of the word "modulus" in inappropriate.

    I have substituted the word "remainder" for the word "modulus" where ever it appears.

    The superfluous "y = x*z/d" after the IF statements has been deleted.

    I modified the IF statements to use ELSEIF wherever appropriate.

    For easy reference, I have placed the computational routines under the label "calc".

    calc:

    IF x<=655 THEN
    z = 100
    y = x*z/d 'decimal part = remainder times multiplier divided by
    ·················· divisor
    ELSEIF x<=6553 THEN
    z = 10
    d = d/10
    y = x*z/d 'decimal part =remainder times multiplier divided by
    ··················· divisor
    ELSEIF x>6553 THEN
    z = 1
    d = d/100
    y = x*z/d 'decimal part = remainder times· multiplier·divided
    ··················· by divisor···
    ENDIF

    DEBUG "Quotient is ", DEC q, ".", DEC2 y, cr, cr
    DEBUG "Press any key to continue", cr
    DEBUGIN com
    GOTO main

    The statement IF x<=65 has been deleted since it is computationally identical to the IF x<=655 statement.

    I had to retain the IF statment "IF x>6355" statement because
    this statement allows me to change the values of z and d.

    The values of·z and d have been adjusted in each IF statement so that the ratio of x*z to d is always 100:1. This allows the use of two decimals places in the DEBUG Quotient statements. It also allows deletion of the three conditional DEBUG Quotient statements, which really cleans up the program.

    I believe this corrects all the deficiencies you noted. I will wait for·your comment before I post Rev A.

    Sid

  • NewzedNewzed Posts: 2,503
    edited 2005-12-05 18:26
    The Division progrm has returned.· Some small errors were corrected and the display has been augmented a bit.· Division Rev C is attached.

    This program is designed primarily for new Stampers, showing how you can derive decimals with a Stamp.

    It is written for a BS2 but will work with any Stamp from a BS2 on up.

    Sid


    Post edited by Newzed

    Post Edited (Newzed) : 12/5/2005 6:29:28 PM GMT
  • Tracy AllenTracy Allen Posts: 6,658
    edited 2005-12-06 09:00
    Hi Sid, and all. I just wanted to illustrate what I meant about displaying the decimal digits of a fraction, N/D, to an arbitrary number of decimal places. This program lets you choose the number of digits of precision you want. Say, 100 digits. Then you enter the fraction, for example, 355/113, and it displays the decimal version of the fraction to 100 places. Observe that the calculation is a very simple repetitive loop that kicks out one digit at a time, and each step acts on the remainder from the one before.

    '{$STAMP BS2pe}
    '{$PBASIC 2.5}
    ' from fraction entered as N/D, program computes decimal fraction x.xxxx.......
    ' Tracy Allen, www.emesystems.com
    n VAR WORD
    d VAR WORD
    idx VAR BYTE
    precision VAR WORD
    DEBUG CR,"computes & displays as decimal a fraction entered as N/D",CR
    DEBUG " e.g., 355/113 ENTER gives 3.1415929203539823...",CR
    DEBUG "N any WORD value 0--65535",CR
    DEBUG "D any word value limited to the range 0--6553",CR,CR
    DEBUG "First enter number of decimal places desired: "
    DEBUGIN DEC precision
    DEBUG CR
    
    DO
      DEBUG "Enter fraction N/D:  "
      DEBUGIN DEC n,DEC d
      DEBUG "   ",DEC n," / ",DEC d," = ", DEC n/d,"."
    
      ' main digit kicker outer...
      FOR idx=1 TO precision-1
        n=n//d*10
        DEBUG DEC1 n/d
      NEXT
      DEBUG DEC1 n//d*10+5/d,CR,CR  ' round off final digit
    
    LOOP    ' request new fraction
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com

    Post Edited (Tracy Allen) : 12/6/2005 4:16:48 PM GMT
  • NewzedNewzed Posts: 2,503
    edited 2005-12-06 16:04
    Tracy, that is a beautiful program.· I was trying to work out a similar routine with my Division program but you beat me to it.

    Incidentally, according to my calculator 355/115 = 3.0869565217, not 3.1415929203,
    when 10 digits are specified.

    Guess I'm a nit-picker, too yeah.gif

    Sid
  • Tracy AllenTracy Allen Posts: 6,658
    edited 2005-12-06 16:20
    Thanks. That should have been 355/113=3.1415929203 for the example, not 355/115. I edited the comment. That is a rational number that approximates pi.

    P.S. The sequence of best rational approximations to pi is,
    3, 22/7, 333/106, 355/113, 103993/33102, 104348/33215, ...

    That is, 355/113 is a good approximation, as it has to jump all the way up to a denominator of 33102 to get to a closer approximation. These are "convergents" of the continued fraction expansion of pi.

    Numbers like 1/7, 2/7, 3/7 etc. are interesting because the decimal expansion is a short repeating sequence. Looking at the expansions could be a good "stamps in math class" exercise.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com

    Post Edited (Tracy Allen) : 12/6/2005 6:09:48 PM GMT
Sign In or Register to comment.