non integer variables
agfa
Posts: 295
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.
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
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
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.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
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?
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.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
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
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