PropBasic - Multiply
camelot2
Posts: 54
hi, I am trying to calculate frqa for a pulse generator project. The frequency I am trying to program is 2222Hz, the sysfreq is 100_000_000Hz. The formula I am using is:
frqa = myfreq X (2^32/sysfreq) = 2222 X 42.94967296 = 95434.17331712 and rounded frqa = 95434. But the program gives 93321 !!!!!
Attached is the PropBasic program. WHAT AM I DOING WRONG ?
thanks for your help - Dave
frqa = myfreq X (2^32/sysfreq) = 2222 X 42.94967296 = 95434.17331712 and rounded frqa = 95434. But the program gives 93321 !!!!!
Attached is the PropBasic program. WHAT AM I DOING WRONG ?
thanks for your help - Dave
Comments
Update: Sorry didn't see the attachment. After looking at it, this is the math you are trying to do:
Result = 42 * 2222 + 0.94967296 * 2222
Result = 93324 + 2110 (Actually 2110.17331712 but truncated to an integer)
Result = 95434
You are computing the whole quantity correctly, but the fractional compution is likely wrong and is coming out negative. Likely because of this step:
value_dec = value ** 4078814305
In reading the prop basic manual I think that code looks good and I'm not sure what is wrong with it. If you just do that operation what value do you get?
I think you want
Bean
Bean, I replaced my code with your code, see attached
results:
value_whole = 93324
value_dec = 4294967184
value_total = 93212
It still is not giving the correct result.
Any thoughts as to why ? thanks for helping
You'll have to use half the value, then shift the result.
Bean
Bean
FRQA := 2222 / CLKFREQ << 32
Well, that's algebra for ya! (yeah, sorry, wrong!)
Hmmm, I don't think it is quite that easy... Try it.
Bean
Last question - How do I know if a value is seen as a negative value ?
Doh, you read my reply before I thought about it too hard. Yeah, you caught me using Algebra!
> $7FFF_FFFF or 2147483647
Bean
Just to clarify some fine points, if you start with a small value and want to bit shift it above $7FFF_FFFF, it will not consider sign, you will just end up with the bits shifted (which will look funny when interpreted as a signed value).
I do wish the prop had the ability to specify unsigned in calculations, when using SPIN.