Shop OBEX P1 Docs P2 Docs Learn Events
Storing decimal numbers? — Parallax Forums

Storing decimal numbers?

Mike15Mike15 Posts: 109
edited 2006-06-25 23:52 in BASIC Stamp
Is it possable to store decimal numbers to the internal eeprom? I want to store the thrust cuve of a motor. I know the basic structure of the code needed but not quite sure how to store a decimal number.

Any suggestions would be great

Comments

  • DigitalManDigitalMan Posts: 42
    edited 2006-06-25 18:12
    I've got a similar dilemma, except I want to do floating-point math on microprocessors (some of which I don't think can even do division...)

    As for storing, my idea was to make the number backwards. So if the number is 53.01, store the 53 normally as 00110101 binary, but the .01 is stored as 10 decimal, 00001010 binary. This way, you know how many preceeding zeroes there should be; else, if you stored it normally as 1, you'd never know if there should be twelve 0s or none, it'd always be 00000001.

    That make any sense?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ;Long live the SX18AC/DP!
  • Mike15Mike15 Posts: 109
    edited 2006-06-25 18:41
    Here is· the thrust curve. The first colum is time and the second thrust in newtons.

    attachment.php?attachmentid=74026
    134 x 176 - 1K
  • DigitalManDigitalMan Posts: 42
    edited 2006-06-25 19:49
    Well, for everything in the first column, unless you get a full 1 integer at·some point,·you'll want to write binary 00000000 first, to keep it consistent (first byte·is the integer, and the immediate following byte is the decimal). You'd then·give it·00001010, decimal 10, for .01. I assume you'd want the second column data immediately after the first, so the third byte to store would be 01111100 (124), and the fourth is 00000110 (6, no reversing needed). When you get to the 109.91, you'd send 01101101, then 00010011 (19, reverse of 91).

    Of course, I'm sure I'm overcomplicating this, you could probably write the whole code without using an binary. I believe either the Stamp or the compiler can figure·it out if you use decimal.·The real trick is getting the program to reverse the decimal value for you, unless your data never changes and you can put it in yourself. And do make sure it keeps track of the byte arrangement, so you pull the right data.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ;Long live the SX18AC/DP!
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2006-06-25 20:38
    Gents -

    At least in the case of the thrust curve, multiply all of your right column numbers by 100 and use a WORD sized variable, multiply those in the left-hand column by 10 and I suspect your problems will go away! Just remember to do the appropriate decimal re-locating when displaying them. The Stamp only does interger artithmatic, and only stores interger numbers, but by ranging your values, you can take decimal fractions into consideration rather easily.

    If floating point is actually necesssary for some application, there are floating point co-processors available. Just ask about them.

    Regards,

    Bruce Bates

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    <!--StartFragment -->
  • Mike15Mike15 Posts: 109
    edited 2006-06-25 21:39
    So the same would hold true with this thrust curve also? Just multiply both colums by 100?
    And if I wanted to make things a little easier when I store both curves I could just multiply all colums by 100. Then when the data is downloade to Excel all I need to do is dvide by 100.

    0.018 10.953···· I just tried this and it dose not fit the 16 bit limit. any
    0.042 39.215···· suggestions?
    0.083 66.888
    0.14 72.075
    0.223 74.958
    0.25 76.694
    0.282 80.156
    0.315 79.577
    0.336 79.577
    0.354 81.64
    0.365 77.841
    0.374 80.724
    0.389 76.694
    0.455 76.116
    0.523 74.39
    0.639 70.928
    0.722 67.467
    0.82 64.005
    0.897 58.817
    0.992 51.894
    1.084 43.824
    1.197 34.017
    1.268 28.251
    1.283 29.987
    1.295 27.104
    1.328 23.642
    1.366 16.719
    1.399 9.803
    1.435 4.612
    1.51 0

    Post Edited (Mike15) : 6/25/2006 9:53:17 PM GMT
  • Mike GreenMike Green Posts: 23,101
    edited 2006-06-25 21:57
    The only problem is that some of the numbers in the second column are greater than 65.535 which is the largest number you can store this way (in 16 bits). One solution is to give up a little precision and to divide the numbers in the second column by 2 on the Stamp and adjust the values on your PC accordingly. So ... 80.156 becomes 40078, 4.612 becomes 9224. You can round the numbers if you want so 64.005 becomes 32003 and 10.953 becomes 5477.
  • Mike GreenMike Green Posts: 23,101
    edited 2006-06-25 21:58
    oops ... 4.612 becomes 2306
  • DigitalManDigitalMan Posts: 42
    edited 2006-06-25 23:52
    Oh! Dude, that's so much simpler than my method... and it might just solve my floating point arithmetic problem as well. Now I just need to get it to multiply and divide 16-32 bits at a time, and position the·decimal point (purely aesthetic, as far as the Stamp is concerned)·properly.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chicks dig nerds... but not until after the prom.
Sign In or Register to comment.