Computer Programming/Physics/Position of an accelerating body function (constant acceleration)

<Source code

The position of an accelerating body is a mathematical function expressed as:


 * $$ \mathbf{s}(t) = \begin{matrix}\frac{1}{2}\end{matrix} \mathbf{a} t^2 + \mathbf{v}_0 t + \mathbf{s}_0 \quad\ $$  if a is constant.

a is acceleration v is initial velocity t is time s is starting position

It is expressed below in various programming languages

=Assembly=

Intel x86 (32-bit)
push edx mul ecx mul ecx shr eax,1 add DWORD PTR [esp],eax mov eax,ebx mul ecx add DWORD PTR [esp],eax pop eax
 * eax = a, ebx = v, ecx = t, edx = s

Intel x86 (32-bit) using the FPU
somewhere in the data section: a dd 0.0 ; m*s^-2 s dd 0.0 ; m v dd 0.0 ; m*s^-1 t dd 0.0 ; s half dd 0.5 ; scalar

code: push 40000000h fld DWORD PTR [esp] fld t fyl2x fld st(0) frndint fsub st(1),st(0) fxch st(1) f2xm1 fld1 fadd fscale fstp st(1) fld a fmul fld half fmul fld v fld t fmul fadd fld s fadd add esp,4 ffree st(0)

=C= inline float PositionofAcceleratingBody(float a, float v, float t, float s) { return .5 * a * t * t + v * t + s; } = C++= template inline Vector PositionofAcceleratingBody(Vector a,Vector v0,Vector s0,Number t) { return (0.5*a*t+v0)*t+s0; }

=Delphi= function PositionofAcceleratingBody(real a, v, t, s): real; begin result := .5 * a * t * t + v * t + s; end;

=PHP= function PositionofAcceleratingBody($a, $v, $t, $s) {    return .5 * $a * $t * $t + $v * $t + $s; }

=Java= public double positionOfAcceleratingBody(double a, double v, double t, double s){ return a * t * t / 2 + v * t + s; }

=Python= def positionOfAcceleratingBody(a, v, t, s): return .5 * a * (t ** 2) + v * t + s