A single line function to modify MOD for better pattern recognition.... - Printable Version +- QB64 Phoenix Edition (https://qb64phoenix.com/forum) +-- Forum: QB64 Rising (https://qb64phoenix.com/forum/forumdisplay.php?fid=1) +--- Forum: Code and Stuff (https://qb64phoenix.com/forum/forumdisplay.php?fid=3) +---- Forum: Utilities (https://qb64phoenix.com/forum/forumdisplay.php?fid=8) +---- Thread: A single line function to modify MOD for better pattern recognition.... (/showthread.php?tid=1197) |
A single line function to modify MOD for better pattern recognition.... - Pete - 11-28-2022 Code: (Select All) DIM AS INTEGER i, j So modx is a way we can modify our QB64 MOD operator so we can work with patterns. It conforms with online modulo calculators. For comparison, see the first result for modx and compare it to the second result of MOD. Note they are the same until the numbers turn negative. Code: (Select All) $CONSOLE:ONLY Note that modx also works with negative modulo integers. I'll leave it to the more math proficient if this utility could be extended to floating point operations. The function can be modified again to change the zero output to the modulo number. See the two modx, modx_p1 and modx_p2 compared below: Code: (Select All) ' Two pattern formulas with MOD. So what the second example is useful for is things like file record look up and calendar apps, etc. Here is a quick example of how it could be used for a calendar. Code: (Select All) WIDTH 80, 42 Now I put the second pattern function together after I made the first, which makes me wonder if instead of adding the last part of the equation, if I could optimize it by changing the prior existing equation. I won't be looking into it now, as I got side tracked from another project for this, but optimization changes are always welcomed. Just be sure any changes will work for all possible possible negative and positive number and modulo situations. Also, if you find any holes in the function, please feel free to post your findings. I'm not certifying this as 100%. Steve and Bplus also have working models posted in another thread. Mine is just a one-liner, which totally suits my personality to a tee... Eeew ya carnsarn idiom! Pete RE: A single line function to modify MOD for better pattern recognition.... - SMcNeill - 11-28-2022 That's some complicated math stuff there! Here's all one would ever need with MOD -- no matter which pattern they like best: Code: (Select All) For i = 10 To -10 Step -1 RE: A single line function to modify MOD for better pattern recognition.... - bplus - 11-28-2022 Well Steve posted while I was about to... Testing Pete's in a very common situation for us hobbyists: Code: (Select All) _Title "MOD versus modx test Petes new thing ;-))" ' b+ 2022-11-28 This is actual situation that comes up and you wish there was a better way. Steve's thing should work fine for this but Pete takes on Negative Modulus! What a guy! Negative Modulus sounds to me like a psychologist looking for business. OK Pete does this hold pattern for a Modulus of a negative _Float? 2 points for anyone who can come up with some example where a modulus of negative float would be needed. I confess I am still getting over doing negative integers with MOD let alone floats positive or negative! RE: A single line function to modify MOD for better pattern recognition.... - bplus - 11-29-2022 Well Steve fixed his and it has much less use of ABS: Code: (Select All) _Title "MOD versus modx test Petes new thing" ' b+ 2022-11-28 BTW don't save file from _title if ;-)) in _title RE: A single line function to modify MOD for better pattern recognition.... - Pete - 11-29-2022 Yes, I posted to Steve in the other thread he had a bit more work to do. I took a different approach form ours and his, Steve's original function, and used SGN() to make up for the differences. Basically the difference in positive to negative numbers is the positive uses the largest number that can be divided into the given number while a negative number uses that result - 1. 7 mod 5 as... 7 = (5 * 1) + 2 where 5 goes into 7 a max of 1 time. [7 mod 5 = 2] -7 = (5 * -2) + 3 where 5 goes into -7 a max of -1 time and subtract 1 = -2. [-7 mod 5] = 3 Personally I'm going to use these as pattern functions going forward. In the past I used to adjust for zero for everything positive and I probably did something completely different if I came across negative numbers. @bplus Your circle demo was great! Pete |