Solved

RE: Convert an array of floats to an array of doubles

Posted on 2004-04-06
5
233 Views
Last Modified: 2010-04-15
Hi there,

Could some one show me an example of converting an array of floats to an array of doubles.

I no VERY little about C and am just trying to hack some code to get a function to work.


Thanks

Steve

0
Comment
Question by:SketchySteve
  • 2
  • 2
5 Comments
 
LVL 4

Accepted Solution

by:
PerryDK earned 65 total points
ID: 10771018
float floatArray[100];
  double doubleArray[100];

  int i;

  //to convert from a float to a double no casting is needed
  for(i = 0; i < 100; i++)
  {
    doubleArray[i] = floatArray[i];
  }

  //now if your converting from a double to a float
  //you will need to do some type casting
  //be aware that some data may lost as
  //floats can store as much precision as doubles
  for(i = 0; i < 100; i++)
  {
    floatArray[i] = (float)(doubleArray[i]);
  }

  //you can technically do this in C
  //although some compilers will give you a warning
  //saying somthing about possible loss of precision
  //It is legal in C and will compile though
  for(i = 0; i < 100; i++)
  {
    floatArray[i] = doubleArray[i];
  }
0
 
LVL 11

Expert Comment

by:avizit
ID: 10771019
Can you try just casting the floats to doubles ..

for example
float a ;
double b ;

a = 1.2 ;

b = (double) a;


do this in a for loop for the whole array

-Abhijit

0
 
LVL 11

Expert Comment

by:avizit
ID: 10771030
guess both of us replied at the same time. :)
0
 
LVL 4

Expert Comment

by:PerryDK
ID: 10771038
if you have a function say
void foo(double* doubleArray)
{
}

you can not pass in an array of floats

like wise if you have a function
void foo2(float* floatArray)
{
}

you can not pass in an array of doubles

The reason for this is floats and doubles are of different sizes.  Most systems floats are 32 bytes wile doubles are 64 bytes.  

So if you have an array of floats and need to pass them to a function that requires an array of doulbes you will have to actually convert your float array to a double array and then pass that in.
0

Featured Post

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
C++ check and remove last word from a string 5 212
sameEnds challenge 3 179
Setting nameservers after res_init fails doing res_query 2 109
What is sub-make ? 2 71
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question