411 REFERENCE · Wikibooks
Computer Programming/Physics/Position of an accelerating body function (constant acceleration)
Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.
<Wikisource:Source code
The position of an accelerating body is a mathematical function expressed as:
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)
; eax = a, ebx = v, ecx = t, edx = s
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
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<class Vector,class Number>
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