473,769 Members | 2,355 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

convert data type help

Hi all,
I have a question about datatype converting. Consider the following
types
std::complex<do uble>
and
struct MyComplex
{
double re, im;
}

I have a larger array of std::complex being converted to that of
MyComplex

I have tried std::transform plus a self-defined converting function.
It works but the efficiency is very low.

I also tried the following method.

std::complex<do uble> *a;
MyComplex *b;

//initialize a, b
b = reinterpret_cas t<MyComplex*>(a );

Yes. Now it's so fast!!! However, I found that the address of a and b
are the same after casting. That is, one of the allocated memory will
be lost!!! That's too bad. Please help!
Jul 19 '05 #1
5 4812
Rex_chaos wrote:
Hi all,
I have a question about datatype converting. Consider the following
types
std::complex<do uble>
and
struct MyComplex
{
double re, im;
}

I have a larger array of std::complex being converted to that of
MyComplex

I have tried std::transform plus a self-defined converting function.
It works but the efficiency is very low.

I also tried the following method.

std::complex<do uble> *a;
MyComplex *b;

//initialize a, b
b = reinterpret_cas t<MyComplex*>(a );

Yes. Now it's so fast!!! However, I found that the address of a and b
are the same after casting. That is, one of the allocated memory will
be lost!!! That's too bad. Please help!


I would rather get rid off MyComplex by defining it (using typedef) to be
std::complex<do uble>.

The hack you do with reinterpret cast is at best non-portable, but I would
say it is so non-portable, that applying a compiler/standard library patch
might kill it.

In this case transform is The Right Thing to do. Try to profile the
transformation (in a simple test program) to see what is slow. If the
transform algorithm is slow, you might overcome that with a hand-made loop.
If the actual copying of the elements is slow (or memory allocation for the
new element) that is unfortunately the trade-off you have to pay for using
two types.

--
Attila aka WW
Jul 19 '05 #2
On Wed, 8 Oct 2003 10:31:22 +0300, "Attila Feher"
<at**********@l mf.ericsson.se> wrote:
The hack you do with reinterpret cast is at best non-portable, but I would
say it is so non-portable, that applying a compiler/standard library patch
might kill it.


It is both portable and likely to become standard. See e.g.
http://std.dkuug.dk/jtc1/sc22/wg21/d...002/n1356.html

Tom
Jul 19 '05 #3
On Wed, 08 Oct 2003 12:50:56 +0100, tom_usenet
<to********@hot mail.com> wrote:
On Wed, 8 Oct 2003 10:31:22 +0300, "Attila Feher"
<at**********@ lmf.ericsson.se > wrote:
The hack you do with reinterpret cast is at best non-portable, but I would
say it is so non-portable, that applying a compiler/standard library patch
might kill it.


It is both portable and likely to become standard. See e.g.
http://std.dkuug.dk/jtc1/sc22/wg21/d...002/n1356.html


Actually, scrub that, the reinterpret cast is strictly speaking only
portable if the cast is to an array of double or the C99 type
_Complex, although I doubt there is a platform on which it would fail
to "do the right thing".

However, if sizeof(MyComple x) == sizeof(double) * 2, then it will
work, but it isn't yet standard.

Tom
Jul 19 '05 #4
On 8 Oct 2003 00:20:21 -0700, re*******@21cn. com (Rex_chaos) wrote:
Hi all,
I have a question about datatype converting. Consider the following
types
std::complex<d ouble>
and
struct MyComplex
{
double re, im;
}

I have a larger array of std::complex being converted to that of
MyComplex

I have tried std::transform plus a self-defined converting function.
You should use a converting functor. e.g.

struct ComplexConverte r
{
MyComplex operator()(std: :complex<double > const& c) const
{
MyComplex mc = {c.real(), c.imag()};
return mc;
}
};
It works but the efficiency is very low.
With the above change, efficiency should be much much higher (when
compiled with maximum optimization), since the conversion calls will
be inlined, whereas the calls through a function pointer probably were
not.

I also tried the following method.

std::complex<d ouble> *a;
MyComplex *b;

//initialize a, b
b = reinterpret_cas t<MyComplex*>(a );

Yes. Now it's so fast!!! However, I found that the address of a and b
are the same after casting. That is, one of the allocated memory will
be lost!!!
Well, just take a copy of it! e.g.

MyComplex* sufficientSpace = ...;
std::copy(b, b + numElements, sufficientSpace );
That's too bad. Please help!


std::complex<do uble> isn't a POD type, so the cast isn't guaranteed to
work. However, as long as sizeof(std::com plex<double>) ==
sizeof(MyComple x) == sizeof(double) * 2, it *should* work. Look out
for the C99 _Complex type, which might help you out.

Tom
Jul 19 '05 #5
tom_usenet wrote:
On Wed, 08 Oct 2003 12:50:56 +0100, tom_usenet
<to********@hot mail.com> wrote:
On Wed, 8 Oct 2003 10:31:22 +0300, "Attila Feher"
<at**********@l mf.ericsson.se> wrote:
The hack you do with reinterpret cast is at best non-portable, but
I would say it is so non-portable, that applying a
compiler/standard library patch might kill it.


It is both portable and likely to become standard. See e.g.
http://std.dkuug.dk/jtc1/sc22/wg21/d...002/n1356.html


Actually, scrub that, the reinterpret cast is strictly speaking only
portable if the cast is to an array of double or the C99 type
_Complex, although I doubt there is a platform on which it would fail
to "do the right thing".

However, if sizeof(MyComple x) == sizeof(double) * 2, then it will
work, but it isn't yet standard.


Ahem. *If* the order of the real/imaginary members is the same!

--
Attila aka WW
Jul 19 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
52689
by: jk | last post by:
I'm having trouble converting a datatable into xml, with resonse.write to aspx. I'm basically converting vb code that saved a recordset into a stream into c#, but the format is wrong. I've tried using streams, datadoc, xmlreader, etc with no success. I need to convert it directly to a string or via a stream, but not in a file. Here's some sample code that gives me the bad format. I'll post the good format below that give me rowsets rather...
17
4378
by: David Scemama | last post by:
Hi, I'm writing a program using VB.NET that needs to communicate with a DOS Pascal program than cannot be modified. The communication channel is through some file databases, and I have a huge problem writing VB Double values to the file so as the Pascal program can read them as Pascal Real values. I've managed to find the algorithm to read the Pascal Real format and convert it to a VB Double, but I cannot figure out the opposite...
5
2725
by: manmit.walia | last post by:
Hello All, I am stuck on a conversion problem. I am trying to convert my application which is written in VB.NET to C# because the project I am working on currently is being written in C#. I tried my best to convert it, but somehow the app does not work. I am also not getting any errors when I complie thus, letting me know I am on the write track. Basically what I want to do is edit,add,delete, and update an XML file in a DataGrid. Below...
28
4818
by: MLH | last post by:
The largest integer A97 can deal with is 2,147,483,647, as I understand it from HELP. I would be content to represent larger integers as strings. For example, "2147483648" would suit me fine. I would be interested in converting larger HEX numbers to decimal integer equivalents. For example, a 16-digit HEX number is quite a large decimal value and I would be happy to represent it as a string, as it would suit my purposes fine. Since the...
5
3790
by: Learner | last post by:
Hello, Here is the code snippet I got strucked at. I am unable to convert the below line of code to its equavalent vb.net code. could some one please help me with this? static public List<RoleData> GetRoles() { return GetRoles(null, false); }
1
9912
by: neeraj | last post by:
Hi All Can any give me the code for convert "DataColumn" data type of "DataTable". Even if data table already populated (have data) Actually I am creating one search module which searches the data from "DataView" using "RowFilter" method. Here I m using like operator for searching, its work fine for string but if data type of "Data Column" is Date or Integer then its not working. Now I m decide I will convert data types of all column...
3
8749
by: mrajanikrishna | last post by:
Hi Friends, I am accepting a number from the user entered in a textbox. I want to assign to a variable in my code and assignt this to that variable. double num1 = (double)txtNum1.text; this produced an error
2
8917
by: Ch Pravin | last post by:
Hi All: I am having the following xml which i need to convert to excel using xslt. Please help me out. Afghanistan.xml <?xml version="1.0" encoding="utf-16"?> <Languages BuildVersion="1,5,0815,0 " CountryName="Afghanistan" > <Language locale="English">
2
10014
by: movieking81 | last post by:
Hello, I have gone through some of the other solutions to similar problems posted here, but none seemed to work for me. So, as a last ditch effort I posting this in hopes someone can help. This is what I have. SQL table column terminateddate - varchar type with date stored as mm/dd/yyyy On an ASP page I'm trying to put together a SELECT statement that compares two dates, the one in the table and a requested variable. Here is what I...
0
10781
Debadatta Mishra
by: Debadatta Mishra | last post by:
Introduction In this article I will provide you an approach to manipulate an image file. This article gives you an insight into some tricks in java so that you can conceal sensitive information inside an image, hide your complete image as text ,search for a particular image inside a directory, minimize the size of the image. However this is not a new concept, there is a concept called Steganography which enables to conceal your secret...
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10050
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9866
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3967
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3570
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.