Quantcast

problem with sum function

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

problem with sum function

Mark A. Albins
Hi!

I'm running R version 2.13.0 (2011-04-13)
Platform: i386-pc-mingw32/i386 (32-bit)

When i type in the command:

sum(c(-0.2, 0.8, 0.8, -3.2, 1.8))

R returns the value:

-5.551115e-17

Why doesn't R return zero in this case?  There shouldn't be any rounding
error in a simple sum.

Thanks,

Mark

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: problem with sum function

Sarah Goslee
Of course there's rounding error: your computer can't
store those decimal numbers precisely. See R FAQ 7.31 for
details.

See also:
sum(10*c(-0.2, 0.8, 0.8, -3.2, 1.8)) / 10

Sarah

On Thu, Mar 1, 2012 at 4:49 PM, Mark A. Albins <[hidden email]> wrote:

> Hi!
>
> I'm running R version 2.13.0 (2011-04-13)
> Platform: i386-pc-mingw32/i386 (32-bit)
>
> When i type in the command:
>
> sum(c(-0.2, 0.8, 0.8, -3.2, 1.8))
>
> R returns the value:
>
> -5.551115e-17
>
> Why doesn't R return zero in this case?  There shouldn't be any rounding
> error in a simple sum.
>
> Thanks,
>
> Mark

--
Sarah Goslee
http://www.functionaldiversity.org

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: problem with sum function

William Dunlap
In reply to this post by Mark A. Albins
In base ten, using any fixed number of digits, compute
   1/3 + 1/3 + 1/3
(doing the divisions before the additions).
Why isn't it 1?

1/5 has the same sort of problem in base two.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com

> -----Original Message-----
> From: [hidden email] [mailto:[hidden email]] On Behalf Of Mark A. Albins
> Sent: Thursday, March 01, 2012 1:50 PM
> To: [hidden email]
> Subject: [R] problem with sum function
>
> Hi!
>
> I'm running R version 2.13.0 (2011-04-13)
> Platform: i386-pc-mingw32/i386 (32-bit)
>
> When i type in the command:
>
> sum(c(-0.2, 0.8, 0.8, -3.2, 1.8))
>
> R returns the value:
>
> -5.551115e-17
>
> Why doesn't R return zero in this case?  There shouldn't be any rounding
> error in a simple sum.
>
> Thanks,
>
> Mark
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
MRD
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: problem with sum function

MRD
This post has NOT been accepted by the mailing list yet.
In reply to this post by Mark A. Albins
sum(-0.2, 0.8, 0.8, -3.2, 1.8) =0

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: problem with sum function

Petr Savicky
In reply to this post by Mark A. Albins
On Thu, Mar 01, 2012 at 01:49:44PM -0800, Mark A. Albins wrote:

> Hi!
>
> I'm running R version 2.13.0 (2011-04-13)
> Platform: i386-pc-mingw32/i386 (32-bit)
>
> When i type in the command:
>
> sum(c(-0.2, 0.8, 0.8, -3.2, 1.8))
>
> R returns the value:
>
> -5.551115e-17
>
> Why doesn't R return zero in this case?  There shouldn't be any rounding
> error in a simple sum.

Hi.

There is a rounding error, since numerical values are represented
in binary system and, for example, 0.2 = 1/5 cannot be represented
in binary exactly. A simpler version is

  0.1 + 0.2 - 0.3

  [1] 5.551115e-17

Use round(), for example

  round(sum(c(-0.2, 0.8, 0.8, -3.2, 1.8)), digits=7)

  [1] 0

See FAQ 7.31 and/or

  http://rwiki.sciviews.org/doku.php?id=misc:r_accuracy

for further hints.

Hope this helps.

Petr Savicky.

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: problem with sum function

Petr Savicky
In reply to this post by Sarah Goslee
On Thu, Mar 01, 2012 at 04:55:55PM -0500, Sarah Goslee wrote:
> Of course there's rounding error: your computer can't
> store those decimal numbers precisely. See R FAQ 7.31 for
> details.
>
> See also:
> sum(10*c(-0.2, 0.8, 0.8, -3.2, 1.8)) / 10

Hi.

This is 0. This works without rounding for one digit precision,
since we always have i == 10*(i/10). Already for two digits, we
may have i != 100*(i/100). So, for example

  sum(100*c(0.28, -0.21, 0.66, -0.73))/100
  [1] 3.552714e-17

Rounding the multiples to be integers yields the expected result

  sum(round(100*c(0.28, -0.21, 0.66, -0.73)))/100
  [1] 0

Petr Savicky.

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: problem with sum function

glsnow
In reply to this post by Mark A. Albins
Others explained why it happens, but you might want to look at the
zapsmall function for one way to deal with it.

On Thu, Mar 1, 2012 at 2:49 PM, Mark A. Albins <[hidden email]> wrote:

> Hi!
>
> I'm running R version 2.13.0 (2011-04-13)
> Platform: i386-pc-mingw32/i386 (32-bit)
>
> When i type in the command:
>
> sum(c(-0.2, 0.8, 0.8, -3.2, 1.8))
>
> R returns the value:
>
> -5.551115e-17
>
> Why doesn't R return zero in this case?  There shouldn't be any rounding
> error in a simple sum.
>
> Thanks,
>
> Mark
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



--
Gregory (Greg) L. Snow Ph.D.
[hidden email]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Loading...