Shop OBEX P1 Docs P2 Docs Learn Events
Math with decimals problems. — Parallax Forums

Math with decimals problems.

neotericneoteric Posts: 144
edited 2006-03-09 05:30 in BASIC Stamp
I understand the BS2 does not do decimals.· I also understand there is a limit to the size number I can have.
I have a variable·A that could be any number from 1 to 5000.· I then need to take that variable, multiply it by 4,680 and divide it by 3958.

A=A*4680
A=A/3958
This would be the same as taking the variable and multiplying it by 1.1824, which is fine.

A=Z*1.1824

I have another variable, B, that could be any number from 1 to 5000.· I then need to take that variable, multiply it by 3010 and divide it by 3729.
B=B*3010
B=B/3729···· (this example is slightly different, because I am multiplying a number that is smaller than the number I am dividing by.
This would bae the same as taking the variable B and multiplying it by .0871 which is fine.

I have searched the forums, and have seen references to "this is done with subroutines..."· but I cannot find any description of how it is actually done.··· I have googled, and have tried some of thier "fixed point" logic, but have been wholely unsuccessful.·· I would really appreciate someone at the very least pointed me in the right direction....

Thanks.

Comments

  • T&E EngineerT&E Engineer Posts: 1,396
    edited 2006-03-07 19:43
    You may want to look at this link for Math calculations with a BS2:

    http://www.emesystems.com/BS2math6.htm

    Thanks.
  • NewzedNewzed Posts: 2,503
    edited 2006-03-07 20:28
    Neteoteric, here is the answer to your question.

    To mulitply a number by a decimal, use the */ operator.· See Help section).

    num = num*/$XXXX

    Your decimal 1.824 has to be converted to $XXXX.· The interportion becomes $01.· The decimal portion becomes .1824 * 255 = 46.512
    Round the number off to 47 and convert it to·· HEX number with your scientific calculator in Windows.· 47 = 2F, so your */ operator is $012F.
    Write num = num*/$012F.

    If your number was 500, then 500*/$012F = 591

    500 * 4680 / 3958 = ·591.2076.

    Close enough?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Sid Weaver
    Do you have a Stamp Tester yet?
    http://hometown.aol.com/newzed/index.html

    ·
  • NewzedNewzed Posts: 2,503
    edited 2006-03-07 20:31
    I meant to say integral instead of interportion.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Sid Weaver
    Do you have a Stamp Tester yet?
    http://hometown.aol.com/newzed/index.html

    ·
  • Ryan ClarkeRyan Clarke Posts: 738
    edited 2006-03-07 20:43
    Neoteric,

    First and foremost I second the suggestion you read Dr. Allen's page that LTE suggested above- however I would suggest you look at this page of his:
    http://www.emesystems.com/BS2math1.htm

    Now, that being said-

    Consider the following:

    Result = ( N */ 47 ) + N

    or

    Result = ( N */ 303 )

    ALSO:

    Result = ( N ** 11954 )

    How did we get these numbers?

    Remember that the */ operator is "units" of "1/256" and the ** operator is "units" of "1/65536"- so multiply your 0.1824 * 256 on a calculator. Also try it with 65536. (Why do we add the extra N in the */ example? Figure that out and you will fully understand the ** and */ operators.)

    Ryan

    (Sid post jumped me [noparse]:)[/noparse]

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Ryan Clarke
    Parallax Tech Support

    RClarke@Parallax.com
  • neotericneoteric Posts: 144
    edited 2006-03-08 15:44
    Well, I think I understand it now.· Thanks to all who guided me.

    ((Why do we add the extra N in the */ example? Figure that out and you will fully understand the ** and */ operators.)

    Because the */ only returns the lower byte.· The N thus has to be added to get the 1.x result.....

    Thanks all.
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2006-03-09 05:30
    You've got it mixed: ** -- in practic -- multiplies by units of 1/65536, so it will always be something less than one. The */ operator works in units of 1/256, hence the upper byte is whole units (up to 255 [noparse][[/noparse]$FF]).

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
Sign In or Register to comment.