Re: [Rcpp-devel] int double multiplication

2016-06-21 Thread Matt D.
Hi, On 6/21/2016 16:33, Andreas Prescher wrote: void f(double d) { int i = vector.size() * d; } A quick note to add to the other answers: You don't want to be using `int` here. Especially since you mention a 64-bit platform -- but IMHO it's a bad idea in general. If you'd like a simple

Re: [Rcpp-devel] int double multiplication

2016-06-21 Thread Yongchao Ge
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 anoth

Re: [Rcpp-devel] int double multiplication

2016-06-21 Thread Dirk Eddelbuettel
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 vector; | for(int i = 0; i < 75; i++) { | vector.push_back(i); | } | int i = vector.size() * d; | } | | from

Re: [Rcpp-devel] int double multiplication

2016-06-21 Thread Andreas Prescher
Hello, I thought it was easy to understand. Ok: void foo(double d) { std::vector 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 i

Re: [Rcpp-devel] int double multiplication

2016-06-21 Thread 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 | a

[Rcpp-devel] int double multiplication

2016-06-21 Thread Andreas Prescher
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 system