[Rcpp-devel] int double multiplication

Yongchao Ge yongchao.ge at gmail.com
Tue Jun 21 19:28:28 CEST 2016


In most cases where the rounding is needed, it is better to replace

int i = vector.size() * d;

with

int i = floor(vector.size() * d+0.5);

If it is the floor function that you are looking for, it will be better to
use the following

int i = floor(vector.size() * d+1e-9);

where 1e-9 can be another very small number (but not too small).





On Tue, Jun 21, 2016 at 11:12 AM, Dirk Eddelbuettel <edd at debian.org> wrote:

>
> On 21 June 2016 at 17:01, Andreas Prescher wrote:
> | Hello,
> |
> | I thought it was easy to understand.
> |
> | Ok:
> |
> | void foo(double d) {
> |       std::vector<int> vector;
> |       for(int i = 0; i < 75; i++) {
> |               vector.push_back(i);
> |       }
> |       int i = vector.size() * d;
> | }
> |
> | from R:
> | foo(1/3)
> |
> | foo is inside package built with
> | package skeleton.
> |
> | Hope its more clear now.
>
> Sure. Appears to be a variant of R FAQ 7.31:
>
>
> https://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-doesn_0027t-R-think-these-numbers-are-equal_003f
>
>
> Also google 'what every computer scientist should know about floating
> point' and
> related documents.  The canonical reference is
>
>    http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html
>
> but there are many other related writeups.
>
>
> You can even google for 'R FAQ 7.31' and get hits...
>
> Dirk
>
> | Andreas
> |
> |
> |
> | Am 21.06.2016 um 16:49 schrieb Dirk Eddelbuettel:
> | >
> | > On 21 June 2016 at 16:33, Andreas Prescher wrote:
> | > | Hello,
> | > |
> | > | calling my function from R with
> | > | f(1/3) gives different results
> | > | on ubuntu 12.04 32 bit
> | > | and 14.04 64 bit, e.g:
> | > |
> | > | void f(double d) {
> | > |   int i = vector.size() * d;
> | > | }
> | > | Given vector of size 75
> | > | I get 25 on 14.04 64 bit
> | > | and 24 on 12.04 32 bit.
> | > |
> | > | Size of int is 4 byte,
> | > | double 8 byte on both systems.
> | > |
> | > | Does anybody have an idea
> | > | how to get same result
> | > | on both machines?
> | >
> | > Please provide a _complete_ and _reproducible_ example.
> | >
> | > Dirk
> | >
> | >
> | > |
> | > | Thanks in advance,
> | > |
> | > | Andreas
> | > |
> | > | (Further information:
> | > | 14.04 64 bit machine:
> | > | g++ (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4
> | > | R version 3.0.2 (2013-09-25)
> | > |
> | > | 12.04 32 bit:
> | > | g++ 4.6.3
> | > | R 3.2.2)
> | > |
> | > | _______________________________________________
> | > | Rcpp-devel mailing list
> | > | Rcpp-devel at lists.r-forge.r-project.org
> | > |
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
> | >
> |
> |
> | _______________________________________________
> | Rcpp-devel mailing list
> | Rcpp-devel at lists.r-forge.r-project.org
> | https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
>
> --
> While traveling abroad, the router hung which left me unable to access
> email
> for a few days (and also blocked access to my website, blog, @cranberries,
> ...)
> Please accept my apologies for delayed and/or somewhat brief replies.
> --
> http://dirk.eddelbuettel.com | @eddelbuettel | edd at debian.org
> _______________________________________________
> Rcpp-devel mailing list
> Rcpp-devel at lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20160621/50d8b838/attachment.html>


More information about the Rcpp-devel mailing list