Shop OBEX P1 Docs P2 Docs Learn Events
PBasic Math — Parallax Forums

PBasic Math

servelloservello Posts: 113
edited 2008-02-22 23:28 in BASIC Stamp
Hi,

Can someone explain to me the last·equation in this program?·The output correctly puts "14" as is written in the Parallax paper, "PBasic Programming". However, on paper, I get "45" (doing the math from left to right) or "270" (by doing the multiplication first - as in the 'Order of Operation' rules). I·can also·get "270" by changing the "result" VAR from a Byte to a Word. I've even used the Google Calculator and I can't get it to equal "14".

Thanks,
Dominic

' UsingVariables.bs2

 
' {$STAMP BS2}
' {$PBASIC 2.5}
 
x        VAR    Byte
y        VAR    Byte
result   VAR    Byte       ' Using "Word" makes 3rd "result" = 270
 
    x = 25
    y = 2
 
    result = x - y         ' Subtraction
    DEBUG "x - y = ", DEC result, CR
 
    result = x / y         ' Division
    DEBUG "x / y = ", DEC result, CR
 
    result = x + y * 10    ' Order of operations
    DEBUG "x + y * 10 = ", DEC result, CR
 
' Output:
' x - y = 23
' x / y = 12
' x + y * 10 = 14           <-- How can this be 14?
 
' Here is what I get using pencil and paper:
' Do equation left to right <-- x(25) + y(2) * 10 [noparse][[/noparse]25 + 2 * 10 = 270]
' Do multiplication first   <-- y(2) * 10 + x(25) [noparse][[/noparse]2 * 10 + 25 = 45]



▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- It's just a scratch, but it's a nasty scratch

