Simple math question
turbosupra
Posts: 1,088
I keep getting 0 when I do this math equation (an example value of Leading1toLeading2EdgeTime is 54382720)
rpml1tol2 := ((1/((Leading1toLeading2EdgeTime/clkfreq) * 36)) * 60)
final value should be 2.451759186251319046442203209647 or rounded to 2
Does anyone have a code example of how to do that? Am I missing something with float here?
rpml1tol2 := ((1/((Leading1toLeading2EdgeTime/clkfreq) * 36)) * 60)
final value should be 2.451759186251319046442203209647 or rounded to 2
Does anyone have a code example of how to do that? Am I missing something with float here?
Comments
Leading1toLeading2EdgeTime/clkfreq
By my calculator with your value (54382720) the answer is 0.6 and change which will be rounded to zero.
Looking at your equation it works on a calculator because you're dealing with fractional values -- you're going to have to rearrange the formula to work within decimal division constraints.
Thanks for the reply ... is there no way to work with a value less than 1?
-Phil
I searched for over an hour on the forum before posting, trying to find an example that would apply to what I'm doing and I was unable to make anything work.
I believe I would include Float32 in the obj section, how would I insert the references/calls to it in my code though?
Assuming a clkfreq of 80,000,000 the constants evaluate to 133,333,333.333
Now the formual is 133,333,333/Leading1toLeading2EdgeTime which equals 2 in integer math.
If you need more precision use 13,333,333,300/Leading1toLeading2EdgeTime which equals 245 in units of 1/100th.
John Abshier
(clkfreq * 5)/(Leading1toLeading2EdgeTime * 3)
This will truncate to the nearest integer value. If you want to round you will need to use the following:
((clkfreq * 5) + (Leading1toLeadin2EdgeTime *3 / 2))/(Leading1toLeading2EdgeTime * 3)
Dave
... and the answer is 2. Changing to:
... gives 245 which provides additional resolution, though one will have to experiment with the full range of possible values to prevent any rollover/rollunder problems. You could do this to keep 1/100ths resolution and extend the possible range of le1tole2:
Should I heed this as a warning to learn more about floating point math and the propeller, or is there always a simple work around like this?
-Phil
2.451759186251319046442203209647
with 31 digits to the right of the decimal point. That is fine if you just want to display the result, but a second method computes it as a fixed point number that you can use in followup calculations,
24517592
with implied 7 digits rounded off to the right of the decimal point.
The core of the algorithm is simple long division: where the first step is the integer part of the result and the loop shifts in n digits to the right of the decimal point.
I'll have to try this, this weekend. I would really like to get floats working in my program, as it is becoming apparent I will need them.