I am using the following equation to figure out the peak velocity of a trapezoidal motion profile when the duration, accel/decel and distance are known. I have attached an image file of the source equation, perhaps someone can check if I have written it out properly in Qlarity code.

I am getting strange result when I plug in different accel(ac) and decel(dc) values in to the equation. I expect that the result (the "speed" variable) will always be above 500, and get closer to 500 as the ac/dc values rise because a move of 10000(dist) counts that takes 20 seconds (dur) should have a speed of 500 if the accel/decel is instantaneous. I am getting results that are always near 500, but sometimes they are above and sometimes they are below, and it is not consistent as I raise the ac/dc values. Perhaps my equation is just bad, or perhaps I have written it poorly, or perhaps there's something going on with the precision of float variables??

Notes: I am using the equation at the bottom of the image I posted as the source for this code. It starts " Vs -> "

Vs = speed (peak velocity)

t = dur (total duration)

d = dist (total distance)

I have omitted all portions of the source equation that contain a multiple of (Vf) because it always equals zero for my purposes. Also, the ac and dc values can not be equal or you will get a division by zero error.

**Code:**

dim dist as float

init dist := 10000

dim dur as float

init dur := 20

dim ac as float

init ac :=1000

dim dc as float

init dc :=1100

dim initspeed as float

init initspeed :=0

dim speed as float

speed=(1/(2*(ac-dc)))*((-2*ac*dc*Dur)-(2*dc*initspeed)+SQRT(power((-2*ac*dc*dur)-(2*dc*initspeed),2)-(4*(dc-ac)*((2*ac*dist*dc)+(dc*power(initspeed,2))))))