doing math with non-integers
Archiver
Posts: 46,084
I've got an equation that I want the stamp to compute:
result = ((0.0514 * adc) - 85.557)
I'm reading the variable adc from my A/D converter, and then I want
to solve the equation above, but apparently I can't use non-integers
in math functions on the stamp, is that correct? So how can I solve
the equation?
result = ((0.0514 * adc) - 85.557)
I'm reading the variable adc from my A/D converter, and then I want
to solve the equation above, but apparently I can't use non-integers
in math functions on the stamp, is that correct? So how can I solve
the equation?
Comments
result = (adc ** 33685) - 856
DEBUG "+" + (result.BIT15 * 2)
result = ABS result
DEBUG DEC (result / 10), ".", DEC1 result
Just note, though, that if your adc count is less that 1665 that your
value will be negative.
For more on BASIC Stamp math tricks, have a look at Dr. Tracy Allen's
web site -- he is without question _the_ BASIC Stamp math guru (and a
great guy, too!).
http://www.emesystems.com/BS2math1.htm
-- Jon Williams
-- Applications Engineer, Parallax
-- Dallas Office
Original Message
From: andy_watson5 [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=c7Er9ARMCRJeDSQ8IbaCCltkrpnmbqIBrLAvLLPZmyLoMuSlwIWm-zbN4dAdndAr6_FslRamiVCeMWLb]andywatson@m...[/url
Sent: Tuesday, June 29, 2004 9:56 AM
To: basicstamps@yahoogroups.com
Subject: [noparse][[/noparse]basicstamps] doing math with non-integers
I've got an equation that I want the stamp to compute:
result = ((0.0514 * adc) - 85.557)
I'm reading the variable adc from my A/D converter, and then I want
to solve the equation above, but apparently I can't use non-integers
in math functions on the stamp, is that correct? So how can I solve
the equation?
to display will be from -50 to 0. Will that be a problem for the
stamp?
--- In basicstamps@yahoogroups.com, "Jon Williams" <jwilliams@p...>
wrote:
> You can get to tenths like this:
>
> result = (adc ** 33685) - 856
> DEBUG "+" + (result.BIT15 * 2)
> result = ABS result
> DEBUG DEC (result / 10), ".", DEC1 result
>
> Just note, though, that if your adc count is less that 1665 that
your
> value will be negative.
>
> For more on BASIC Stamp math tricks, have a look at Dr. Tracy
Allen's
> web site -- he is without question _the_ BASIC Stamp math guru (and
a
> great guy, too!).
>
> http://www.emesystems.com/BS2math1.htm
>
> -- Jon Williams
> -- Applications Engineer, Parallax
> -- Dallas Office
>
>
>
Original Message
> From: andy_watson5 [noparse][[/noparse]mailto:andywatson@m...]
> Sent: Tuesday, June 29, 2004 9:56 AM
> To: basicstamps@yahoogroups.com
> Subject: [noparse][[/noparse]basicstamps] doing math with non-integers
>
>
> I've got an equation that I want the stamp to compute:
>
> result = ((0.0514 * adc) - 85.557)
>
> I'm reading the variable adc from my A/D converter, and then I want
> to solve the equation above, but apparently I can't use non-
integers
> in math functions on the stamp, is that correct? So how can I
solve
> the equation?
values. You can test for negative by looking at Bit15 (see code below).
The ABS operator will take care of making the value positive, because
you can't do division on negative numbers in the BASIC Stamp.
-- Jon Williams
-- Applications Engineer, Parallax
-- Dallas Office
Original Message
From: andy_watson5 [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=TdeCv2HnDnhp0u5PWcT1XgDi59gc52bPz7JtZbhn1QQjhvEyroh7ip4SPUM_-5E5cUy6lWB5jiJd_5RF]andywatson@m...[/url
Sent: Tuesday, June 29, 2004 10:50 AM
To: basicstamps@yahoogroups.com
Subject: [noparse][[/noparse]basicstamps] Re: doing math with non-integers
Actually, all my results need to be negative, since the value I want
to display will be from -50 to 0. Will that be a problem for the
stamp?
--- In basicstamps@yahoogroups.com, "Jon Williams" <jwilliams@p...>
wrote:
> You can get to tenths like this:
>
> result = (adc ** 33685) - 856
> DEBUG "+" + (result.BIT15 * 2)
> result = ABS result
> DEBUG DEC (result / 10), ".", DEC1 result
>
> Just note, though, that if your adc count is less that 1665 that
your
> value will be negative.
>
> For more on BASIC Stamp math tricks, have a look at Dr. Tracy
Allen's
> web site -- he is without question _the_ BASIC Stamp math guru (and
a
> great guy, too!).
>
> http://www.emesystems.com/BS2math1.htm
>
> -- Jon Williams
> -- Applications Engineer, Parallax
> -- Dallas Office
>
>
>
Original Message
> From: andy_watson5 [noparse][[/noparse]mailto:andywatson@m...]
> Sent: Tuesday, June 29, 2004 9:56 AM
> To: basicstamps@yahoogroups.com
> Subject: [noparse][[/noparse]basicstamps] doing math with non-integers
>
>
> I've got an equation that I want the stamp to compute:
>
> result = ((0.0514 * adc) - 85.557)
>
> I'm reading the variable adc from my A/D converter, and then I want
> to solve the equation above, but apparently I can't use non-
integers
> in math functions on the stamp, is that correct? So how can I
solve
> the equation?
To UNSUBSCRIBE, just send mail to:
basicstamps-unsubscribe@yahoogroups.com
from the same email address that you subscribed. Text in the Subject
and Body of the message will be ignored.
Yahoo! Groups Links
This message has been scanned by WebShield. Please report SPAM to
abuse@p....
http://www.awce.com/pak1.htm.
However, there are often ways to handle your numbers with integers. Tracy
Allen is the resident guru on that.
You mention:
((0.0514 * adc) - 85.557)
One strategy would be to decide how much decimal precison you really need
and use fixed point. So if you can live with .01 you could multiply adc*100
and then use 5*adc-856. The problem is that adc*100 must be <65K, so that
limits your range of adc.
Another would be to compute something like:
(51*adc)/100 (but that assumes that 51*adc<65K too). Then you'd have to
settle for subtracting 86.
There are other methods, and I'm sure someone will chime in.
Regards,
Al Williams
AWC
* 8 channels of PWM
http://www.awce.com/pak5.htm
Original Message
From: andy_watson5 [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=afNOyDSIz8rKLL0P6eVQUygW1Cz-Ly4ZjQha-yEFNVG8ZgGBE1W9I6_VcHZ2QOs2ha0i_GEET8Z8Re_bTDM]andywatson@m...[/url
Sent: Tuesday, June 29, 2004 9:56 AM
To: basicstamps@yahoogroups.com
Subject: [noparse][[/noparse]basicstamps] doing math with non-integers
I've got an equation that I want the stamp to compute:
result = ((0.0514 * adc) - 85.557)
I'm reading the variable adc from my A/D converter, and then I want
to solve the equation above, but apparently I can't use non-integers
in math functions on the stamp, is that correct? So how can I solve
the equation?
To UNSUBSCRIBE, just send mail to:
basicstamps-unsubscribe@yahoogroups.com
from the same email address that you subscribed. Text in the Subject and
Body of the message will be ignored.
Yahoo! Groups Links
a negative voltage, right? An ADC chip can only
measure voltages between ground and its VCC --
I'm pretty sure.
--- In basicstamps@yahoogroups.com, "Jon Williams" <jwilliams@p...>
wrote:
> No, just understand that negatives are stored as two's-compliment
> values. You can test for negative by looking at Bit15 (see code
below).
> The ABS operator will take care of making the value positive,
because
> you can't do division on negative numbers in the BASIC Stamp.
>
> -- Jon Williams
> -- Applications Engineer, Parallax
> -- Dallas Office
>
>
>
Original Message
> From: andy_watson5 [noparse][[/noparse]mailto:andywatson@m...]
> Sent: Tuesday, June 29, 2004 10:50 AM
> To: basicstamps@yahoogroups.com
> Subject: [noparse][[/noparse]basicstamps] Re: doing math with non-integers
>
>
> Actually, all my results need to be negative, since the value I
want
> to display will be from -50 to 0. Will that be a problem for the
> stamp?
>
> --- In basicstamps@yahoogroups.com, "Jon Williams" <jwilliams@p...>
> wrote:
> > You can get to tenths like this:
> >
> > result = (adc ** 33685) - 856
> > DEBUG "+" + (result.BIT15 * 2)
> > result = ABS result
> > DEBUG DEC (result / 10), ".", DEC1 result
> >
> > Just note, though, that if your adc count is less that 1665 that
> your
> > value will be negative.
> >
> > For more on BASIC Stamp math tricks, have a look at Dr. Tracy
> Allen's
> > web site -- he is without question _the_ BASIC Stamp math guru
(and
> a
> > great guy, too!).
> >
> > http://www.emesystems.com/BS2math1.htm
> >
> > -- Jon Williams
> > -- Applications Engineer, Parallax
> > -- Dallas Office
> >
> >
> >
Original Message
> > From: andy_watson5 [noparse][[/noparse]mailto:andywatson@m...]
> > Sent: Tuesday, June 29, 2004 9:56 AM
> > To: basicstamps@yahoogroups.com
> > Subject: [noparse][[/noparse]basicstamps] doing math with non-integers
> >
> >
> > I've got an equation that I want the stamp to compute:
> >
> > result = ((0.0514 * adc) - 85.557)
> >
> > I'm reading the variable adc from my A/D converter, and then I
want
> > to solve the equation above, but apparently I can't use non-
> integers
> > in math functions on the stamp, is that correct? So how can I
> solve
> > the equation?
>
>
>
> To UNSUBSCRIBE, just send mail to:
> basicstamps-unsubscribe@yahoogroups.com
> from the same email address that you subscribed. Text in the
Subject
> and Body of the message will be ignored.
>
> Yahoo! Groups Links
>
>
>
>
>
>
>
> This message has been scanned by WebShield. Please report SPAM to
> abuse@p...
Did you end up using the TLC2543? The value from the adc will be
positive but the subsequent subtraction can make it negative, Use
something like the following to display the result with sign and
decimal point:
result = (adc ** 33686) - 856
DEBUG REP "-"\result.bit15, DEC ABS result / 10), ".", DEC1 ABS result
The REP operator prints a negative sign only if the sign bit in 2s
complement is set. Then the rest on the absolute value.
As Jon pointed out, integer division does not work right on twos
complement numbers, and the same thing goes for operators like **, */
and // that have an implied division, However, multiplication,
addition and subtraction do work fine in twos complement.
If your result is expected to be between 0 and -50, the corresponding
adc values are from 692 to 1664, a span of about 1000 counts. The
above formula spans 00.0 to -50.0, pretty good with only a little
loss of precision. If it is important, you could use,
result = (5 * adc) + (9175 ** adc) - 8556
DEBUG REP "-"\result.bit15,DEC ABS result / 100), ".", DEC2 ABS result
to span 00.00 to -50.00. Overkill, but it gives you good basis for
rounding off to 0.05.
-- Tracy
>Actually, all my results need to be negative, since the value I want
>to display will be from -50 to 0. Will that be a problem for the
>stamp?
>
>--- In basicstamps@yahoogroups.com, "Jon Williams" <jwilliams@p...>
>wrote:
>> You can get to tenths like this:
>>
> > result = (adc ** 33685) - 856
> > DEBUG "+" + (result.BIT15 * 2)
>> result = ABS result
> > DEBUG DEC (result / 10), ".", DEC1 result
> >
>> Just note, though, that if your adc count is less that 1665 that
>your
>> value will be negative.
>>
>> For more on BASIC Stamp math tricks, have a look at Dr. Tracy
>Allen's
>> web site -- he is without question _the_ BASIC Stamp math guru (and
>a
>> great guy, too!).
>>
>> http://www.emesystems.com/BS2math1.htm
>>
>> -- Jon Williams
>> -- Applications Engineer, Parallax
>> -- Dallas Office
>>
>>
>>
Original Message
>> From: andy_watson5 [noparse][[/noparse]mailto:andywatson@m...]
>> Sent: Tuesday, June 29, 2004 9:56 AM
>> To: basicstamps@yahoogroups.com
>> Subject: [noparse][[/noparse]basicstamps] doing math with non-integers
>>
>>
>> I've got an equation that I want the stamp to compute:
>>
>> result = ((0.0514 * adc) - 85.557)
>>
>> I'm reading the variable adc from my A/D converter, and then I want
>> to solve the equation above, but apparently I can't use non-
>integers
>> in math functions on the stamp, is that correct? So how can I
>solve
>> the equation?
>
>
>
>To UNSUBSCRIBE, just send mail to:
> basicstamps-unsubscribe@yahoogroups.com
>from the same email address that you subscribed. Text in the
>Subject and Body of the message will be ignored.
>
>Yahoo! Groups Links
>
>
>
>