• AVR Freaks

Hot!Fractional and percentage value calculation PIC24

Author
elektor18
New Member
  • Total Posts : 11
  • Reward points : 0
  • Joined: 2019/05/28 04:35:22
  • Location: 0
  • Status: offline
2019/07/15 03:46:39 (permalink)
0

Fractional and percentage value calculation PIC24

Hi All,
 
I need to calculate value for x coordinate but I don't really know how to do that.
My rage is from 0 to 65535(max_width) and x = max_width * (actual_position/65535).
Any suggestion how to get smooth x value increased by 1? x must be integer value. At the moment I'm getting 0 or max.
 
 
#1

13 Replies Related Threads

    ric
    Super Member
    • Total Posts : 23185
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Fractional and percentage value calculation PIC24 2019/07/15 04:01:14 (permalink)
    5 (2)
    I guess you are not  a native English speaker, as your question sounds very odd.
    e.g.
    "Any suggestion how to get smooth x value increased by 1"
    The only answer to that is "x = x + 1" (or "x++").
     
    I think I know what you are really asking, and the answer is to multiply BEFORE you divide.
    You must use variables wide enough to hold the result of the multiplication.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #2
    elektor18
    New Member
    • Total Posts : 11
    • Reward points : 0
    • Joined: 2019/05/28 04:35:22
    • Location: 0
    • Status: offline
    Re: Fractional and percentage value calculation PIC24 2019/07/15 04:34:24 (permalink)
    0
    Sorry about confusion, agreed, it was odd asked:P
     
    So again, 
    I have variable width = 200 and that is the x position of cursor on my screen. Now I want to change that position depend at actual_position value but in range 0 to 200. actual_position  is 16bit variable, so I thought I can do 
     x = max_width * (actual_position/65535) and that should give me cursor step every 1 position on x axis, am I right?
    x and actual_position are double type.
    #3
    ric
    Super Member
    • Total Posts : 23185
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Fractional and percentage value calculation PIC24 2019/07/15 04:39:24 (permalink)
    5 (1)
    elektor18
    ...
     so I thought I can do 
     x = max_width * (actual_position/65535) and that should give me cursor step every 1 position on x axis, am I right?

    No.
    As I already explained, you must do the multiplication BEFORE the divide.
    This is because you are using integer arithmetic, so there are no fractions.

    x and actual_position are double type.

    Are you sure? From all your descriptions, it seems more like actual_position is an integer type.
    It would be extremely slow to use floating point for this sort of work.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #4
    elektor18
    New Member
    • Total Posts : 11
    • Reward points : 0
    • Joined: 2019/05/28 04:35:22
    • Location: 0
    • Status: offline
    Re: Fractional and percentage value calculation PIC24 2019/07/15 06:12:11 (permalink)
    0
    sorry, x and actual_position are int 16bit. So in this case I need to change it 32bits? Can you give some example?
     
    #5
    NKurzman
    A Guy on the Net
    • Total Posts : 17599
    • Reward points : 0
    • Joined: 2008/01/16 19:33:48
    • Location: 0
    • Status: offline
    Re: Fractional and percentage value calculation PIC24 2019/07/15 07:52:20 (permalink)
    4 (1)
    You don’t need to make them 32 bits.
    Just cast one of the variables to 32 bits. The compiler will take care of the rest.
    #6
    elektor18
    New Member
    • Total Posts : 11
    • Reward points : 0
    • Joined: 2019/05/28 04:35:22
    • Location: 0
    • Status: offline
    Re: Fractional and percentage value calculation PIC24 2019/07/15 08:00:48 (permalink)
    0
    I tried (max_width*((actual_position*100)>>8))/100 and it dosn't work as I wanted 
    #7
    NKurzman
    A Guy on the Net
    • Total Posts : 17599
    • Reward points : 0
    • Joined: 2008/01/16 19:33:48
    • Location: 0
    • Status: offline
    Re: Fractional and percentage value calculation PIC24 2019/07/15 08:36:49 (permalink)
    4 (1)
    (max_width*((actual_position*100UL)>>8))/100

    Or

    (max_width*(((unsigned long)actual_position*100)>>8))/100;
    #8
    NKurzman
    A Guy on the Net
    • Total Posts : 17599
    • Reward points : 0
    • Joined: 2008/01/16 19:33:48
    • Location: 0
    • Status: offline
    Re: Fractional and percentage value calculation PIC24 2019/07/15 08:41:33 (permalink)
    4 (1)
    Actually since actual_position *100 is less than 65535

    ((unsigned long)max_width*((actual_position*100)>>8))/100;

    Saves a 32bit multiply.
    #9
    elektor18
    New Member
    • Total Posts : 11
    • Reward points : 0
    • Joined: 2019/05/28 04:35:22
    • Location: 0
    • Status: offline
    Re: Fractional and percentage value calculation PIC24 2019/07/17 02:36:44 (permalink)
    0
    It is working now. I used ((unsigned long)max_width*((actual_position*100)>>8))/100.
    Thank you all for advice.
    #10
    Aussie Susan
    Super Member
    • Total Posts : 3607
    • Reward points : 0
    • Joined: 2008/08/18 22:20:40
    • Location: Melbourne, Australia
    • Status: offline
    Re: Fractional and percentage value calculation PIC24 2019/07/17 18:21:38 (permalink)
    0
    But do you understand why it is working, or have you just copied what Neil suggested?
    I get the feeling that you have not really understood integer arithmetic in this context and why they various suggestions were made.
    Susan
    #11
    NorthGuy
    Super Member
    • Total Posts : 5541
    • Reward points : 0
    • Joined: 2014/02/23 14:23:23
    • Location: Northern Canada
    • Status: offline
    Re: Fractional and percentage value calculation PIC24 2019/07/17 20:42:09 (permalink)
    4 (1)
    elektor18
    It is working now. I used ((unsigned long)max_width*((actual_position*100)>>8))/100.
    Thank you all for advice.



    Why so many complications. Why not simply ((unsigned long)max_width*actual_position)>>8))
     
    <edit>Silly spellchecker changed "complications" to "compilations". Fighting back.
    post edited by NorthGuy - 2019/07/18 10:23:47
    #12
    elektor18
    New Member
    • Total Posts : 11
    • Reward points : 0
    • Joined: 2019/05/28 04:35:22
    • Location: 0
    • Status: offline
    Re: Fractional and percentage value calculation PIC24 2019/07/18 00:02:53 (permalink)
    0
    Aussie Susan
    But do you understand why it is working, or have you just copied what Neil suggested?
    I get the feeling that you have not really understood integer arithmetic in this context and why they various suggestions were made.
    Susan




    I do understand this Susan ;) No worries about that. I had different way to approach it in my head. Instead of working on int type I was planing to go with double/float but this solution is not too bad, steps are not spot on but close enough.
     
    #13
    ric
    Super Member
    • Total Posts : 23185
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Fractional and percentage value calculation PIC24 2019/07/18 00:10:57 (permalink)
    0
    elektor18
    ... Instead of working on int type I was planing to go with double/float

    That would be a bad/silly way to do it.
     

    but this solution is not too bad, steps are not spot on but close enough.

    Using which equation?
    From post#10, or post#12?
    If #10, have you tried #12?
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #14
    Jump to:
    © 2019 APG vNext Commercial Version 4.5