Comments

  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2008-02-20 22:59
    Hi, a byte is 8 bits and can only be a value between 0 and 255. The value 270 is 9 bits long so the most significant bit (256) is dropped leaving 8 bits ( 00001110 ) which is decimal 14.

    Jeff T.

    EDIT a good way to get familiar with different numbering systems, if you have Windows OS, is to use the Calculator in scientific mode. It allows you to display a number in different formats and different data lengths (word or byte)


    Post Edited (Unsoundcode) : 2/20/2008 11:07:35 PM GMT
  • CMcGCMcG Posts: 17
    edited 2008-02-20 23:03
    Hello Dominic,

    If you change the line

    Result· VAR· Byte· to

    Result· VAR· WORD

    ...you will get the answer that you are after. A WORD variable is 16 bits long and can have values to 65535.
    In your case the result of 270 needs a WORD to display it correctly rather than a byte·which can only have values to 255.·When only using a byte sized variable, a value of 270 rolls over to 14 as you are seeing.

    Good luck
    Carl



    Post Edited (CMcG) : 2/20/2008 11:08:36 PM GMT
  • servelloservello Posts: 113
    edited 2008-02-21 00:19
    Unsoundcode said...

    Hi, a byte is 8 bits and can only be a value between 0 and 255. The value 270 is 9 bits long so the most significant bit (256) is dropped leaving 8 bits ( 00001110 ) which is decimal 14.

    Excellent, Jeff! Thanks.

    Best,
    Dominic

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - It's just a scratch, but it's a nasty scratch
  • servelloservello Posts: 113
    edited 2008-02-21 00:45
    CMcG said...
    Hello Dominic,

    If you change the line

    Result· VAR· Byte· to

    Result· VAR· WORD

    ...you will get the answer that you are after. A WORD variable is 16 bits long and can have values to 65535.
    In your case the result of 270 needs a WORD to display it correctly rather than a byte·which can only have values to 255.·When only using a byte sized variable, a value of 270 rolls over to 14 as you are seeing.

    Good luck
    Carl

    Thanks for the reply, Carl.

    If you go back to my original post, you will see in my comments, I understood·that by·changing the variable from a·Byte to a·Word·the result would be "270".

    However, I was trying to arrive at "14" (because the paper I am reading gave that answer). That was a poor example from Parallax, and quite misleading. They show:
    (result    VAR    Byte
     
    x + y * 10 = 14
    

    because they·declared the "result" variable as a Byte (which is wrong)

    If they declared it as a Word (which would be correct) the example would show:

    result    VAR    Word
     
    x + y * 10 = 270
    

    Best,
    Dominic

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - It's just a scratch, but it's a nasty scratch
  • kjennejohnkjennejohn Posts: 171
    edited 2008-02-21 00:58
    Hi.
    Perhaps defining 'result' as a byte and showing the third equation's result as 14 is EXACTLY what was meant to happen. Perhaps it is NOT an error on their part. They may have wanted to display the vagaries of using variable sizes not adequate for the math being done. That and the fact that the equation is done 'inline' rather than using proper math order. I think this code demonstrates those two points nicely.

    Later!
    kenjj
  • servelloservello Posts: 113
    edited 2008-02-21 02:06
    Hey kenjj,

    Thanks for replying.

    I'm not sure that's such a great way to present an example for the purpose of instruction.

    For instance, a teacher tells the class that 1 + 1 = 2. And then they go on to explain *why* 1 + 1 = 2.

    The paper should have·explained *why* the result variable was 14 and then show the proper way the equation would produce the result variable showing 270 (using a Word size variable).

    As a student that would have been more appropriate as a *teaching* example.

    Best,
    Dominic

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - It's just a scratch, but it's a nasty scratch

    Post Edited (servello) : 2/21/2008 3:55:19 AM GMT
  • NewStamperNewStamper Posts: 33
    edited 2008-02-21 02:26
    Guys, good effort here. Could any one explain why x/2 is not 12.5? I have some Basic skills from college days when declaring my variables as Integers.
    Thanks
    NewStamper
  • Mike GreenMike Green Posts: 23,101
    edited 2008-02-21 02:56
    The Stamps use integer arithmetic so there are no fractions like 12.5. It is posslble to use scaled fixed point arithmetic where you represent fractional values using integers. For example, if you multiply everything by 10, you can set x to 250, then divide by two to get 125. There's some good discussion of this here: www.emesystems.com.
  • NewStamperNewStamper Posts: 33
    edited 2008-02-21 03:19
    Mike:
    Thank you for your input and direction, which I value. I cannot wait to get my first OEM Stamp and parts kit; so eager to learn the basics. Next, I will turn to video imaging tracking. Do you know any good knowledge dumps?
    NewStamper
  • Mike GreenMike Green Posts: 23,101
    edited 2008-02-21 04:17
    Have you looked at the CMU-CAM? (www.cmucam.org/)
  • NewStamperNewStamper Posts: 33
    edited 2008-02-21 04:31
    Yes, in fact I did. I still have that page open from a week ago. As you know, I am new to this business. Other than designing a mother board in college days with an Apple chip, a while back; I was not sure if CMOS would be the way to go. This was the only one I found interesting though. Thank you for drawing my attention to it again. Have you used it?

    NewStamper
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2008-02-21 04:46
    Hi NewStamper, you may have seen Roborealm if not check it out, really is impressive software

    www.roborealm.com

    Jeff T.
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2008-02-21 15:32
    NewStamper,

    What is the paper you are referring to? Where did you get it? If there is incorrect or misleading information we would like to correct it, but we would need to know the title of the document.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
  • servelloservello Posts: 113
    edited 2008-02-22 01:14
    Chris Savage (Parallax) said...
    NewStamper,

    What is the paper you are referring to? Where did you get it? If there is incorrect or misleading information we would like to correct it, but we would need to know the title of the document.

    Greetings Chris,

    I am the original poster of this thread and in my first post I actually did mention the source as a·paper named, "PBasic Programming".

    I am·attaching the actual pdf.·The code in question is on page 6 titled, "Using Variables".

    Also, the pdf properties lists Kris Magri as the Author.

    Best,
    Dominic

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - It's just a scratch, but it's a nasty scratch

    Post Edited (servello) : 2/22/2008 1:19:58 AM GMT
  • kjennejohnkjennejohn Posts: 171
    edited 2008-02-22 02:51
    I see what you mean, Servello. I think the original intent here was strictly to demonstrate the use of the DEBUG command. However, after commenting· "Order of operations" in the third equation he fails to mention the significance of the order in which math operations are done. And he leaves one wondering why the third result was 14. He has plenty of white space left on the page to do so.

    At no time does he explain integer math and order of operation to help explain any of this. Is this even a true Parallax document?

    kenjj
  • servelloservello Posts: 113
    edited 2008-02-22 03:27
    Hey kenjj,

    You can download the attached pdf in my earlier post. On the first page is the "Parallax, Inc" logo (which of course doesn't mean it has to be from Parallax), and the pdf properties lists Kris Magri as the Author.

    I'm still waiting for Chris Savage to reply to that post.

    Best,
    Dominic

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - It's just a scratch, but it's a nasty scratch
  • NewStamperNewStamper Posts: 33
    edited 2008-02-22 05:48
    Chris:
    It was not me who had difficulty reading the example. Dominic had questioned the way it was written in some paper which he did not identify. Perhaps contact him. I might add that it was a very intuitive math example that could puzzle quite a few folks. I work with IP's and subnet mask on a daily basis, which helped me understand the enigma. Your inquiry shows that Parallax is committed to improving their documentation. Therefore, I would like to avail this opportunity and give you feedback about another document/book that I have read. It is the, "Basic Stamp 2P" by Kuhnel and Zahnert. Let me preface my critique with praises. I thank Parallax for providing it for free. One should not complain. Nonetheless, I found it a very hard read. The authors have provided a bible, if I may call it that, which requires a Sunday preacher to show what it means. At times more knowledge on the author's part mean taking certain things for granted, thus, are not explained. In a nutshell, I am some what disappointed in some of the documents that I have read. (some are written well). Of course, as my knowledge improves, my evaluation of your literature could turn more favorable. Here in lies a problem. To improve your market share, Parallax must produce documentation that gets favorable ratings from the new guy on the block and not losing them. I am new to Stamp but not to technology. In college some moons ago, I did design a mother board and I was exposed to Aida, Assembly and Visual Basic.

    I think at times words are cheap. Therefore, let me share a few passages from my document that I made for my little boy in the middle school; to make him understand Dominc's original math question. I am learning BS2 for my kids. I am hoping that you would find my explanation easy to read and would ask Parallax to produce writings that would work for a novice, if not kids. Being newly registered, I do admire a few guys on this forum contributing their time quite generously. They also explain things quite well. You can use that as your guide to writing your literature. This math example was explained very well by these folks. Why could Parallax not do it? I know the answer though.

    Partial Cut and paste from my doc for a middle school kid:
    Math operation becomes very tricky if you do not pay attention when defining your variables. In the math example later below, all the variables are classified as a byte = 8 bits long. In that case, an 8 bit variable cannot hold a value greater than 255 because a byte long variable which has 8 bits and that generates a maximum number of 255.

    Byte = 8 bits = 255 in our decimal counting. Also, (2^8 – 1) = 255 is further explained below.

    Step 1
    So we lay out our 8 bits
    8 7 6 5 4 3 2 1-> bits

    Yet a proper way to write your 8 bits is:

    7 6 5 4 3 2 1 0 -> bit number
    128 64 32 16 8 4 2 1
    1 1 1 1 1 1 1 1 -> All bits are ON

    If all bits are “ON,” meaning "ON" is 1, you simply add those, also know as adding the weighted outcome of bits that are ON.
    128+64+32+16+8+4+2+1= 255

    .....some parts skiped

    Step 2
    What if you want to write the number 270 in binary? It is obvious that we will have to bring in more bit(s) because 270 is greater than 255 that 8 bits can generate. Let’s add another bit to the previous 8 bits above, making it 9 bits and see if the combination bits will be enough to cover 270.

    9 8 7 6 5 4 3 2 1-> bits or
    8 7 6 5 4 3 2 1 0 -> bit number
    256 128 64 32 16 8 4 2 1

    Convert 270 into binary. We do not need all 9 bits to get 270. We must turn some bits off=0. Let’s see, combining what numbers/bits will give us a total of 270.
    256 128 64 32 16 8 4 2 1
    1 0 0 0 0 1 1 1 0
    256 +0 +0 +0 +0 +8 +4 +2 +0 = 270

    In order to lead up to our math example, let’s write 14 in binary.
    128 64 32 16 8 4 2 1
    0 0 0 0 1 1 1 0
    0 +0 +0 +0 +8 +4 +2 +0 = 14

    Step 3
    Since your variables are set to only 8 bits, you must drop the 9th bit, which is the left most significant bit. (Numbers in blue must be dropped leaving us combination in red)

    9 8 7 6 5 4 3 2 1 (It would be proper to start with 0 and not 1.)
    256 128 64 32 16 8 4 2 1
    1 0 0 0 0 1 1 1 0
    You are left with:
    (0 0 0 0 1 1 1 0 ) = 14
    Isn’t this combination the same as what we calculated in step 2 for the number 14?

    If you want an answer of 270, you must declare your variable as "Word". Then the “result” variable would yield 270. A “word” is 16 bits. The largest number it can support is 2^16 -1 = 65,535

    Skipping quite a bit here ...............

    Since our variables are a byte long, only 8 bits, the 9th bit as explained in step 3 must get dropped, leaving us 8 bits binary combination (00001110=14) from the original nine bit calculation.

    I wrote a 3 page document for my son and he did not ask me any questions on it. Would the same document be enough for a 10 year old, probably not. I do feel that your documents do need improvement from my perspective. Other seasoned guys that help me here would not agree with me because they have crossed that learning curve. To increase your market share, Parallax must reduce the drop rate by producing outstanding documents and support.

    I thank Parallax and the forum clan for their support.

    NewStamper
  • servelloservello Posts: 113
    edited 2008-02-22 07:44
    NewStanmper said...
    Chris:
    It was not me who had difficulty reading the example. Dominic had questioned the way it was written in some paper which he did not identify. Perhaps contact him.

    <snip>
    Hello NewStamper,

    As shown above, you mention that I (Dominic) did not identify the paper wherin I found the code in question.

    I take care to be as thorough as possible in my posts. If you re-read my original post, the very first post of this thread, you will see that I clearly do identify the Parallax paper.

    Also, I mentioned the paper by name again as well as the Author (Kris Magri)·in the 15th post down from the top, as a reply to Chris Savage.

    And if that wasn't enough, I've even included the actual paper (.pdf) as an attachment in that very same post.

    May I ask what more·I could·have done to identify the paper?

    BTW, the essay you wrote for your son·was well done. Can you put it up somewhere as a pdf. It would serve as a great example for those of us new to programming and microcontrollers.

    Take care,
    Dominic

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - It's just a scratch, but it's a nasty scratch

    Post Edited (servello) : 2/22/2008 8:17:08 AM GMT
  • servelloservello Posts: 113
    edited 2008-02-22 08:11
    Hi all,

    For those who are curious and wanted to know if the "PBasic Programming" paper was an authentic·Parallax item, I did a bit of Googling and found some items·showing·the Author of the paper, Kris Magri as an actual member of Parallax.

    #1 From the Parallax book, "WHAT'S A MICROCONTROLLER"

    SPECIAL CONTRIBUTORS
    The Parallax team assembled to write this text includes: curriculum design and technical
    writing by Andy Lindsay, illustration by Rich Allred, cover design by Jen Jacobs and
    Larissa Crittenden, general consulting by Aristides Alvarez and Jeff Martin,
    electromechanical consulting by John Barrowman, technical review and solutions by Kris
    Magri
    , technical editing by Stephanie Lindsay, and committee review by Rich Allred,
    Gabe Duran, Stephanie Lindsay, and Kris Magri
    .


    #2 From "Basic Logic using the Parallax Digital Trainer"
    The web-controlled Boe-Bot was developed by Kris Magri of Parallax.

    #3 From the Parallax Forum http://forums.parallax.com/showthread.php?p=469591
    Posted 10/4/2004 11:36 AM (GMT -5)
    3rd post down Kris Magri replies to a forum question as a Parallax member.

    So, it may actually be a Parallax paper. I'm still waiting for Chris Savage to reply.

    Bye,

    Dominic

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - It's just a scratch, but it's a nasty scratch
  • NewStamperNewStamper Posts: 33
    edited 2008-02-22 15:55
    Dominic:
    Sorry, I later realized that you did mention the source. I was planning to put a correction note, but I was side tracked.
    Dominic writes: "May I ask what more I could have done to identify the paper?"
    You have done fine.

    NewStamper
  • servelloservello Posts: 113
    edited 2008-02-22 18:13
    Hey NewStamper,

    My post was not meant to come across as being an angry reply. I just wanted to set the record straight so that there is a clear continuity in this thread.

    Forgive me if it sounded ill-tempered.

    Best,
    Dominic

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - It's just a scratch, but it's a nasty scratch
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2008-02-22 20:45
    Hello,

    ·· It appears the paper you refer to predates my time here at Parallax. I was not familiar with the paper, nor could I find it on our website. You did state the name and source of the paper after I asked for it. There was a reply that read:

    “Being newly registered, I do admire a few guys on this forum contributing their time quite generously. They also explain things quite well. You can use that as your guide to writing your literature. This math example was explained very well by these folks. Why could Parallax not do it? I know the answer though.”

    ·· I have to say though, in all my years working with Parallax products and now as an employee, I have never heard anyone say that our documentation does not explain things very well or clearly. The documentation and support is one of the main factors for me wanting to work for this company. Yes, many individuals on the forums do generously donate their time to help others. Before coming to Parallax I was one of those people. It is also my job to make sure that the forums are maintained and that support is provided where we can. I feel that level of support exceeds all of the other companies I have dealt with in the past, which is one of the joys of working here. I love to help people and have, in the past, answered forum questions and support e-mail during evenings and weekends when Parallax is closed.

    ·· As I mentioned earlier in the thread, if our documentation is in error we do our best to correct it. To that end there are a few items which have Errata sheets posted listing the errors and corrections. This document is quite out-dated, not even covering the PBASIC 2.5 syntax and should not be used. Please see our “What’s a Microcontroller?” book, available free as a PDF from our website for a more complete tutorial.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
  • NewStamperNewStamper Posts: 33
    edited 2008-02-22 22:56
    Chris:

    In a nut shell, Parallax is everything that you have said. I can see that. I am quite proud of your company and your support.

    It is my opinion that with my skill level some documents that I have read can stand a lot of improvement. Please respect my opinion. Should you put out a document explaining calculus, even poorly written, would get a relatively favorable review from me because of my schooling and experience in Calc.

    Parallax has a program for educators. If your target is those kids, then it is my humble opinion your book, "Basic Stamp 2P" by Kuhnel and Zahnert would not prove to be a good medium for instruction. There are many professors who are teaching, some cannot depart from their knowledge even though they are very knowledgeable. Disseminating knowledge effectively is not every professor's strong point and they all get evaluated by students at the end of the semester, hopefully to improve.

    Parallax's documentation is a fare game to be critiqued. Your reply, "I have never heard anyone say that our documentation does not explain things very well or clearly" approach may stop Parallax from improving their documentation. I would have taken keen interest if your reply would have asked me to give you an example that would show why the aforesaid read was not an easy task to comprehend. I would have done so, as I provided some basics on the 8 bit binary in one of my previous post.

    Going back to Dominic's research which he provided regarding his read. If that research is valid, I commend Parallax by hiring numerous consultants for reviewing and vetting it for accuracy. Yet, Dominic has a very valid point regarding it lacking basic explanation. It could be that Parallax saw, it was lacking instructional capabilities, that it pulled it off circulation for that reason or any other reason. One member explained the math eloquently by posting two sentences. If the book authors had incorporated such verbiage, we would not be having this discussion.

    I am putting time in this post because I sincerely believe that Parallax is trying its best to give us quality support through human interface and through documentation. Since I am at the receiving end now. I would like to see some improvement for my level to help me teach kids with minimal effort. We all can stand improvement. The document on the serial LCD panel is a nice one in my opinion.

    Now on a lighter note; my parts kit and OEM has arrived. Chris, gear up and wear your shields, I think it will get ugly with my basic 101 questions. I will be indebted to your support as always.......smile

    NewStamper
    (I need to fix my nick, some how "n" has slipped in there)!!!
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2008-02-22 23:11
    I understand your points…One final note on my part. I did ask in my first reply for anything that was incorrect or needed to be addressed as we strive to provide accurate documentation. So far, all of the references mentioned such as the PBASIC Programming and the BASIC Stamp 2P are older documents that are not published anymore. As we progress we update and correct documentation as we need to or based on feedback provided by customers. This will continue. So as a note for those who see documentation or references that they have questions on my first advice would be to get this information directly from our website, not via a Google search. Second, if you find an error, check to see if an errata was already created for it. Lastly, if you want to notify us of an issue we just need complete information. I have gotten many an e-mail that said something like, “I don’t understand line 5 of the code on page 134…can you tell me how it works?” Enjoy your OEM Kit…I have installed dozens of those in various projects, many of which are still in use today.

    Edit, you can correct your "Display Name" in the Control panel.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
  • servelloservello Posts: 113
    edited 2008-02-22 23:20
    Hello Chris,

    I'm not sure if you were referring to me in your last post, but there is enough in it to make me think so.

    First
    I did not write that quoted reply you mention (in blue font). And because I did not write it, your next paragraph could not possibly be directed to me. Although it certainly wouldn't make much sense, if you were speaking to me, please let me know.

    Second
    In your last paragraph, you advise me(?) to "see our 'What's a Microcontroller?' book" . . .

    As I quoted a selection from that very book in an earlier post, it is quite obvious that I must already have access to the book. In fact, it was part of my purchase when I ordered the entire package (BS2, BOE, Syntax Book, etc.).

    Third
    You asked me for the information so that you could check to see if it was a Parallax document. I did as you requested, and even did some digging on my own to help out. So I fail to see the reason for your last post.

    Fourth
    Please know that I am in no way complaining about Parallax (or anything else for that matter). I thoroughly enjoy the products, the instruction, the help etc. of Parallax. I intend to be a loyal customer for many years to come.

    Thanks,
    Dominic

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - It's just a scratch, but it's a nasty scratch
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2008-02-22 23:25
    My last post was in reference to the thread in its entirety and not necessarily directed at anyone in particular. It was more for clarification of my previous points, which may have been misunderstood or lost in the confusion.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
  • servelloservello Posts: 113
    edited 2008-02-22 23:28
    Great.

    Now maybe this thread can die honorably [noparse];)[/noparse]

    -Dominic

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - It's just a scratch, but it's a nasty scratch
Sign In or Register to comment.