In addition to the above comment about the NSNull stuff, you could also replace these 6 lines with these two (unnecessary, but thought I'd list it here for fun).

box[i] = @(MIN(val, [box[i] doubleValue]));
box[i+2] = @(MAX(val, [box[i+2] doubleValue]));