I have another simple task for the svin's math book. Here you go:

Imagine a straight road with two cars. Both cars have a start position (xstart1 and xstart2), as well as (constant) velocities (v1, v2). Note that the velocities can be positive or negative. a negative value indicating the car's moving left, positive value means it's moving right.

You have the following variables:
.data?

; inputs:
xstart1 real4 ? ; car 1 start point
xstart2 real4 ? ; car 2 start point
v1 real4 ? ; car 1 velocity
v2 real4 ? ; car 2 velocity

your task is to determine when and where one car passes the other. So the outputs are:

; outputs:
time real4 ? ; time both cars meet each other
place real4 ? ; place they meet

Time starts at 0, when both cars start moving. Write a piece of code that sets the time variable to the time both cars are at the same point, and store the coordinate (x) of this point in the place variable as well.
You may assume that both cars actually meet (they are not driving away from each other).

I hope the assignment is clear the way I wrote it. I haven't worked out a solution, I'll leave that to you.

Thomas
Posted on 2002-02-08 15:37:02 by Thomas
I really love maths problems, heres a solution.

One car is at x1, the other at x2. The distance between the cars is a simple x2-x1.
Both cars are traveling in different directions, therefore their velocities are of different sign. The relative velocity of car2 to car1 is therefore v1-v2.

Time it takes to travel is d/v = (x2-x1)/(v1-v2) = t.
The postion at which they meet will be t*v1 + x1 = p.

A direct translation of the equations would be
fld x2

fsub x1
fld v1
fsub v2
fdiv
fst t
fmul v1
fadd x1
fstp p


But a more efficient one is
fld x1

fld v1
fld x2
fsub st,st(2)
fld st(1)
fsub v2
fdiv
fst t
fmul
fadd
fstp p
Posted on 2002-02-08 18:08:36 by Eóin
Of course the same size optimizations would apply equally well here. I did know the answer to the problem, too. But I am at work.
Posted on 2002-02-08 18:19:46 by bitRAKE
E?in for now scores 3 (the first, fast, small) since nobody gave other code yet.
Thomas 2 (the first, fast) for previous
bitRake 1 (small) for previous

1. Eyin 3
2. Thomas 2
3. bitRake

:)
Posted on 2002-02-08 21:08:06 by The Svin
E?in's second version yields 46 bytes.
mov eax,offset x1

fld REAL4 PTR [eax][offset x2 - offset x1]
fsub REAL4 PTR [eax][offset x1 - offset x1]
fld REAL4 PTR [eax][offset v1 - offset x1]
fsub REAL4 PTR [eax][offset v2 - offset x1]
fdiv
fst REAL4 PTR [eax][offset t - offset x1]
fmul REAL4 PTR [eax][offset v1 - offset x1]
fadd REAL4 PTR [eax][offset x1 - offset x1]
fstp REAL4 PTR [eax][offset p - offset x1]
bytes 29

Of course, we choose an offset that is used more than once (x1/v1) - saving an additional byte.
Posted on 2002-02-09 00:39:15 by bitRAKE
...and because I'm a macro lovin' fool:
SetBase MACRO xREG:REQ, address:REQ

@CatStr(<__AddressBase__>,xREG) TEXTEQU <&address>
mov xREG, offset address
ENDM

FromBase MACRO xREG:REQ, item:REQ
LOCAL tmpStr
tmpStr TEXTEQU @CatStr(<__AddressBase__>,xREG)
EXITM @CatStr(<[>,xREG,<][>,<offset >,tmpStr,< - >,<offset >,item,<]>)
ENDM


SetBase eax,x1
fld REAL4 PTR FromBase(eax,x2)
fsub REAL4 PTR FromBase(eax,x1)
fld REAL4 PTR FromBase(eax,v1)
fsub REAL4 PTR FromBase(eax,v2)
fdiv
fst REAL4 PTR FromBase(eax,t)
fmul REAL4 PTR FromBase(eax,v1)
fadd REAL4 PTR FromBase(eax,x1)
fstp REAL4 PTR FromBase(eax,p)
Posted on 2002-02-09 00:59:51 by bitRAKE
:alright: Well done both! I'll think of some other math exercises..

Thomas
Posted on 2002-02-09 06:04:33 by Thomas
I've got a much harder one, same a Thomas's but each car also has acceleration.

Now, am I allowed enter code if I knew the answer before posting the question :grin:. Well ye have three hours form now anyway before I can get back on line. :alright:
Posted on 2002-02-09 07:47:49 by Eóin
OK, I'll give ye all a clue, here's an equation which ties together all the variables; distance = velocity*time + (accelereration*time square)/2.

