Solved

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

Posted on 2004-04-06
5
229 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them 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.

707 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now