Math problem!


Closed Thread
Results 1 to 9 of 9

Thread: Math problem!

  1. #1
    's Avatar
    Guest

    Default Math problem!

    If i do like this:

    i var byte
    x var byte

    i = 4

    x = i / 3


    Then i just get an integer. How do i do to get the answer with decimals?

  2. #2
    's Avatar
    Guest


    Did you find this post helpful? Yes | No

    Default

    Hello
    use the modulus operator://

  3. #3
    's Avatar
    Guest


    Did you find this post helpful? Yes | No

    Default

    Hi Johan,

    The modulus command will only return the remainder of your calculation, not the decimal value like you wanted. Instead, you need a floating point math routine in your code to get the results your after.

    Go to http://www.cambsnet.net/pbasic/FP.htm and download the fp.zip file from the link on that page. This is a long work around but it should get the result you're after.

    Good coding,

    Sherm

  4. #4
    sherm's Avatar
    sherm Guest


    Did you find this post helpful? Yes | No

    Default

    Would have helped to have logged in first .... ops:

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


    Did you find this post helpful? Yes | No

    Cool

    This is probably way too late, but, Here's one possibility for getting the decimal result of a division.

    Code:
    ' Display Decimal result of a division
    
    define  OSC 4
    define  LOADER_USED 1
    DEFINE  HSER_TXSTA  24h            ' Use this for Higher Baud Rates
    DEFINE  HSER_SPBRG  25             ' 9600 Baud at 4mhz
    
    i var byte
    x var byte
    y var byte
    Res var byte
    DD var word
    
    clear
    
    i = 4
    y = 3
    
    x = i / y      ' do the division
    Res = i // y   ' get the remainder
    DD = 10000     ' Multiply remainder * 10,000
    DD = Res * DD  
    DD = div32 y   ' Divide remainder by original divisor
    
    Hserout [ Dec x,".",dec4 DD,13,10,13,10]   ' Sends 1.3333 out the hardware serial port
    
    'Lcdout Dec x,".",dec4 DD       ' Displays 1.3333 on LCD
    
    end
    Best regards,
    Darrel Taylor

  6. #6
    Magnus K's Avatar
    Magnus K Guest


    Did you find this post helpful? Yes | No

    Default

    Maybe a stupid queston but:

    How do I do this in picbasic ? :

    (clk is a counter which is under 10000 pulses/sec)

    t=(clk*0.27)*3.6

    And output the result 't' to an lcd..
    The result can be an integer.
    (How to output to an lcd is no problem, just the maths)

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


    Did you find this post helpful? Yes | No

    Default

    Scale up to use integers and then divide down using the DIV32 command...

    Firstly let's deal with the fractions...

    0.27 is illegal in integer math, so multiply by 100 so we have 27.

    3.6 is illegal in integer math, so multiply by ten so we have 36.

    Multiplying the two we get 27*36 which is 972 but we are a factor of one-thousand out...

    Now lets do the DIV32 bit... (PBP manual 4.17.8)...

    t var word ' resultant
    clk var word ' your variable in range 0-10000
    a var word
    temp var word

    a=972
    temp=clk*a
    t=div32 1000

    Your answer is in t as required.

    Melanie

  8. #8
    Magnus K's Avatar
    Magnus K Guest


    Did you find this post helpful? Yes | No

    Default

    Thanks for a very quick response and

    that did exactly what I requested!

  9. #9
    Join Date
    Jul 2003
    Location
    Sweden
    Posts
    237


    Did you find this post helpful? Yes | No

    Arrow

    Hi Magnus,

    Using the "**" and "*/" operators you can do it a little faster. The "**" operator does an "invisible" division by 65536 and the "*/" does the same with 256. It's basically the same thing as Melanie showed you but she used a denominator of 1000. Since WORD(16bits = 65536) is the biggest number allowed by PBP, we can use "**" for constants ranging from 1 to 0 (65535 / 65536). "*/" gives a range from 256 to 0 (65535 / 256).

    Applying this to your equation t=(clk*0.27)*3.6.

    0,27 * 65536 = 17694,72. Round off to 17695.
    3,6 * 256 = 921,6. Round off to 922.

    Now the equation can be written as......
    t=clk ** 17695 */ 922

    Not much faster since we're still using two multiplications, we also have some rounding off errors. We should ofcourse do 0,27*3,6 before (just like Mel did). 0,27 * 3,6 = 0,972 this is less than 1, we should use "**". The magic number will be 0,972 * 65536 = 63701. The equation will now look like......

    t=clk ** 63701

    As you can see we have now done away with the DIV32 completely. Should be faster......lots......

    Tacey Allen has a nice page with loads of goodies.....
    http://www.emesys.com/BS2index.htm

    /Ingvar

Similar Threads

  1. Line Graph, math problem...
    By TerdRatchett in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 6th May 2009, 04:20
  2. Math problem with string result
    By Lotondo in forum mel PIC BASIC Pro
    Replies: 10
    Last Post: - 15th November 2006, 10:06
  3. PicBasic Pro Math Problem??
    By Glen65 in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 5th March 2006, 04:36
  4. Math Formula / Variable Problem! Help!
    By bwarp in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 18th February 2006, 07:59
  5. Math problem...with picbasic pro
    By pcaccia in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 15th October 2005, 19:28

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