Or as writen in standard notation; s = (u)(t) + (1/2)(a)(t^2)
Posted on 2002-02-09 11:48:59 by Eóin
E?in, do you mean you don't use Tensor Analysis?
What about the effects of gravity?
How much do the cars weigh?
What about friction?
:grin:
Posted on 2002-02-09 12:16:49 by bitRAKE
bitRAKE, Tensor Analysis, what is that even? Friction, no thanks, I'm not in a mood to integrate today. Gravity and weight, actually they'd be easily factored in as they only alter the cars acceleration :) .

But on more important matters, I'm disappointed guys :grin:, here's a solution for ye, when I said earlier that I knew how to do it, well :rolleyes: I ment in theory I knew how to do it. Actually codeing it proved quite difficult.

Basically it involes using the quadratic root equation to find two values for time, you then discard the negative one and use the positive to figure out where the cars meet.

Following is a beautiful bit of FPU code which does just that. I couldn't optimise it beyond trying to keep as many values on the FPU stack as I could, if there was one more register then no value would need to be loaded more than once, but alas thats not so.

fld a1			; Use quadratic equ to find roots

fsub a2 ; (a1-a2)

fld v2
fsub v1 ; (v2-v1)
fst st(4) ; Store for later

fld st
fmul st,st ; (v2-v1)^2

fld st(2) ; (a1-a2)
fld x1
fst st(6) ; Stored for later
fsub x2
fmul ; (a1-a2)(x2-x1)
fadd st,st ; 2(a1-a2)(x2-x1)
fsub ; (v1-v2) - 2(a1-a2)(x2-x1)

fsqrt ; Sqrt((v1-v2) - 2(a1-a2)(x2-x1))

fxch
fld st
fadd st,st(2)
fxch st(2)
fsub ; st = (v2-v1) - Sqrt((v1-v2) - 2(a1-a2)(x2-x1))
; st1 = (v2-v1) + Sqrt((v1-v2) - 2(a1-a2)(x2-x1))
fld st(2)
fmul st(2),st
fmul ; st & st1 * (a1-a2), This gets the correct sign

fcomi st,st(1)
ja @F ; We want to keep the positive root, this will always
fxch ; be the bigger root as the other refers to the past
@@: ; and is therefore negative.
fstp st(3)
fstp st
fmul st,st
fdiv ; Root / (a1-a2)^2, Square necessary to counteract
; earlier multiplication to adjust signs.
fst t

fld st ; Repeat equation to find distance, s = ut + (1/2)at^2
fmul st,st ; t^2
fmul a1 ; at^2
fmul fpc(0.5) ; (1/2)at^2
fxch st(1)
fmul st,st(3) ; * v1, ut
fadd ; ut + (1/2)at^2
fadd ; + x1
fstp p ; And We're done
fstp st ; Remove v1 from stack
Posted on 2002-02-09 18:25:17 by Eóin
i have an addition to the question

add friction and when the cars get to the same place they dont pass each other .. they crash( idont know the english word how to say this so i 'll write the equations)

m1v1+m2v2=m1u1+m2u2
v1+u1=v2+u2
and another situation
m1v1+m2v2=(m1+m2)U

the question when calculate in when will the cars stop and where
in both cases

bye
eko
Posted on 2002-02-10 07:56:01 by eko
eko, I don't think your equations are correct, the first one is the conservation of momentium equation and while it does refeer to two object coliding, it more specificially refeers to when they colide and bounce off in different direction. This won't help you with friction.

Actually I was worng whn I mentioned integration was needed for friction, I was getting friction and air resistance confused. :rolleyes:

For friction you'd need to know the weight of the cars, however once you know that you also have to know what way the cars are traveling. This could be hard as a car traveling right initially but accelerating to the left would eventually stop and start traveling left whereupon the direction friction acts would change.

All in all, a very messy problem.
Posted on 2002-02-10 11:26:26 by Eóin
for firction of course you need to know the weight of the cars ,
but i more focus on crashes than firction .

my equations are correct here is a little explantion ( but i'm sure you know this allready)
Posted on 2002-02-11 12:19:04 by eko
Tensor Analysis is more of a theoretical tool used for physics problems with multiple unknowns in multiple dimensions - popularized by Einsteins general theory.
Posted on 2002-02-11 12:42:42 by bitRAKE
If I recall correctly, invented by Berhard Rienmann, and used by Maxwell's (theory of light) even before the great Albert ;)

Physics rule :)

Greets,
Mav
Posted on 2002-02-11 16:36:22 by Maverick
Thanks bitRAKE and Maverick, at least we don't need it for any of these problems.

eko, I didn't mean to suggest your equation was wrong, and I admit I had mistakenly thought you ment to use the equation with regard to friction. Sorry if I came off a little condesending.

Your idea is actually a good one. I think I'll post it as a propper question. But if you don't mind I'll use balls for the objects instead of cars. After all when cars crash you more so get the idea that they get mangled rather than bounce off each other. :grin:
Posted on 2002-02-12 05:08:48 by Eóin