954,504 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

int array to double array

Hi

I'm looking for an efficient way of converting an array of integers to an array of doubles.

Currently I'm doing this:

int *a = new int[1000]; //And then assign values to the indexes
double *b = new double[1000];
for(int i = 0; i < 1000; i++)
{
   b[i] = (double) a[i];
}


But this seems extremely inefficient to me, especially if you do this with large arrays over and over again. Isn't there some other way to do the conversion, such as cast or so?

goocreations
Newbie Poster
10 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

Int and double have completely different memory representations -> No other way of converting the arrays.

Caligulaminus
Junior Poster
106 posts since Feb 2011
Reputation Points: 72
Solved Threads: 30
 

Is there any reason why you can't use double instead of int in the first place? That way the conversion would be unnecessary.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

Yes, there is an external function that takes a double array (and I can't change that function), and I'm using this function.

goocreations
Newbie Poster
10 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

What I mean is, why not use an array of double from the start instead of an array of int, then pass that into the external function? Are you also using an external function that can't be changed which gives you an array of int, or something?

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

Can you modify the function so that it needs ints or make it template?

firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: