Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Cast from (long double*) to (const double*)

Reply
Thread Tools

Cast from (long double*) to (const double*)

 
 
ferran
Guest
Posts: n/a
 
      04-10-2004
How can I cast from (long double*) to (const double*)

I have tried:
const double* Value1 = (const double*)Value2;

The compiler does not complain but the actual results when I access
the const double* are incorrect.

Note that the (long double*) is a pointer to an array of long doubles.
 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      04-10-2004

"ferran" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> How can I cast from (long double*) to (const double*)
>
> I have tried:
> const double* Value1 = (const double*)Value2;
>


Well you've answered your own question.

> The compiler does not complain but the actual results when I access
> the const double* are incorrect.
>


Not surprisingly.

> Note that the (long double*) is a pointer to an array of long doubles.


I think the question you meant to ask is how to I *convert* an array of long
doubles to an array of doubles, casting is not helpful as you have already
found out.

The only way to allocate a new array of doubles and copy the long doubles
over to the new array one by one. Something like

double *Value1 = new double[N];
for (int i = 0; i < N; ++i)
Value1[i] = Value2[i];

john


 
Reply With Quote
 
 
 
 
David Harmon
Guest
Posts: n/a
 
      04-10-2004
On Sat, 10 Apr 2004 16:44:44 +0100 in comp.lang.c++, "John Harrison"
<(E-Mail Removed)> wrote,
>The only way to allocate a new array of doubles and copy the long doubles
>over to the new array one by one. Something like
>
>double *Value1 = new double[N];
>for (int i = 0; i < N; ++i)
> Value1[i] = Value2[i];


Surely there is something better than copying one by one.
std::vector<double> Value1( Value2, Value2+N );

Or at the worst
std::copy(Value2, Value2+N, Value1);

Why would you ever write a 'for' loop for that? I don't get it.

 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      04-10-2004
ferran wrote:

> How can I cast from (long double*) to (const double*)
>
> I have tried:
> const double* Value1 = (const double*)Value2;
>
> The compiler does not complain but the actual results when I access
> the const double* are incorrect.


The above cast is only appropriate if you have a long double* that
actually doesn't point to a long double, but to a double.

> Note that the (long double*) is a pointer to an array of long doubles.


I guess you actually want to have an array of double. In this case you
have to create a copy of your array.
 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      04-10-2004

"David Harmon" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Sat, 10 Apr 2004 16:44:44 +0100 in comp.lang.c++, "John Harrison"
> <(E-Mail Removed)> wrote,
> >The only way to allocate a new array of doubles and copy the long doubles
> >over to the new array one by one. Something like
> >
> >double *Value1 = new double[N];
> >for (int i = 0; i < N; ++i)
> > Value1[i] = Value2[i];

>
> Surely there is something better than copying one by one.
> std::vector<double> Value1( Value2, Value2+N );
>
> Or at the worst
> std::copy(Value2, Value2+N, Value1);
>
> Why would you ever write a 'for' loop for that? I don't get it.
>


Granted, but I was just trying to emphasise to the OP that there has to be
some work done to perform the conversion (he was trying to cast), whether
that's done with an explicit loop, or in the internals of std::vector
constructor or std::copy.

john


 
Reply With Quote
 
David Harmon
Guest
Posts: n/a
 
      04-10-2004
On Sat, 10 Apr 2004 18:24:06 +0100 in comp.lang.c++, "John Harrison"
<(E-Mail Removed)> wrote,
>Granted, but I was just trying to emphasise to the OP that there has to be
>some work done to perform the conversion (he was trying to cast), whether
>that's done with an explicit loop, or in the internals of std::vector
>constructor or std::copy.


Good point; certainly the ultimate conversion from long double to double
must be done one at a time somewhere. Perhaps it could be done without
needing to store the whole set of results. We don't know how the
results are to be used, but perhaps some kind of adapter class might
avoid a copying step. I guess if the destination is hard coded to
require an array of double there is not much choice but to build one.

 
Reply With Quote
 
ferran
Guest
Posts: n/a
 
      04-11-2004
Thankyou guys, I have implement a 'FOR' loop in order to copy al the
values from the array of long doubles to one of const doubles, and it
works perfectly.
One last question, besides the looking style, is there any reason why
should I use instead of a 'FOR' loop any of the next statements (that
were mentioned)?

std::copy(LondDoubleArray, LondDoubleArray + N, ConstDoubleArray);
or
std::vector<double>ConstDoubleArray (LondDoubleArray, LondDoubleArray+
N);


Thanks a lot for your help, I was getting a bit crazy.
 
Reply With Quote
 
Kevin Goodsell
Guest
Posts: n/a
 
      04-11-2004
ferran wrote:

> Thankyou guys, I have implement a 'FOR' loop in order to copy al the
> values from the array of long doubles to one of const doubles, and it
> works perfectly.
> One last question, besides the looking style, is there any reason why
> should I use instead of a 'FOR' loop any of the next statements (that
> were mentioned)?
>
> std::copy(LondDoubleArray, LondDoubleArray + N, ConstDoubleArray);


For something like this I can think of no good reason to use a 'for'
loop instead of std::copy.

> or
> std::vector<double>ConstDoubleArray (LondDoubleArray, LondDoubleArray+
> N);


This is fine if a vector is what you want, and still preferable to the
'for' loop, in my opinion.

On the other hand, your std::copy version is not fine if
ConstDoubleArray is actually a vector. If it is an empty vector, you'd
want to pass std::back_inserter(ConstDoubleArray) as the third argument.
If it's a vector that you've already resized to N elements, you want
ConstDoubleArray.begin() as the third argument.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      04-12-2004
Kevin Goodsell wrote:

>> One last question, besides the looking style, is there any reason why
>> should I use instead of a 'FOR' loop any of the next statements (that
>> were mentioned)?
>>
>> std::copy(LondDoubleArray, LondDoubleArray + N, ConstDoubleArray);

>
> For something like this I can think of no good reason to use a 'for'
> loop instead of std::copy.


The OP was asking for a reason to use std::copy instead of a 'for' loop,
not the other way round.

 
Reply With Quote
 
Kevin Goodsell
Guest
Posts: n/a
 
      04-12-2004
Rolf Magnus wrote:
> Kevin Goodsell wrote:
>
>
>>>One last question, besides the looking style, is there any reason why
>>>should I use instead of a 'FOR' loop any of the next statements (that
>>>were mentioned)?
>>>
>>>std::copy(LondDoubleArray, LondDoubleArray + N, ConstDoubleArray);

>>
>>For something like this I can think of no good reason to use a 'for'
>>loop instead of std::copy.

>
>
> The OP was asking for a reason to use std::copy instead of a 'for' loop,
> not the other way round.
>


Yeah, it looks like I misread it.

The reasons are that they are shorter, simpler, easier to understand,
and harder to get wrong than the 'for' loop.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is the result of valid dynamic cast always equal to the result ofcorrespondent static cast? Pavel C++ 7 09-18-2010 11:35 PM
error C2440: 'return' : cannot convert from 'const char *' to 'const unsigned short *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast Abhijit Bhadra C++ 2 12-01-2004 04:43 PM
malloc - to cast or not to cast, that is the question... EvilRix C Programming 8 02-14-2004 12:08 PM
to cast or not to cast malloc ? MSG C Programming 38 02-10-2004 03:13 PM
how to cast system.intptr to struct BestNews ASP .Net 1 09-03-2003 09:04 AM



Advertisments