c++ implementation works modulo small numerical issues