If i do like this:
i var byte
x var byte
i = 4
x = i / 3
Then i just get an integer. How do i do to get the answer with decimals?
Printable View
If i do like this:
i var byte
x var byte
i = 4
x = i / 3
Then i just get an integer. How do i do to get the answer with decimals?
Hello
use the modulus operator://
Hi Johan,
The modulus command will only return the remainder of your calculation, not the decimal value like you wanted. Instead, you need a floating point math routine in your code to get the results your after.
Go to http://www.cambsnet.net/pbasic/FP.htm and download the fp.zip file from the link on that page. This is a long work around but it should get the result you're after.
Good coding,
Sherm
Would have helped to have logged in first .... :oops:
This is probably way too late, but, Here's one possibility for getting the decimal result of a division.
Best regards,Code:' Display Decimal result of a division
define OSC 4
define LOADER_USED 1
DEFINE HSER_TXSTA 24h ' Use this for Higher Baud Rates
DEFINE HSER_SPBRG 25 ' 9600 Baud at 4mhz
i var byte
x var byte
y var byte
Res var byte
DD var word
clear
i = 4
y = 3
x = i / y ' do the division
Res = i // y ' get the remainder
DD = 10000 ' Multiply remainder * 10,000
DD = Res * DD
DD = div32 y ' Divide remainder by original divisor
Hserout [ Dec x,".",dec4 DD,13,10,13,10] ' Sends 1.3333 out the hardware serial port
'Lcdout Dec x,".",dec4 DD ' Displays 1.3333 on LCD
end
Darrel Taylor
Maybe a stupid queston but:
How do I do this in picbasic ? :
(clk is a counter which is under 10000 pulses/sec)
t=(clk*0.27)*3.6
And output the result 't' to an lcd..
The result can be an integer.
(How to output to an lcd is no problem, just the maths)
Scale up to use integers and then divide down using the DIV32 command...
Firstly let's deal with the fractions...
0.27 is illegal in integer math, so multiply by 100 so we have 27.
3.6 is illegal in integer math, so multiply by ten so we have 36.
Multiplying the two we get 27*36 which is 972 but we are a factor of one-thousand out...
Now lets do the DIV32 bit... (PBP manual 4.17.8)...
t var word ' resultant
clk var word ' your variable in range 0-10000
a var word
temp var word
a=972
temp=clk*a
t=div32 1000
Your answer is in t as required.
Melanie
Thanks for a very quick response and
that did exactly what I requested! :)
Hi Magnus,
Using the "**" and "*/" operators you can do it a little faster. The "**" operator does an "invisible" division by 65536 and the "*/" does the same with 256. It's basically the same thing as Melanie showed you but she used a denominator of 1000. Since WORD(16bits = 65536) is the biggest number allowed by PBP, we can use "**" for constants ranging from 1 to 0 (65535 / 65536). "*/" gives a range from 256 to 0 (65535 / 256).
Applying this to your equation t=(clk*0.27)*3.6.
0,27 * 65536 = 17694,72. Round off to 17695.
3,6 * 256 = 921,6. Round off to 922.
Now the equation can be written as......
t=clk ** 17695 */ 922
Not much faster since we're still using two multiplications, we also have some rounding off errors. We should ofcourse do 0,27*3,6 before (just like Mel did). 0,27 * 3,6 = 0,972 this is less than 1, we should use "**". The magic number will be 0,972 * 65536 = 63701. The equation will now look like......
t=clk ** 63701
As you can see we have now done away with the DIV32 completely. Should be faster......lots......
Tacey Allen has a nice page with loads of goodies.....
http://www.emesys.com/BS2index.htm
/Ingvar