Shop OBEX P1 Docs P2 Docs Learn Events
mm to inches — Parallax Forums

mm to inches

oldhippyoldhippy Posts: 36
edited 2014-11-11 19:26 in General Discussion
I am trying to convert mm to inches using a BS2. the BS2 rounds off. Any way to get tenths of an inch out of the BS2?

Comments

  • kwinnkwinn Posts: 8,697
    edited 2014-11-10 06:00
    A mm is 0.03936996 inches. or approximately 0.04 inches so if you multiply the mm by 2.54 (or do successive adds) you can get tenths of an inch without the decimal.

    If you are doing this in floating point multiply the mm by 10 or 100 and divide by the same amount after the conversion is done.
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2014-11-10 08:28
    to tenths:
    inch10 = mm ** 25802 ' good up to 65536mm
    to hundredths:
    inch100 = mm * 10 ** 25802 ' good up to 6553mm
    to thousandths
    inch1000 = mm * 100 ** 25802 ' good up to 655mm

    example
    mm = 25
    inch1000 = 984 ' meaning 0.984 inch true value 0.984251968505

    ** 25802 using the PBASIC ** operator.is equivalent to 25802/65536 = ~0.3934
  • Duane DegnDuane Degn Posts: 10,588
    edited 2014-11-10 08:56
    I gave this a try before seeing Tracy's post.

    I'm very rusty in PBASIC but this was the output when entering 88 mm.
    Please enter millimeters: 88
    88 mm = 3.5 in
    Please enter millimeters:
    

    Here's the program:
    
    
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    
    CONVERSION      CON     254
    ROUNDING        CON     127
    SCALE100        CON     100
    
    
    millimeters     VAR     Word
    inches          VAR     Word
    tenthsOfInches  VAR     Word
    
    
    Main:
      DO
        DEBUG CR, "Please enter millimeters: "     ' prompt user
        DEBUGIN NUM millimeters     ' retrieve number
        tenthsOfInches = ((millimeters * SCALE100) + ROUNDING) / CONVERSION
        inches = tenthsOfInches / 10
        tenthsOfInches = tenthsOfInches - (10 * inches)  ' I don't remember how to do modulus with BS.
        DEBUG DEC millimeters, " mm = ", DEC inches, ".", DEC tenthsOfInches, " in"
    
    
        PAUSE 1000                          ' wait one second
      LOOP                                  ' repeat forever
    

    Unfortunately the word size variables in PBASIC don't lend themselves to doing math with large numbers. I bet a much better program could be written using the tricks Tracy showed us.
  • oldhippyoldhippy Posts: 36
    edited 2014-11-11 19:26
    Thanks i'll try it tomorrow
Sign In or Register to comment.