Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.sys.acorn.programmer > #5995
| Date | 2020-02-23 20:46 +0000 |
|---|---|
| From | Matthew Phillips <spam2011m@yahoo.co.uk> |
| Newsgroups | comp.sys.acorn.programmer |
| Subject | Re: Subtracting times |
| Message-ID | <62b9fa4658.Matthew@sinenomine.freeserve.co.uk> (permalink) |
| References | <37f8364458.Alan.Adams@ArmX6.adamshome.org.uk> <757ca84458.Matthew@sinenomine.freeserve.co.uk> <4e83b04458.Alan.Adams@ArmX6.adamshome.org.uk> |
In message <4e83b04458.Alan.Adams@ArmX6.adamshome.org.uk>
on 19 Feb 2020 Alan Adams wrote:
> In message <757ca84458.Matthew@sinenomine.freeserve.co.uk>
> Matthew Phillips <spam2011m@yahoo.co.uk> wrote:
>
> > In message <37f8364458.Alan.Adams@ArmX6.adamshome.org.uk>
> > on 18 Feb 2020 Alan Adams wrote:
>
> >> My head is hurting.
> >>
> >> I need a BASIC routine to subtract one 5-byte time from another and return
> >> a correctly signed result.
> >>
> >> Complications arise at the point where the low word goes from &7fffffff to
> >> &80000000, and where the high byte increases.
> >>
> >> BASIC doesn't allow access to the carry or borrow from arithmetic
> >> operations.
> >>
> >> I've developed the following which *seems* to work correctly. However I'm
> >> not sure about all the edge cases.
>
> > I don't think it's doing quite what you want it to do. From the above, my
> > understanding is that you want a signed result, so that if stamp1% is
> > three centiseconds bigger than stamp2% you want a positive number (listing
> > the bytes in memory order):
>
> > 03 00 00 00 00
>
> > and if stamp1% is three centiseconds less than stamp2% you want a negative
> > result:
>
> > FD FF FF FF FF
>
> > I also take it that you want a five-byte result, but that in your use case
> > four bytes would always contain the differences.
>
> I do want a 4-byte result - the final stage after calling this is
>
> interval%=!resultbuf%. i need this value to be correctly signed.
When you say "correctly signed" do you mean that you always want the result
to be positive, so that it expresses the difference in time between stamp1%
and stamp2% regardless of which is the earlier?
Or do you mean that you want the answer to be negative if stamp1% is
earlier than stamp2% and positive if stamp2% is earlier?
I was assuming the latter from your other postings, because you say that you
need to detect the condition of the finisher knocking a button before the
starter starts someone.
If you want the result to be +/- as I had assumed, then you need to get rid
of the bit that says
IF L% < 0 THEN
resultbuf%!0 = -(resultbuf%!0)
ENDIF
That will not be helping. As I said...
> > If you want a signed result, you need to remove the following three
> > lines:
>
> >> IF L% < 0 THEN
> >> resultbuf%!0 = -(resultbuf%!0)
> >> ENDIF
>
> That bit is intended to ensure that the 40-byte part is correctly signed.
You get a correctly signed plus or minus result by just looping through the
five bytes doing the subtraction and borrowing. That is the beauty of
two's-complement representation of negative numbers: exactly the same
operation is done at the byte level no matter whether it is signed or
unsigned arithmentic. The only difference is in the detection of carrying or
overflow. You've already made it clear that the times you are dealing with a
relatively close to each other (within a day or two, say) so you will not get
any overflow from subtracting them and the top byte can be ignored
completely.
> The following shows why this makes my head hurt.
>
> adding two positive numbers shouldn't produce a negative result.
>
> >A%=&7FFFFFFF
> >PRINT A%
> 2.14748365E9
> >B%=A%+1
> >PRINT B%
> -2.14748365E9
Well, that's a quirk of BBC BASIC that we were discussing a few weeks ago.
BASIC stupidly, in my opinion, allows you to do arithmetic on signed values
without warning about overflows.
I suggest you use:
PROCsubstamp(resultbuf%,stamp1%,stamp2%)
LOCAL I%,J%,K%,L%,borrow%
borrow%=0
FOR I%=0 TO3
J%=stamp1%?I%
K%=stamp2%?I%
L%=J%-K%-borrow%
resultbuf%?I%=L%AND&FF
IF L% < 0 THEN borrow%=1 ELSE borrow%=0
NEXT
ENDPROC
And then your answer can be read from !resultbuf% as a 4-byte signed value.
This will only work if the two timestamps are within 497 days of each other.
You are probably worried what will happen when we have these two timestamps:
A: 57FFFFFFFF
B: 5800000000
B is one centisecond higher than A.
Take FF off 0 and we get 1, borrow one.
Take FF and a borrow off 0 and we get 0
Take FF and a borrow off 0 and we get 0
Take FF and a borrow off 0 and we get 0
Take 57 and a borrow off 58 and we also get 0, but you're only looping from 0
to 3 so we don't even calculate this bit.
Therefore you get the answer 00000001 which is what you want.
Similarly if you take B off A, you will get FFFFFFFF which is -1 and is
correct.
If you know your answer is always going to fit in 4 bytes, just ditch all the
code after the end of your FOR-NEXT loop.
--
Matthew Phillips
Durham
Back to comp.sys.acorn.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Subtracting times Alan Adams <alan@adamshome.org.uk> - 2020-02-18 11:55 +0000
Re: Subtracting times Jean-Michel <jmc.bruck@orange.fr> - 2020-02-18 19:11 +0100
Re: Subtracting times Steve Drain <steve@kappa.me.uk> - 2020-02-21 03:28 -0800
Re: Subtracting times Alan Adams <alan@adamshome.org.uk> - 2020-02-21 12:45 +0000
Re: Subtracting times Alan Adams <alan@adamshome.org.uk> - 2020-02-21 15:17 +0000
Re: Subtracting times Steve Drain <steve@kappa.me.uk> - 2020-02-22 10:37 +0000
Re: Subtracting times Steve Drain <steve@kappa.me.uk> - 2020-02-25 16:05 +0000
Re: Subtracting times Alan Adams <alan@adamshome.org.uk> - 2020-02-25 16:35 +0000
Re: Subtracting times Steve Drain <steve@kappa.me.uk> - 2020-02-27 17:10 +0000
Re: Subtracting times Steve Drain <steve@kappa.me.uk> - 2020-02-27 10:06 -0800
Re: Subtracting times Alan Adams <alan@adamshome.org.uk> - 2020-02-28 11:21 +0000
Re: Subtracting times jeffrey.a.doggett@gmail.com - 2020-04-22 12:00 -0700
Re: Subtracting times Martin <News03@avisoft.f9.co.uk> - 2020-04-23 00:07 +0100
Re: Subtracting times jeffrey.a.doggett@gmail.com - 2020-04-23 03:13 -0700
Re: Subtracting times Martin <News03@avisoft.f9.co.uk> - 2020-04-23 18:01 +0100
Re: Subtracting times "John Williams (News)" <UCEbin@tiscali.co.uk> - 2020-04-23 18:06 +0100
Re: Subtracting times jeffrey.a.doggett@gmail.com - 2020-04-23 11:18 -0700
Re: Subtracting times Martin <News03@avisoft.f9.co.uk> - 2020-04-23 21:23 +0100
Re: Subtracting times David Higton <dave@davehigton.me.uk> - 2020-04-24 15:24 +0100
Re: Subtracting times David Higton <dave@davehigton.me.uk> - 2020-04-23 20:57 +0100
Re: Subtracting times jgh@mdfs.net - 2020-02-23 17:39 -0800
Re: Subtracting times Steve Drain <steve@kappa.me.uk> - 2020-02-24 10:23 +0000
Re: Subtracting times Alan Adams <alan@adamshome.org.uk> - 2020-02-24 11:12 +0000
Re: Subtracting times Martin <News03@avisoft.f9.co.uk> - 2020-02-24 13:05 +0000
Re: Subtracting times Matthew Phillips <spam2011m@yahoo.co.uk> - 2020-02-19 08:35 +0000
Re: Subtracting times Alan Adams <alan@adamshome.org.uk> - 2020-02-19 10:03 +0000
Re: Subtracting times Sebastian Barthel <naitsabes@freenet.de> - 2020-02-19 13:04 +0000
Re: Subtracting times Alan Adams <alan@adamshome.org.uk> - 2020-02-19 16:41 +0000
Re: Subtracting times jgh@mdfs.net - 2020-02-19 12:01 -0800
Re: Subtracting times Matthew Phillips <spam2011m@yahoo.co.uk> - 2020-02-23 20:46 +0000
Re: Subtracting times Martin <News03@avisoft.f9.co.uk> - 2020-02-19 23:54 +0000
Re: Subtracting times Alan Adams <alan@adamshome.org.uk> - 2020-02-20 11:18 +0000
csiph-web