MULDIV64() question
I'm converting some angular degrees to 32 bit value for a QROTATE() command.
I'm using 360_000 as 360 degrees.
My conversion is MULDIV64(degrees,$FFFF_FFFF,360_000)
Everything works well except I thought that when degrees gets above 360_000, that I would get the lower 32 bits, the "remainder", like for 361_000, I thought I would get a MULDIV64(1_000,$FFFF_FFFF,360_000) back. But I get something weird back in the upper 16 bits...
In other words, I thought MULDIV64(361_000,$FFFF_FFFF,360_000) and MULDIV64(1_000,$FFFF_FFFF,360_000) would return the same thing, they don't.
I'm currently doing this to get around it:
IF (degrees >= 360_000)
degrees := degrees -360_000
I tried it in pasm and got the same result.
What am I doing wrong?
Thanks,
Mike
Comments
Why should MULDIV64 care about your expectations? ;o)
MULDIV64 has nothing to do with degrees. It multiplies 2 values that have 32 bits giving a 64 bit result and divides the result by another 32 bit value.
The range of 32 bits is 0-4_294_967_295, which is a little bit more than your 360_000. So, lots of room to go even further than 361_000.
Please, look at the following post, from Ariba (Andy):
https://forums.parallax.com/discussion/comment/1518542/#Comment_1518542
Hope it helps.
Henrique
My 64bit division by a 32bit is too big to be expressed as a 32 bit number, so what does it return?
The lower 32 bits? That's what I expected, but no, I'm getting something else.
Do this:
(degrees // 360_000) FRAC 360_000
That will get the modulus of degrees and compute the $0000_0000 to $FFFF_FFFF value you are looking for.
@cgracey , Thanks! Now I understand FRAC and QFRAC.... I've been really liking the CORDIC functions built into the P2!
Mike
Glad that makes sense. I've been thinking about how it is necessary to develop a mental model of what goes on in a chip, in order to be able to program it confidently. This takes time to develop. What could totally expedite this process is good visualization tools. This is what I want to pursue. That would enable people to become better programmers much more quickly than relying so exclusively on mental models that take time and energy to build. Programming could become more about how you think and less about what you had to infer.