Shop OBEX P1 Docs P2 Docs Learn Events
how to assign a decimal value in bs2? — Parallax Forums

how to assign a decimal value in bs2?

darshanrtdarshanrt Posts: 4
edited 2007-10-19 15:45 in BASIC Stamp
Hello Everyone,

I am stuck up in between of my thesis. I'm suppose to use bs for my hardware implementation and how I can assign a decimal value for the same...?? means which instruction will provide me the display of e.g. x = 2.38. I want to see such decimal values on the screen entered by user. please help me regd. the same. Appriciate your time and help. You can help me finishing my thesis.

Regards,
Darshan

Comments

  • LoopyBytelooseLoopyByteloose Posts: 12,537
    edited 2007-10-16 11:40
    Fundamentally, microprocessors just don't compute in decimal. The binary numerical data at some point requires an algorhythm to convert to such, called Binary Coded Decimal.

    PBasic already does much of the conversion, storage, and manipulation for display purposes.·It is quite convienent, but hides the actual processes occuring at machine level.·So much of the literature that explains this is written in assembly language tutorials.

    The book 'Art of Electronics' has a very good in depth explanation of all the different numerical systems that may help you greatly. In addition, the microprocessor groups memory into 8 bits, 16 bits, or larger. But to be compact, 4 bits are used to store a decimal digit. So in a byte, you would store 2 decimal numbers 0-9 and you would ignore the ability to go beyond 9 [noparse][[/noparse]since 4 bits can really store 0-15]. That is how a string of decimal digits would be stored, two digits in each byte.

    To store the decimal point, you would have to decide how many decimal places you want to display.·Also, you would have to have a related byte provide the positional data as a LOG base 10 number. You could use signed arthmetic to·position·between roughly +/- 32,000 decimal places, but you might only display a maximum of 8 digits in scientific notation.· So·-14 in binary code·might look like 45.678901 x 10 -12·· As you can see, the decimal point can move to 8 different places in the display.· The numerical data would be stored as 45 67 89 01.

    These examples·will store and display numbers as BCD [noparse][[/noparse]Binary Coded Decimal - see Wikipedia]. But, unless you have a special math coprocessor, it is best to do all your math calculations in binary for both accuracy and speed. And you really have to understand the limitations that 8 bits places on the calculations.



    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "Everything in the world is purchased by labour; and our passions are the only causes of labor." -- David·Hume (1711-76)········
    ···················· Tropically,····· G. Herzog [noparse][[/noparse]·黃鶴 ]·in Taiwan

    Post Edited (Kramer) : 10/16/2007 12:43:37 PM GMT
  • stephenwagnerstephenwagner Posts: 147
    edited 2007-10-16 12:20
    If you want to display 2.38

    Multiply by 100

    x= 2.38 * 100 = 238

    In your basic program, do the following and use the "/" and the "//" operations.

    watch out for a binary word overflow

    238 / 100 (this will return 2)

    238 // 100 (this will return the remainder 38)

    serout ...your code...·[noparse][[/noparse]dec x/100, ".", dec x//100]

    If you search through the Parallax papers, you will come accross some examples.



    I hope this helps.
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2007-10-19 15:45
    Duplicate post removed.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
Sign In or Register to comment.