"""Does the float arithmetic of _steps_clipped land exactly on the target/floor? A miss by 1 ulp means
`eq >= TARGET` is False (or `eq <= -MAXDD` False) and the account takes an extra trade."""
from pa_imp import *
rng=np.random.default_rng(1)
n=5000
g=PA.cost_share(34.79)+ (-(-6.24+1.76)/2/10*0.743/34.79)
risk=0.03; cost=g*risk
print('cost share',g,'cost',cost)
eq=np.zeros(n); alive=np.ones(n,bool); done=np.zeros(n,bool)
miss_up=0; hit_up=0; miss_dn=0; hit_dn=0
for day in range(400):
    a=alive&~done
    if not a.any(): break
    st,win,w=PA._steps_clipped(rng,n,risk,cost,1.0,eq,True,True)
    new=np.where(a,eq+st,eq)
    # classify trades that were designed to land on target / floor
    land_t=a&win&(np.abs(new-PA.TARGET)<1e-9)
    land_f=a&~win&(np.abs(new+PA.MAXDD)<1e-9)
    hit_up+=(land_t&(new>=PA.TARGET)).sum(); miss_up+=(land_t&(new<PA.TARGET)).sum()
    hit_dn+=(land_f&(new<=-PA.MAXDD)).sum(); miss_dn+=(land_f&(new>-PA.MAXDD)).sum()
    eq=new
    alive&=~(a&((eq<=-PA.MAXDD)))
    done|=alive&(eq>=PA.TARGET)
print('designed target landings: counted',hit_up,'missed by float',miss_up)
print('designed floor landings : counted',hit_dn,'missed by float',miss_dn)
