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
16 3832
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.
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(LondD oubleArray, LondDoubleArray + N, ConstDoubleArra y);
or
std::vector<dou ble>ConstDouble Array (LondDoubleArra y, LondDoubleArray +
N);
Thanks a lot for your help, I was getting a bit crazy.
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(LondD oubleArray, LondDoubleArray + N, ConstDoubleArra y);
or
std::vector<dou ble>ConstDouble Array (LondDoubleArra y, LondDoubleArray +
N);
Thanks a lot for your help, I was getting a bit crazy.
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(LondD oubleArray, LondDoubleArray + N, ConstDoubleArra y);
For something like this I can think of no good reason to use a 'for'
loop instead of std::copy.
or std::vector<dou ble>ConstDouble Array (LondDoubleArra y, 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
ConstDoubleArra y is actually a vector. If it is an empty vector, you'd
want to pass std::back_inser ter(ConstDouble Array) as the third argument.
If it's a vector that you've already resized to N elements, you want
ConstDoubleArra y.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.
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(LondD oubleArray, LondDoubleArray + N, ConstDoubleArra y);
For something like this I can think of no good reason to use a 'for'
loop instead of std::copy.
or std::vector<dou ble>ConstDouble Array (LondDoubleArra y, 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
ConstDoubleArra y is actually a vector. If it is an empty vector, you'd
want to pass std::back_inser ter(ConstDouble Array) as the third argument.
If it's a vector that you've already resized to N elements, you want
ConstDoubleArra y.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.
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(LondD oubleArray, LondDoubleArray + N, ConstDoubleArra y);
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.
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(Lo ndDoubleArray, LondDoubleArray + N, ConstDoubleArra y);
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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.
To get around this limitation on double, I'd like to keep the bits of
the double the *same* but change its interpretation to long. I can use
"&" on longs. I tried to use reinterpret_cast for this purpose, but it
returned zero every time.
double...
|
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 const double* are incorrect.
Note that the (long double*) is a pointer to an array of long doubles.
|
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 of an addition is always class A, or double (double +
double can't be changed).
|
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 draw and compute the area of a shape
are stored in a "virtual function table"
and a pointer to the appropriate virtual function table
is stored in every shape object.
The "virtual" functions use this pointer to retrieve
a pointer to the...
|
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() {};
| |
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;
|
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 since I am so very keen to learn to
code in standard C.
Could someone tell me the long double equivalent of atof()? I was
getting some peculiar behaviour in a little bit of math code I am
working with. I thought the problem was in the math...
|
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 "input$char$void"
int input(); //can be compiled to name "input$int$void"
....
int i= 3+'0'+input(); //can be compiled to:
int(3)+int('0')+input$int$void()
|
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 significand bits
(with an implicit leading 1, for 24 total), and 8 exponent bits
double precision: 64 bits including 1 sign bit, 52 significand bits
|
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...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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...
|
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();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |