PDA

View Full Version : not quite understanding the MATH function



Rhatidbwoy
- 16th January 2006, 16:23
I am still in the learning phase. Just complete one program and moving on to hte other. The second program is based around plenty of math. I have attached portion of my program showing the type of math that I trying to do. With it I am getting error messages in my complex math based around basic functions. I even went into the extent of breaking it down to different levels and still got error. Is there anything that anyone could hinder for a problem solving. That is in the case of me not understanding what I am doing wrong.

Kamikaze47
- 16th January 2006, 18:13
It will help if you post the part of your program where you declare all of the variables you are using, and also post the errors you are getting.

Its not a good idea to use 0.5*(etc etc). You will be better off shifting 1 place to the right like this (etc etc)>>1.

Also, if you are declaring variables as bytes you have to make sure you cant possibly end up with a value higher than 255 in any of your calculations.

Melanie
- 16th January 2006, 20:39
> Its not a good idea to use 0.5*(etc etc).

Actually, it's not a good idea to use any numbers with Decimal places... Why? Because PICBasic ONLY works with INTEGERS.

Rhatidbwoy
- 16th January 2006, 20:52
Learning in the process. Thanks for the heads up. I think that is were I was getting the mistake or the headache.

I have attached the whole prgram that I am trying to use. The problem that I am having starts with labels LIMITSA. I think that the whole problem that I am getting is with that .5

The other thing is that I am using a linear equation to solve for a result in the range of 1 to 2. With the label PROGRAM2 you can see that I have 510 with part of the equation, but it should all come back and relate to that range of 1-2. will this be a problem.

The equations that I would like to base all of this off of is the second line to label PROGRAM1 and PROGRAM2. The difference between the two is that of the slope.

Kamikaze47
- 16th January 2006, 21:29
im sorry to say there are quite a few problems with your program.

all 11 lines with decimal numbers are invalid. This includes the lines where you multiply by 0.5 and the lines which include 1.5. As Melanie said, PBP only deals with integers.

in one of the lines you divide something by negative 255, however PBP does not support signed variables.

you may want to make some more variables words instead of bytes becuase it looks like some of your variables may overflow

Rhatidbwoy
- 17th January 2006, 00:22
ok. I make sense to what you are saying. Looking at what the manual has and to what you are saying. I made some correction since the last time we have talk.

Still working out some of the kinks that are in the program so.

I wrote out everything long hand and the other thing that I have to refresh myself up on is that >>1 = .5 ordeal. Been too long since I last seen that.

Will work more into making sense with what you are saying and learning about that difference. Thanks

Kamikaze47
- 17th January 2006, 07:05
yes, variable>>1 is the same as variable/2. >> is a bnary shift. so if a=8 for example, thats 00001000 in binary. when u shift it all 1 place to the right (>>1), you get 00000100 which is 4 (i.e. you have halved the value).

I notice you have some (1/2)'s in your modified program, but im afraid that wont work either. PBP will calculate that 1/2 (or 1>>1) as 0. 1 is represented in binary as 00000001 and if you shift that right 1 place you get 00000000.

Remember that time[lmt] is an allways going to be an integer (whole number). Therefore there is no point in checking if it is equal to 1.5 since it will never be 1.5.

Rhatidbwoy
- 17th January 2006, 19:00
Making sense. Been too long since i last did all of this. ok 1 is set in place for 0001B, 01Q, 1, and 01H then how does the half come. I can follow the counting that takes place when 0-F in Hex or 0-9 in decimal, butthe halves. I think it is safe to ask for reference to fractional literature. I would like to further my knowledge in such.

It is not quite as simple when it come to the basic math as we know of it as (what we learned in grade school) and for the following of fractions using controllers, well... Thanks I would have to say. Just show how far out off the loop I fall.


"yes, variable>>1 is the same as variable/2. >> is a bnary shift. so if a=8 for example, thats 00001000 in binary. when u shift it all 1 place to the right (>>1), you get 00000100 which is 4 (i.e. you have halved the value)."

so if I put >>2 would that be /4 (division by four). The shift right by 2 places. Trying to make sense.



The second question is the label PROGRAM2. I have an equation that has a negative value in the denominator. In paper math it should cancel out to a positive number. You had mention something earlier about this, would I have to change the equation so that there is no negative in the denominator or is there some way to work with the equation as is?



The third question is that I have the 1.5 would this fall to you shift comment. That it is more appropriate to utilize (1 + (>>1)) rather than to what I have.

Kamikaze47
- 17th January 2006, 19:16
yea, >>2 is the same as dividing by 4, >>3 is the same as dividing by 8 etc... you can also multiply by shifting left (e.g. <<1 is the same as multiplying by 2)

the line "time[chl] = (chnl1[chl] - 510) / -255" can of course be re-written as "time[chl] = (510 - chnl1[chl]) / 255". If you dont mind dividing by 256 instead it could also be written as "time[chl] = (510 - chnl1[chl])>>8" and would be much quicker to execute. However you must keep in mind that the result of this line is ALLWAYS going to be an integer even if the real matematical answer is 1.3 for example, the value stored in time[chl] will be 1

the same thing applies to the lines with 1.5 in them. the variables cant not possibly be equal to 1.5. You seem to be trying to check if the variable time[lmt] is bigger than, less than or equal to 1.5. This is not possible since time[lmt] is of course an integer and can only have whole values. You may need to re-think how your program works if you are trying to use a range of numbers between 1 and 2.

Perhaps you can post the equations/algorithms that you are trying to calculate and I may be able to work out what you are trying to do in your program.

Just for your reference, a variable that has been declared as a byte has 256 possible values in the set [0,1,2,...,255]. A variable that has been declared as a word has 65536 possible vlaues in the set [0,1,2,...,65535]

Rhatidbwoy
- 17th January 2006, 20:24
I got the last thing that you have said with the word and byte. Was reviewing that. The other thing is that I will have to change the value of 1-2 to something with higher values.


All to what I am trying to do is create a means to control a servo. The time constant that I was trying to work with first is that of 1-2 sec. That will have to change in means to 1-11 I guess. the only reason to that guess is the tenth place there are so many given posibility that will end up between 1-2 with .1 increment. So I guess then I would have to make the appropriate changes to the equation.

In other words I was using 0 to 255 as my input(y-axis) and 1-2 as my result(x-axis) coming to the equation of what I have there for my normal eq. and my reverse eq. I would just have to figure things out for my x-axis as being 1-11 rather than 1-2 . This whole thing does make sense. This then leave me to further refining the program to get that of the 1-2sec with IF..THEN and PULOUT. Not to bad.


The other thing to is that I did some reviewing and found out to what you are talking about with the shifts. I do have to THANK YOU. on the side note you taught me a few thing along with this forum. Yes there are people that are willing to help, but I believe the power of understanding is learning it. I can say that I did learn something and it is not just this problem but to what this forum has to offer.

Kamikaze47
- 17th January 2006, 21:20
Rhatidbwoy: No problem at all. I'm glad to help.

What you could do is apply a scaler to your result. For example, instead of seconds, work it out in milliseconds (scaler of 1000). Or if you dont need it that accurate, use a smaller scaler like 32 for example. Its good to choose a scaler that is a power of 2 (i.e. 2, 4, 8, 16, etc...) becuase you can multiply by these numbers very quickly and easily using the shifting you have learned.