Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2004-04-06
5
Medium Priority
?
237 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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…
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 and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

688 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