Fractional and percentage value calculation PIC24https://www.microchip.com/forums/(c) Microchip30Re: Fractional and percentage value calculation PIC24 (ric)<blockquote class="quote"><span class="original">elektor18</span><br/>... Instead of working on int type I was planing to go with double/float </blockquote><br/>That would be a bad/silly way to do it.<br/> <br/><blockquote class="quote"><span class="original"></span><br/>but this solution is not too bad, steps are not spot on but close enough.<br/></blockquote><br>Using which equation?<br/>From post#10, or post#12?<br/>If #10, have you tried #12?<br/> <br/>https://www.microchip.com/forums/FindPost/1105528Thu, 18 Jul 2019 00:10:57 GMTRe: Fractional and percentage value calculation PIC24 (elektor18)<blockquote class="quote"><span class="original">Aussie Susan</span><br/>But do you understand why it is working, or have you just copied what Neil suggested?<br/>I get the feeling that you have not really understood integer arithmetic in this context and why they various suggestions were made.<br/>Susan<br/><br></blockquote><br><br><br/>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.<br/> <br/>https://www.microchip.com/forums/FindPost/1105525Thu, 18 Jul 2019 00:02:53 GMTRe: Fractional and percentage value calculation PIC24 (NorthGuy)<blockquote class="quote"><span class="original">elektor18</span><br/>It is working now. I used ((unsigned long)max_width*((actual_position*100)>>8))/100.<br/>Thank you all for advice.<br/></blockquote><br><br><br/>Why so many complications. Why not simply ((unsigned long)max_width*actual_position)>>8))<br/> <br/><edit>Silly spellchecker changed "complications" to "compilations". Fighting back.<br/>https://www.microchip.com/forums/FindPost/1105512Wed, 17 Jul 2019 20:42:09 GMTRe: Fractional and percentage value calculation PIC24 (Aussie Susan)But do you understand why it is working, or have you just copied what Neil suggested?<br/>I get the feeling that you have not really understood integer arithmetic in this context and why they various suggestions were made.<br/>Susan<br/>https://www.microchip.com/forums/FindPost/1105500Wed, 17 Jul 2019 18:21:38 GMTRe: Fractional and percentage value calculation PIC24 (elektor18)It is working now. I used ((unsigned long)max_width*((actual_position*100)>>8))/100.<br/>Thank you all for advice.<br/>https://www.microchip.com/forums/FindPost/1105407Wed, 17 Jul 2019 02:36:44 GMTRe: Fractional and percentage value calculation PIC24 (NKurzman)Actually since actual_position *100 is less than 65535 <br><br> ((unsigned long)max_width*((actual_position*100)>>8))/100;<br><br>Saves a 32bit multiply.https://www.microchip.com/forums/FindPost/1105156Mon, 15 Jul 2019 08:41:33 GMTRe: Fractional and percentage value calculation PIC24 (NKurzman)(max_width*((actual_position*100UL)>>8))/100<br><br>Or<br><br> (max_width*(((unsigned long)actual_position*100)>>8))/100;https://www.microchip.com/forums/FindPost/1105155Mon, 15 Jul 2019 08:36:49 GMTRe: Fractional and percentage value calculation PIC24 (elektor18)I tried (max_width*((actual_position*100)>>8))/100 and it dosn't work as I wanted <br/>https://www.microchip.com/forums/FindPost/1105152Mon, 15 Jul 2019 08:00:48 GMTRe: Fractional and percentage value calculation PIC24 (NKurzman)You don’t need to make them 32 bits. <br> Just cast one of the variables to 32 bits. The compiler will take care of the rest.https://www.microchip.com/forums/FindPost/1105151Mon, 15 Jul 2019 07:52:20 GMTRe: Fractional and percentage value calculation PIC24 (elektor18)sorry, x and actual_position are int 16bit. So in this case I need to change it 32bits? Can you give some example?<br/> <br/>https://www.microchip.com/forums/FindPost/1105143Mon, 15 Jul 2019 06:12:11 GMTRe: Fractional and percentage value calculation PIC24 (ric)<blockquote class="quote"><span class="original">elektor18</span><br/>...<br/> so I thought I can do <br/> x = max_width * (actual_position/65535) and that should give me cursor step every 1 position on x axis, am I right?<br/></blockquote><br/>No.<br/>As I already explained, you must do the multiplication BEFORE the divide.<br/>This is because you are using integer arithmetic, so there are no fractions.<br/><blockquote class="quote"><span class="original"></span><br/>x and actual_position are double type.</blockquote><br/>Are you sure? From all your descriptions, it seems more like actual_position is an integer type.<br/>It would be extremely slow to use floating point for this sort of work.<br/> <br/>https://www.microchip.com/forums/FindPost/1105124Mon, 15 Jul 2019 04:39:24 GMTRe: Fractional and percentage value calculation PIC24 (elektor18)Sorry about confusion, agreed, it was odd asked:P<br/> <br/>So again, <br/>I have variable <b>width</b> <b>= 200 </b>and that is the <b>x</b> position of cursor on my screen. Now I want to change that position depend at <b>actual_position </b>value but in range 0 to 200. <b>actual_position </b> is 16bit variable, so I thought I can do <br/> x = max_width * (actual_position/65535) and that should give me cursor step every 1 position on x axis, am I right?<br/>x and actual_position are double type.<br/>https://www.microchip.com/forums/FindPost/1105123Mon, 15 Jul 2019 04:34:24 GMTRe: Fractional and percentage value calculation PIC24 (ric)I guess you are not a native English speaker, as your question sounds very odd.<br/>e.g.<br/>"Any suggestion how to get smooth x value increased by 1"<br/>The only answer to that is "x = x + 1" (or "x++").<br/> <br/>I think I know what you are really asking, and the answer is to multiply BEFORE you divide.<br/>You must use variables wide enough to hold the result of the multiplication.<br/> <br/>https://www.microchip.com/forums/FindPost/1105117Mon, 15 Jul 2019 04:01:14 GMTFractional and percentage value calculation PIC24 (elektor18)Hi All,<br/> <br/>I need to calculate value for x coordinate but I don't really know how to do that.<br/>My rage is from 0 to 65535(max_width) and x = max_width * (actual_position/65535).<br/>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.<br/> <br/> <br/>https://www.microchip.com/forums/FindPost/1105114Mon, 15 Jul 2019 03:46:39 GMT