Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2004-04-06
5
Medium Priority
?
242 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 260 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
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.
Suggested Courses

971 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