473,499 Members | 1,659 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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.
Jul 22 '05 #1
16 3797

"ferran" <fe*****@yahoo.com> wrote in message
news:53**************************@posting.google.c 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
Jul 22 '05 #2

"ferran" <fe*****@yahoo.com> wrote in message
news:53**************************@posting.google.c 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
Jul 22 '05 #3
On Sat, 10 Apr 2004 16:44:44 +0100 in comp.lang.c++, "John Harrison"
<jo*************@hotmail.com> 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.

Jul 22 '05 #4
On Sat, 10 Apr 2004 16:44:44 +0100 in comp.lang.c++, "John Harrison"
<jo*************@hotmail.com> 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.

Jul 22 '05 #5
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.
Jul 22 '05 #6
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.
Jul 22 '05 #7

"David Harmon" <so****@netcom.com> wrote in message
news:40***************@news.west.earthlink.net...
On Sat, 10 Apr 2004 16:44:44 +0100 in comp.lang.c++, "John Harrison"
<jo*************@hotmail.com> 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
Jul 22 '05 #8

"David Harmon" <so****@netcom.com> wrote in message
news:40***************@news.west.earthlink.net...
On Sat, 10 Apr 2004 16:44:44 +0100 in comp.lang.c++, "John Harrison"
<jo*************@hotmail.com> 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
Jul 22 '05 #9
On Sat, 10 Apr 2004 18:24:06 +0100 in comp.lang.c++, "John Harrison"
<jo*************@hotmail.com> 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.

Jul 22 '05 #10
On Sat, 10 Apr 2004 18:24:06 +0100 in comp.lang.c++, "John Harrison"
<jo*************@hotmail.com> 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.

Jul 22 '05 #11
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.
Jul 22 '05 #12
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.
Jul 22 '05 #13
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.
Jul 22 '05 #14
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.
Jul 22 '05 #15
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.

Jul 22 '05 #16
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.
Jul 22 '05 #17

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

Similar topics

17
12756
by: Suzanne Vogel | last post by:
I'd like to convert a double to a binary representation. I can use the "&" bit operation with a bit mask to convert *non* float types to binary representations, but I can't use "&" on doubles. ...
16
1429
by: ferran | last post by:
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...
5
1493
by: Gernot Frisch | last post by:
Hi, I have a problem. I want to be able to write this: a = b + d + c; Where a can be a double or class A; b,d,c can each indiviually be one of these: double, class A or class B. The result...
6
1517
by: E. Robert Tisdale | last post by:
The following example shows how an Object Oriented C programmer might implement run-time polymorphism. The Circle "class" is "derived" from the Shape "class". Pointers to functions which actually...
9
2353
by: Nicolas Blais | last post by:
Hi, I have this following class which I use as a timer: #include <sys/time.h> using namespace std; class chrono { public: chrono() {};
16
11197
by: Martin Jørgensen | last post by:
Hi, Short question: Any particular reason for why I'm getting a warning here: (cast from function call of type int to non-matching type double) xdouble = (double)rand()/(double)RAND_MAX;
5
6529
by: lcw1964 | last post by:
Greetings again, I will burden the group with yet another tenderfoot question, but since conscientious googling hasn't yield a lucid answer I thought I would risk the shortcut of asking here...
10
2575
by: Grizlyk | last post by:
1. Can abybody explain me why C++ function can not be overloaded by its return type? Return type can has priority higher than casting rules. For example: char input(); //can be compiled to name...
0
2537
by: Charles Coldwell | last post by:
James Kanze <james.kanze@gmail.comwrites: True, with some additional considerations. The commonly used IEEE 754 floating point formats are single precision: 32 bits including 1 sign bit, 23...
0
7134
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,...
0
7180
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
5485
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4921
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...
0
4609
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...
0
3103
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1429
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 ...
1
667
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
311
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.