Hi, I found in Tracy Allen's app-notes two interesting things, i) how to multiply X times PI and ii) how to display decimals, but my code (attached) does not run as expected. Where is my error?
Are you expecting to see x*pi to two decimal places? It doesn't work that way. The calculation is done using integer arithmetic and there are no decimal places.
Take out the "y = y * 100", then try x = 100. You should see "3.14" on the DEBUG window.
The formula that Tracy gave is correct (Y = (3 * X) + (9280 ** X)). As I mentioned, it gives an integer result.
Your program then attempts to get two decimal places by multiplying by 100 (Y = Y * 100). That can't work the way you want.
When you display the result using two "decimal places", all you get is zeroes.
So, the simple answer to "how to have the correct answer?" is "you can't".
For you, what's "the correct answer"? If you just want two decimal places, you can scale X as I suggested so that X is actually in units of 0.01.
To get Pi * 1, you use X = 100. To get Pi * 2, you use X = 200. You can't go much beyond that because of the 16 bit word size.
Comments
Take out the "y = y * 100", then try x = 100. You should see "3.14" on the DEBUG window.
Post Edited (MichelB) : 6/23/2009 8:00:07 AM GMT
Your program then attempts to get two decimal places by multiplying by 100 (Y = Y * 100). That can't work the way you want.
When you display the result using two "decimal places", all you get is zeroes.
So, the simple answer to "how to have the correct answer?" is "you can't".
For you, what's "the correct answer"? If you just want two decimal places, you can scale X as I suggested so that X is actually in units of 0.01.
To get Pi * 1, you use X = 100. To get Pi * 2, you use X = 200. You can't go much beyond that because of the 16 bit word size.
P.S.: modified code attached.
Post Edited (MichelB) : 6/23/2009 3:36:38 PM GMT