Computer Programming/Physics/Position of an accelerating body function (constant acceleration)
Appearance
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
[edit | edit source]Intel x86 (32-bit)
[edit | edit source]; 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
[edit | edit source]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
[edit | edit source]inline float PositionofAcceleratingBody(float a, float v, float t, float s) { return .5 * a * t * t + v * t + s; }
C++
[edit | edit source]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
[edit | edit source]function PositionofAcceleratingBody(real a, v, t, s): real; begin result := .5 * a * t * t + v * t + s; end;
PHP
[edit | edit source]function PositionofAcceleratingBody($a, $v, $t, $s) { return .5 * $a * $t * $t + $v * $t + $s; }
Java
[edit | edit source]public double positionOfAcceleratingBody(double a, double v, double t, double s){ return a * t * t / 2 + v * t + s; }
Python
[edit | edit source]def positionOfAcceleratingBody(a, v, t, s): return .5 * a * (t ** 2) + v * t + s