Wrong values with COSINE!


Closed Thread
Results 1 to 25 of 25
  1. #1
    Eng4444's Avatar
    Eng4444 Guest

    Angry Wrong values with COSINE!

    Hello,

    i am working on a very serious project and i chose to use PicBasic Pro.
    but i just encountered a serious problem:

    in a certain part of my program i got a certain value "alpha". I needed to get cosine alpha.
    but the problem is that:

    "COS returns the 8-bit cosine of a value. The result is in two’s complement
    form (i.e. -127 to 127). It uses a quarter-wave lookup table to find the
    result. Cosine starts with a value in binary radians, 0 to 255, as opposed
    to the usual 0 to 359 degrees."

    but i need the usual 0 to 359 degrees!! and this is so essential in my program!

    Now i am getting: cos 0 = 127 ! i just need to get back the normal values example: cos 0 = 1, cos 90 = 0, cos 70 = 0.342

    Please any help??

    It's so URGENT... write a code if possible and i'll be more than grateful...

  2. #2
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    You're not going to get an answer any faster if you post it once or if you post a dozen times... but if you keep doing it, it'll be considered spamming and they'll all be removed!

    PICBasic only handles integers. Like it or not, you won't get a fractional result.

  3. #3
    Eng4444's Avatar
    Eng4444 Guest


    Did you find this post helpful? Yes | No

    Question it's not about integers...

    I wasn't talking about integers... i noticed that you wre only concentrating on me sending many posts.. why not concentrating about the contents of posts?

    the cosine and sine functions are not giving the basic answers...they are giving results in twos's complement!

    for example, instead of giving a "1" for "cosine 0", it is giving 127!!

    i need to go to the normal numbering system... anyone has a hint?

  4. #4
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    >> i noticed that you wre only concentrating on me sending many posts..

    That's what System Admins are supposed to so...

    >> why not concentrating about the contents of posts?

    You'll have a solution about an hour after despositing US$5K in my Bank Account... there's around a couple of dozen people on this forum that also have the skills and could probably get it down to thirty minutes... Somebody may do it for free if you're lucky and post it on the forum, but until then be patient... a lot of people only look at this list once or twice a week.

  5. #5
    Eng4444's Avatar
    Eng4444 Guest


    Did you find this post helpful? Yes | No

    Default Well you must be right..

    Quote Originally Posted by Melanie
    >> i noticed that you wre only concentrating on me sending many posts..

    That's what System Admins are supposed to so...

    >> why not concentrating about the contents of posts?

    You'll have a solution about an hour after despositing US$5K in my Bank Account... there's around a couple of dozen people on this forum that also have the skills and could probably get it down to thirty minutes... Somebody may do it for free if you're lucky and post it on the forum, but until then be patient... a lot of people only look at this list once or twice a week.
    -----------------------------------------------------------------

    Well,

    i'll wait as you advice me but i really need a quick answer...i posted this msg in several websites and even sent a mail to picbasic pro community.. i hope i'll get at least one good answer!

  6. #6
    Join Date
    Oct 2004
    Location
    Hangover, Germany
    Posts
    289


    Did you find this post helpful? Yes | No

    Talking

    Uuuhhhh...

    Super-Mel is right, don't cry as a angry kid !!!!


    Use your brain, or source it out !

    Transfering a value of 0-360 to 0-255 ?
    What a heavy task !
    perhaps: x*255/360 ?

    And now, the final:
    Converting a number -127..127 to -1..1 ?
    perhaps: x/127 ?

    Have a nice day, coding the FP-math. And after that you will see, it is not necessary, because your real problem has another - slim - solution.

    Or use some of these f******* C-dinos around us.
    PBP 2.50C, MCS+ 3.0.0.5, MPLAB 8, MPASM 5.14, ASIX Presto, PoScope, mE mikroBasic V7.2, PICKIT2

  7. #7
    Eng4444's Avatar
    Eng4444 Guest


    Did you find this post helpful? Yes | No

    Talking But look...

    It's not at all as you thought dear Mr intelligent...

    By the way, thanks for your friendly reply! you're so much sociable and i think all your friends envie you

    there real problem is that i have tried your small equation... maybe befor you were born! but it didn't work at all..... i wonder what is the mistery and this is why i have asked a help....

    THIS EQUATION DOES NOT WORK... just try it and you will see! don't just "talk", try and tell me the result... are you getting real values???

  8. #8
    Join Date
    Oct 2004
    Location
    Italy
    Posts
    695


    Did you find this post helpful? Yes | No

    Default

    Hi,

    Please don't insult me if the link below it is not useful to you!

    http://www.emesystems.com/BS2math3.htm#arcCos

    Luciano

  9. #9
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,614


    Did you find this post helpful? Yes | No

    Talking Idiot Inside or Outside ????

    Quote Originally Posted by Luciano
    Hi,

    Please don't insult me if the link below it is not useful to you!

    http://www.emesystems.com/BS2math3.htm#arcCos

    Luciano
    Hi Luciano,

    You like living dangerously ...

    Our Genius told you it was VERY IMPORTANT and VERY URGENT... a question of life ; might be yours !!!

    May be we've forgotten to tell him 8th Bit is sign and 7 others the absolute value in 1/127 steps.

    Gee, I run and upload my Marlin 444 ... one never knows.

    Good night, Mr Genius ...

    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  10. #10
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    As i like to live dangerousely, i'll ask to Mr Eng4444 here why he must absolutely use COS. Part of a huge and complicated formula?

    Why not simply create a table in EXCEL (or else) then dump the results in a EEPROM. Once done, just read results from there.

    Mr Eng4444, this kind of polite atitude you're using will certainely help you to have help Is it caused by the stress? If so, it's not our problem. Take a deep breath... feel better now? Don't do an heart-attack for that. Your life and health is far much important than your work!
    Last edited by mister_e; - 7th May 2006 at 20:20.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  11. #11
    Join Date
    Oct 2004
    Location
    Hangover, Germany
    Posts
    289


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Eng4444
    It's not at all as you thought dear Mr intelligent...

    By the way, thanks for your friendly reply! you're so much sociable and i think all your friends envie you

    there real problem is that i have tried your small equation... maybe befor you were born! but it didn't work at all..... i wonder what is the mistery and this is why i have asked a help....

    THIS EQUATION DOES NOT WORK... just try it and you will see! don't just "talk", try and tell me the result... are you getting real values???

    You don't know to whom you are talking - Stupid!

    I worked with computers long time before there was a PC. We worked with this cases with blinking lights and switches in front...
    I think, I know what bits look like and how to implement calculations ....

    And now - I'm sitting around, waiting for you and all your friends...
    But THEY will not help you!
    PBP 2.50C, MCS+ 3.0.0.5, MPLAB 8, MPASM 5.14, ASIX Presto, PoScope, mE mikroBasic V7.2, PICKIT2

  12. #12
    Eng4444's Avatar
    Eng4444 Guest


    Did you find this post helpful? Yes | No

    Thumbs up Hey people.... thanks for your replies!:)

    Some of you are good ones and others are.... really bad! especially the one of lights and bulbs! haha.

    Dear Sirs,

    my question was simple and clear. You do not have to worry about what i am doing and why i did not use a table! you really have smart ideas there! but i just needed to see if there is one single HEAD of you able to solve this matter...

    i have 3 possibilities other than using TABLES.. but i still only have to solve this matter which is:
    i need to use the provided "cosine" question in the PicBasic Pro tutorial in order to get the usual answers... like cos 0 = 1 and cos 90 = 0 etc etc

    so just in case someone has a method, please say it and give the method otherwise, if it is only for dummie answers and stupidities, i have enough of IMPORTANT things to deal with and not obliged deal with bulbs and lights or anything else....

    By the way Mr Bulb, as long as I DUNNO WITH WHOM I AM TALKING.... why don't you show me what do you have INSIDE your brain? and not just what you USED to do in the past?

    maybe still dealing with bulbs till now?..........

    Come on people! the world has changed! lol

  13. #13
    Eng4444's Avatar
    Eng4444 Guest


    Did you find this post helpful? Yes | No

    Default One more thing...

    thanks for:

    http://www.emesystems.com/BS2math3.htm#arcCos

    but in my application, i am not using x or y...

    i am just getting a phase shift from a Phase Locked-Loop, transforming this number to degree and then, i need the cosine of this number...

    All the problem is that "cosine" was made to give an answer in two's complement... and this is bad...... i need the normal answers...

  14. #14
    Join Date
    Oct 2004
    Location
    Italy
    Posts
    695


    Did you find this post helpful? Yes | No

    Default

    Hi,

    From the link:
    http://www.emesystems.com/BS2math3.htm#arcCos

    Here is a simple demo program that illustrates the use of the SIN and COS on the BASIC Stamp. It steps through all the angles from 0 to 360. The angle is printed in degrees and the cosine and sine of the angle are printed as decimal numbers in the range of -1.00 to +1.00.

    Luciano

  15. #15
    Eng4444's Avatar
    Eng4444 Guest


    Did you find this post helpful? Yes | No

    Exclamation OKay

    Thanks agains Luciano,

    but i really do not need them in this way... i don't mean to be rude or not caring but i really really do not need them like this..

  16. #16
    Join Date
    Oct 2004
    Location
    Italy
    Posts
    695


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Eng4444
    ...i just need to get back the normal values example: cos 0 = 1, cos 90 = 0, cos 70 = 0.342
    The sample in the link will print the values as you are asking in your first post.
    Please explain that again with a real example with numbers.

    Luciano

  17. #17
    Eng4444's Avatar
    Eng4444 Guest


    Did you find this post helpful? Yes | No

    Exclamation Here it is:

    Thanks for your interest , i will try to provide more details and hope they will be helpful for you.

    But first, in that link, i am not using "ATN and HYP", nor "X and Y", nor arctan or arcos or logarith or interpolation...

    all my work is that i am getting Power. It is given as you know by:

    P = V x I x Cos alpha.

    and i am using PIC 16F876A with PicBasic Pro.

    I solved the matter of V and I by applying sensors. and for the phase between them, i used a PLL. now i am inserting all 3 variables as inputs for the PIC and i need it to give me the correct power but i discovered that it is giving very wrong values!

    i found out that it's from the "cos" because as it is stated, the cosine give value in 2's complement from -127 to +127 and not in the traditional way from 0 to 359 degrees...

    But for us, we use the traditional numbering system.... so i need to use it! But it is not working... i can do it with a table but i need to find out if there is a mathematical way to use the same function "cosine" , already provided..

    thanks again and i hope you will be able to help me.

  18. #18
    Join Date
    Oct 2004
    Location
    Hangover, Germany
    Posts
    289


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Eng4444
    all my work is that i am getting Power. It is given as you know by:

    P = V x I x Cos alpha.
    And out of this silly task you are able to make such a lot of dust ?

    P=V*I 16 Bit
    X=COS(Alpha*255/360) 8 Bit 2-komplement
    Minus=X.7
    P=P*(X and %01111111) 32 Bit !!!
    P=Div32 127
    It's quick-n-dirty, maybe there is an error inside....

    And all the way without using this boring 0,xxx, only integers !
    Ha, the good old times are back, where programmers must understand what they are doing and counting the bits...

    Just USE YOUR BRAIN, not your mouth !



    Hello Mel,
    Yes, I ruin you prices and your time....
    but I inserted 1 mistake, so this code will not work right. But some postings ago, there was the right hint. Maybe we could train his brain....
    Last edited by BigWumpus; - 7th May 2006 at 22:21.
    PBP 2.50C, MCS+ 3.0.0.5, MPLAB 8, MPASM 5.14, ASIX Presto, PoScope, mE mikroBasic V7.2, PICKIT2

  19. #19
    Eng4444's Avatar
    Eng4444 Guest


    Did you find this post helpful? Yes | No

    Unhappy You know what i like about forums?

    Quote Originally Posted by BigWumpus
    And out of this silly task you are able to make such a lot of dust ?

    P=V*I 16 Bit
    X=COS(Alpha*255/360) 8 Bit 2-komplement
    Minus=X.7
    P=P*(X and %01111111) 32 Bit !!!
    P=Div32 127
    It's quick-n-dirty, maybe there is an error inside....

    Just USE YOUR BRAIN, not your mouth !



    Hello Mel,
    Yes, I ruin you prices and your time....
    but I inserted 1 mistake, so will not work right. But some postings ago, there was the right hint. Maybe we could train his brain....
    --------------------------------------------------------------------


    It's just that you like to show up... i wonder if you succeeded because for me you were just acting like a clown... trying to be nice to mel just by making fun of me.

    i wonder what you get from all this - stupid... and moreover, you wanna train my brain? lol and you submit and error on purpose???!

    i came here because i didn't know how to do it so do you really find it worthy to take all your advantages and try to make fun of me?? or just to appear the intelligent?? you are idiot! keep your knowledge for yourself.

    In case someone else has an idea about my question i'll be grateful.



    By the way Mel,

    Did you made this forum just to make fun of people with your friends?
    thanks a lot for your kindness.

  20. #20
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    It's not my forum. I just make sure folks don't abuse it (or each other) too much...

    From your first post, I gather this is a serious project with a serious problem... unfortunately I only answer the frivolous ones - serious I leave to other people - unless there's money involved.

    But really, to join a forum just in order to ask folks to drop what they're doing and IMMEDIATELY write code for you because you need it URGENTLY is really the height of bad manners - and you should expect a little fun to be made from it.

    My original post pointed you to the fact that you CANNOT have fractions with PICBasic... ONLY INTEGERS. If you are capable of doing the circuit you describe, then you are also capable of a little high-school mathematics in converting degrees to radians. Then feeding that figure into the provided COS function and converting the resultant into the required COSINE but scaled-up because we must always work in INTEGERS.

    You have been shown how to convert degrees to radians and how to get your Cosine function... a little bit of experimentation on your part will complete the code and you will have a working program. So make an effort, try to understand what's been written (thank you BigW), and don't expect everyone else to do it all for you...

    See... this is why we're not getting rich on this forum... somebody always does it for free... *sob*

  21. #21
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Nicely done Melanie.

    I'm affraid I would have been much more severe.

    However, my severeity would have been directed to everyone besides ENG4444.

    Maybe it wasn't the best entrance to our forum, but. The words, idiot, stupid, and the dreaded F word came from well established users. Not the poor guy that made his first post here today. (originally)

    As the "Bad Cop" I'd like to "Run you ALL in" for inciting a riot.

    Fortunately, there's a "Good Cop" around that handles children better than I do.
    <br>
    DT

    P.S. Luciano excluded!

  22. #22
    Eng4444's Avatar
    Eng4444 Guest


    Did you find this post helpful? Yes | No

    Arrow thanks...

    I have joined several forums and it was the first time where i see people asking for money and the sentence "don't expect to do it for free".

    it's a real big shame because orginally, that was the aim of forums in case some of you do not know.


    in all cases, i don't wanna get far from my subject. I'm still asking that question and still waiting more answers

  23. #23
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    You know, if I want a Pizza and I don't know how to make one...

    1. I look in a Cookbook and follow the recipie (for Cookbook read PICBasic manual)...
    2. I go to Cookery School and learn...
    3. I at least make an attempt at something and if it doesn't turn out right then perhaps somebody will feel sorry for me and help me out...
    4. I pay someone to bring it to my door...

    I sure as hell don't run down the street shouting "I'm hungry and I want someone to make me a Pizza NOW!" and when neighbours offer their recipies and suggestions I dismiss them without at least trying to understand what they're offering me...

    Quick (one minute of coding) and dirty 0-90 degree FREE Cosine demonstrator...

    If you don't like it... go write your own...
    If you want 91-359 Degrees... go write your own - it's not immediately obvious...
    If you don't like the precision... go write your own...
    ...I think you get the idea...

    Code:
    	Angle Var WORD
    	Cosine var WORD
    	Radian var BYTE
    	TempA var WORD
    	TempB var WORD
    
    	LCDOut $FE,1
    Loop:
    	For Angle=0 to 90
    		Gosub GetCosine
    		LCDOut $FE,$80,"Angle=",#Angle,"  ",$FE,$C0,"Cosine="
    		LCDOut #Cosine DIG 3,".",#Cosine DIG 2,#Cosine DIG 1,#Cosine DIG 0
    		Pause 1000
    		Next Angle
    	Goto Loop
    
    GetCosine:
    	Radian=(Angle+1)*255/360
    	TempA=COS(Radian)
    	TempA=TempA & %01111111
    	TempB=TempA*1000
    	Cosine=DIV32 127
    	Return
    Oh... and before you say anything to the contrary... yes it DOES work...

  24. #24
    Join Date
    Oct 2004
    Location
    Italy
    Posts
    695


    Did you find this post helpful? Yes | No

    Default

    Hi,

    The precision you can achieve with your code is not the only issue.

    Luciano

    * * *

    Power factor / Dissipating the Myths

    By David Gibson

    Power factor is one of those subjects about which there seems to be a great deal of confusion and many misconceptions. The culprit is the the assertion that power factor = cos(Phi), Phi being the phase shift between voltage and current. While this is true under certain ideal conditions, there are many real-world instances where it is quite incorrect. This article will give you a basic introduction into power factor, and explain why cos(Phi) is not the whole answer.

    Read this article here:
    http://www.microconsultants.com/tips...t/pfarticl.htm

  25. #25
    Eng4444's Avatar
    Eng4444 Guest


    Did you find this post helpful? Yes | No

    Thumbs up Sorry i forgot one thing..

    Thanks Melanie a lot.

    Your code is great and it worked perfectly.


    Luciano,

    Also a big thanks because that article is extremely important.

Similar Threads

  1. WRITECODE stores wrong 14-bit word values in FlashMEM
    By BobPigford in forum mel PIC BASIC Pro
    Replies: 18
    Last Post: - 26th June 2009, 04:35
  2. Looking at pic basic pro symbol values in mplab 8.15a simulator
    By ukemigrant in forum mel PIC BASIC Pro
    Replies: 10
    Last Post: - 16th May 2009, 13:18
  3. Need help calculating OSC cap values
    By Kamikaze47 in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 26th June 2008, 21:27
  4. Funny PULSIN values: what is going on???
    By xnihilo in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 30th April 2008, 08:02
  5. 16F877A problem: "coupled" digital values
    By Lupo83 in forum General
    Replies: 16
    Last Post: - 4th December 2007, 12:46

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts