Shop OBEX P1 Docs P2 Docs Learn Events
non integer variables — Parallax Forums

non integer variables

agfaagfa Posts: 295
edited 2008-07-27 19:22 in Propeller 1
Just for the sake of understanding.

how would a variable be defined if I needed it to represent the range of 100 values, lets say, 0 to 1.·(0, 0.01, 0.02, 0.03 etc.)?· Could this be defined as a byte?· Would it be better to just do a math conversion?

I can't yet grasp how non integers are represented in binary.

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2008-07-25 12:11
    When you have a byte variable, you have 256 values. You can use them as the usual integers or you can call them anything you like. You could have 256 shades of color. You could have 256 different kinds of screws. You can also scale these numbers by implicitly (and explicitly) dividing them by a constant where appropriate. For example, you could use the integers from 0 to 256 to represent the values 0/100, 1/100, 2/100, ... 255/100. These are more easily written as 0.00, 0.01, 0.02, ..., 2.55, but the values are the same. You can directly add these values, so 3/100 + 5/100 = 8/100. You can also directly compare them numerically. When you multiply or divide them, you have to apply a scale factor (of 100 here since there's an extra factor of 100).
  • jeffjohnvoljeffjohnvol Posts: 197
    edited 2008-07-25 13:08
    Mike's right.

    A byte is 8 bits, so it can hold a maximium value of 2 to the power of 8, or 256. A nibble (4 bits) can hold 2^4 which is 16. A word has 16 bits and can hold 2^16 which is 65536.

    when you see 256 or 11111111 in binary , its the same value, just a different visual representation.

    Here's another way to look at it. Lets say you have 2 toggle switches. How many different combinations of on/off can it have? 4 of course. Add one more switch, now you have doubled the possible combinations to 8, or 2^3. This is essentially how binary relates to real world numbers.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    End dependance on foreign oil.· www.pickensplan.com
  • jazzedjazzed Posts: 11,803
    edited 2008-07-25 20:14
    Mike's scale factor description is the key. For example ....
    ratios like 0.5 can be expressed in integer math using bytes and words:

    0.5 = 64/128

    If you scale the number by scale factor of 100 you can use math operations:

    50 = 64*100/128

    To multipy 0.5 by 0.33 for example, you can say:

    50*0.33 = 0.33*64*100/128 = 64*33/128 = 2112/128
    16.5 = 2112/128

    No need to get into exponent & mantissa or n-tuples etc....
    Hope this helps some way.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
  • agfaagfa Posts: 295
    edited 2008-07-26 12:08
    Thanks,

    the "scale factor" is new to me and very interesting,·maybe it answered my question and i just don't get it.

    i do understand the binary system. i do understand that a byte has 256 unique values.

    i don't get what·a decimal 2.55 is in binary.· is there a binary representation for the decimal and the value to the right of the decimal?· depending on the answere,·i thought a variable·used as· 0 to 2.55 with 256 steps might need more than a byte to define it.

    a loop 0 to 255 step 1 uses a given amount of resources.· i would think that a loop 0 to 2.55 step .01 is using more.· Would a loop 0 to 255 step 1 / 100 use more or less resources than a loop 0 to 2.55 step .01 / execute more or less efficiently,·faster or slower?
  • jazzedjazzed Posts: 11,803
    edited 2008-07-26 16:35
    http://en.wikipedia.org/wiki/IEEE_754 is a good place to start understanding binary representations of floating point numbers.
    Use http://babbage.cs.qc.edu/IEEE-754/Decimal.html to find out the binary representation of 2.55.

    Without floating point, one must create an integer version of the numbers with a predefined understanding (a scale factor) of their meaning. Your 2.55 for example would be stored as 255 with each "increment" of the number meaning 0.01.

    There is no built in floating point "step" for repeat loops. Using "repeat ii from 0 to 255 step 0.1" compiles, but does not function.

    If you need to keep track of a floating point number value for comparison in a loop other than using a scale factor, you should consider looking at the float_demo.spin packaged with the propeller tool.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
  • agfaagfa Posts: 295
    edited 2008-07-26 18:24
    jazzed, thanks for the responses.· thats what i was looking for.
  • StefanL38StefanL38 Posts: 2,292
    edited 2008-07-27 19:13
    hello agfa,

    if you take a pocket calculator and type 0.5 * 0.5 the result will be 0.25

    if you type 5 * 5 the result will be 25

    except of the "0." there are the same digits.

    32bit integer means something around 4*1.000.000.000

    If you want to to calculations with values like 0.01, 1.25, 0.0007 etc.

    You can handle them as following. Get the smallest possible value
    f.e. 0.0007 multiplying 0.0007 with 10000 rersults in 7
    Multiplying with 10000 means the decimalpoint moves 4 digits to the right.

    If you have to do a calculation like 0.0007 * 1.25 your pocketcalculator says 0,000875

    With integermath you do 7 * 12500 = 87500

    If the propeller should show the correct result of 0.0007 * 1.25
    you move the decimalpoint for 2*4 digits to the right

    12345.678
    87500.000
    0.0008750
    
    



    If your values do not vary too big between very small and big numbers you can
    move the decimalpoint of each factor by multiplying it by 10^X to get rid of the leading zeros
    and first in the output to a display you place the decimalpoint at the right place

    Another example: 1.2 * 1.2 = 1.44
    to have this with two digits BEHIND the decimalpoint you would do
    1200 * 1200 = 1440000

    then even 1.473 * 1.326 = 1.953198
    would be exactly calculated as
    1473 * 1326 = 1953198

    inside your software you have the ("wrong") value 1953198
    now move the decimalpoint for 6 digits to the left
    1 digit 195319.8
    2 digit 19531.98
    3 digit 1953.198
    4 digit 195.3198
    5 digit 19.53198
    6 digit 1.953198

    and there it is the original result

    So to show the correct result to the user you
    would send to the display
    "1", decimalpoint, rest of the digits

    this is called FIXED-floating point because the amount of digits AFTER the decimalpoint is a fixed number


    For a lookup-table where your REAL values are 0.002, 0.003 .... ,1.2
    you would define a table with 2,3,... ,120 (using a scalefactor of 1000): 0.002 * 1000 = 2, 0.003 * 1000 = 3 etc.

    As soon as you have to calculate things like 0.000000237 * 8733222 / 0.000765 * 77423098
    it is better to use the floatingpoint-objects from the obex

    best regards

    Stefan
  • AleAle Posts: 2,363
    edited 2008-07-27 19:22
    It is much better to use a scale factor that a power of two is. That way you do not multiply by 1000 for instance, you shift it say 10 bits, 1024.
Sign In or Register to comment.