# Has std() enough precision? Classic List Threaded 4 messages Open this post in threaded view
|

## Has std() enough precision?

 The returned value from std should be zero if all the values are equal (since the distance between the mean and the values should be zero). However, the returned value isn't zero:`julia> total0.11008615848501174julia> mean(Float64[total for i in 1:100])0.11008615848501173julia> std(Float64[total for i in 1:100])1.3947701538996772e-17julia> std(Float64[total for i in 1:100]) ≈ zero(Float64)falsejulia> mean(Float64[total for i in 1:100]) ≈ totaltrue`How can I get a more precise result from std()?Best,
Open this post in threaded view
|

## Re: Has std() enough precision?

 This is not an appropriate question for julia-dev. -- JohnOn Sep 7, 2015, at 1:30 AM, Diego Javier Zea <[hidden email]> wrote:The returned value from std should be zero if all the values are equal (since the distance between the mean and the values should be zero). However, the returned value isn't zero:`julia> total0.11008615848501174julia> mean(Float64[total for i in 1:100])0.11008615848501173julia> std(Float64[total for i in 1:100])1.3947701538996772e-17julia> std(Float64[total for i in 1:100]) ≈ zero(Float64)falsejulia> mean(Float64[total for i in 1:100]) ≈ totaltrue`How can I get a more precise result from std()?Best,
 Sorry for that... I confused this list with the julia-users when I sent it (they are next to each other on my google groups). Should I delete it from this list and send the same to the other or is fine enough leaving it here?2015-09-06 20:07 GMT-03:00 John Myles White :This is not an appropriate question for julia-dev. -- JohnOn Sep 7, 2015, at 1:30 AM, Diego Javier Zea <[hidden email]> wrote:The returned value from std should be zero if all the values are equal (since the distance between the mean and the values should be zero). However, the returned value isn't zero:`julia> total0.11008615848501174julia> mean(Float64[total for i in 1:100])0.11008615848501173julia> std(Float64[total for i in 1:100])1.3947701538996772e-17julia> std(Float64[total for i in 1:100]) ≈ zero(Float64)falsejulia> mean(Float64[total for i in 1:100]) ≈ totaltrue`How can I get a more precise result from std()?Best,-- Diego Javier ZeaDepartamento de Ciencia y TecnologíaUniversidad Nacional de Quilmes Roque Saenz Pena 1821876 BernalArgentinaPhone:+541143657100 ext 4135
 John is right that this isn't the right list for asking the question, but here is an answer anyway.It is only true that the standard deviation of a vector with identical entries is zero when there is no rounding errors. When adding elements of different magnitude you will eventually get a rounding error, e.g.julia> err = 0.110086158485011740.11008615848501174julia> err + err + err + err + err + err - 6err-1.1102230246251565e-16bacause the sum grows large relative to err. If you instead split the sum in smaller chunks and sum the chunks first, you can avoid the errorjulia> (err + err + err) + (err + err + err) - 6err0.0This is actually how our sum works and it reduces the error growth in the accumulation, but it has a threshold for the number of elements that is too large to avoid the small error you see in your example.In practice, the safe solution is to never rely on a floting point value being exactly zero. The value 1.3947701538996772e-17 you got should simply be considered zero in application.On Mon, Sep 7, 2015 at 1:27 AM, Diego Javier Zea wrote:Sorry for that... I confused this list with the julia-users when I sent it (they are next to each other on my google groups). Should I delete it from this list and send the same to the other or is fine enough leaving it here?2015-09-06 20:07 GMT-03:00 John Myles White :This is not an appropriate question for julia-dev. -- JohnOn Sep 7, 2015, at 1:30 AM, Diego Javier Zea <[hidden email]> wrote:The returned value from std should be zero if all the values are equal (since the distance between the mean and the values should be zero). However, the returned value isn't zero:`julia> total0.11008615848501174julia> mean(Float64[total for i in 1:100])0.11008615848501173julia> std(Float64[total for i in 1:100])1.3947701538996772e-17julia> std(Float64[total for i in 1:100]) ≈ zero(Float64)falsejulia> mean(Float64[total for i in 1:100]) ≈ totaltrue`How can I get a more precise result from std()?Best,-- Diego Javier ZeaDepartamento de Ciencia y TecnologíaUniversidad Nacional de Quilmes Roque Saenz Pena 1821876 BernalArgentinaPhone:+541143657100 ext 4135