fix the invalid-read to valarray reported by valgrind that can cause segfault on older mac OS