473,783 Members | 2,350 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

convert a double keeping msb ?

hello all,

I have a problem, I have a DOUBLE value like that

4065 4000 0000 0000

and I want to convert it to a DWORD like that:
4065 4000

if I do result=(DWORD)M yDouble it end with AA(170)
wich I don't want.

Believe me or not but I spent 4 hours on this,
I cannot shift the bits, the compiler complaint with double,
it say: "bad left operand" "bad right operand" while it work
well with DWORD.

I'm puzzled, any help appreciated (VC++ 5.0)

Dan
Jul 22 '05 #1
12 2321

"DanSteph" <Da******@caram ail.com> wrote in message
news:40******** **************@ news.free.fr...
hello all,

I have a problem, I have a DOUBLE value like that

4065 4000 0000 0000

and I want to convert it to a DWORD like that:
4065 4000

if I do result=(DWORD)M yDouble it end with AA(170)
wich I don't want.

Believe me or not but I spent 4 hours on this,
I cannot shift the bits, the compiler complaint with double,
it say: "bad left operand" "bad right operand" while it work
well with DWORD.

I'm puzzled, any help appreciated (VC++ 5.0)

Dan


Like this (this is not portable code)

double x = ...;
DWORD msb = *(DWORD*)&x;

if that doesn't work try

DWORD msb = *((DWORD*)&x + 1);

In either case the trick is doing the conversion via a pointer. That avoids
the compiler converting a double value to a DWORD value.

john
Jul 22 '05 #2
John Harrison wrote:

"DanSteph" <Da******@caram ail.com> wrote in message
news:40******** **************@ news.free.fr...
hello all,

I have a problem, I have a DOUBLE value like that

4065 4000 0000 0000

and I want to convert it to a DWORD like that:
4065 4000

if I do result=(DWORD)M yDouble it end with AA(170)
wich I don't want.

Believe me or not but I spent 4 hours on this,
I cannot shift the bits, the compiler complaint with double,
it say: "bad left operand" "bad right operand" while it work
well with DWORD.

I'm puzzled, any help appreciated (VC++ 5.0)

Dan


Like this (this is not portable code)

double x = ...;
DWORD msb = *(DWORD*)&x;

if that doesn't work try

DWORD msb = *((DWORD*)&x + 1);


It is DWORD msb = *((DWORD*)&x + 1); on little endian machines
(e.g. PC).

This one got me thinking: is the result of the above undefined
or implementation-defined behaviour (assuming the size of double
and DWORD as above)? There are two things that I am unsure
about: adjusting a reinterpret_cas t-ed pointer and dereferencing
the resulting value. As far as I know, neither is explicitly
condoned. Just a theoretical interest.

Denis
Jul 22 '05 #3
> >
Like this (this is not portable code)

double x = ...;
DWORD msb = *(DWORD*)&x;

if that doesn't work try

DWORD msb = *((DWORD*)&x + 1);


It is DWORD msb = *((DWORD*)&x + 1); on little endian machines
(e.g. PC).

This one got me thinking: is the result of the above undefined
or implementation-defined behaviour (assuming the size of double
and DWORD as above)? There are two things that I am unsure
about: adjusting a reinterpret_cas t-ed pointer and dereferencing
the resulting value. As far as I know, neither is explicitly
condoned. Just a theoretical interest.

Denis


Its the sort of thing that the standard is really bad at explaining. I
believe that it is undefined behaviour (on both counts) because I've seen
others claim that in this group, I couldn't point you to the sections of the
standard to back that up.

Copying to an array of char is explicitly allowed by the standard however,
so if the OP was concerned about UB then he should copy the double to an
array of char and construct the DWORD from that.

john
Jul 22 '05 #4
Many thanks for your replies,

I finaly woke up in the midle of the night (couln't sleep)
of course pointer was the solution, So I finaly went with that:

DWORD* Ptrweightshort= (DWORD*)&FsStat ionCrew[0].Weight; // this is double
Ptrweightshort+ =1;
DWORD weightshort=*Pt rweightshort;

Nothing as elegant as somes of your solutions but it work.
I'll now try yours.

Many thanks again

Dan
Jul 22 '05 #5
John Harrison wrote:
Like this (this is not portable code)

double x = ...;
DWORD msb = *(DWORD*)&x;

if that doesn't work try

DWORD msb = *((DWORD*)&x + 1);


It is DWORD msb = *((DWORD*)&x + 1); on little endian machines
(e.g. PC).

This one got me thinking: is the result of the above undefined
or implementation-defined behaviour (assuming the size of double
and DWORD as above)? There are two things that I am unsure
about: adjusting a reinterpret_cas t-ed pointer and dereferencing
the resulting value. As far as I know, neither is explicitly
condoned. Just a theoretical interest.


Its the sort of thing that the standard is really bad at explaining. I
believe that it is undefined behaviour (on both counts) because I've seen
others claim that in this group, I couldn't point you to the sections of the
standard to back that up.


It is bad because of aliasing rules. I don't know what a "DWORD" is but
unless it is somehow related to double the code is not correct.
The C++ aliasing rules are something like this:

If the program attempts to access the stored value of an object through
an lvalue of other than one of the following types the behavior is
undefined:

1. the dynamic type of the object,
2. a cv-qualified version of the declared type of the object,
3. a type that is the signed or unsigned type corresponding to the
declared type of the object,
4. a type that is the signed or unsigned type corresponding to a
cv-qualified version of the declared type of the object,
5. an aggregate or union type that includes one of the
aforementioned types among its members (including, recursively, a member
of a subaggregate or contained union),
6. a type that is a (possibly cv-qualified) base class type of the
declared type of the object,
7. a char or unsigned char type.
Jul 22 '05 #6
Bill Seurer wrote:
It is bad because of aliasing rules. I don't know what a "DWORD" is but
unless it is somehow related to double the code is not correct.


DWORD is a double-word (double meaning 2, not double as in double precision
float) in Windows, which is defined as a 32-bit unsigned integral type.
Jul 22 '05 #7

"Bill Seurer" <se****@us.ibm. com> wrote in message
news:cb******** **@news.rchland .ibm.com...
John Harrison wrote:
Like this (this is not portable code)

double x = ...;
DWORD msb = *(DWORD*)&x;

if that doesn't work try

DWORD msb = *((DWORD*)&x + 1);
It is DWORD msb = *((DWORD*)&x + 1); on little endian machines
(e.g. PC).

This one got me thinking: is the result of the above undefined
or implementation-defined behaviour (assuming the size of double
and DWORD as above)? There are two things that I am unsure
about: adjusting a reinterpret_cas t-ed pointer and dereferencing
the resulting value. As far as I know, neither is explicitly
condoned. Just a theoretical interest.
Its the sort of thing that the standard is really bad at explaining. I
believe that it is undefined behaviour (on both counts) because I've seen others claim that in this group, I couldn't point you to the sections of the standard to back that up.


It is bad because of aliasing rules. I don't know what a "DWORD" is but
unless it is somehow related to double the code is not correct.


Its unsigned long.

The C++ aliasing rules are something like this:

If the program attempts to access the stored value of an object through
an lvalue of other than one of the following types the behavior is
undefined:

1. the dynamic type of the object,
2. a cv-qualified version of the declared type of the object,
3. a type that is the signed or unsigned type corresponding to the
declared type of the object,
4. a type that is the signed or unsigned type corresponding to a
cv-qualified version of the declared type of the object,
5. an aggregate or union type that includes one of the
aforementioned types among its members (including, recursively, a member
of a subaggregate or contained union),
6. a type that is a (possibly cv-qualified) base class type of the
declared type of the object,
7. a char or unsigned char type.


Could you give me the C++ standard section? I looked up aliasing in the
index but it didn't help.

john
Jul 22 '05 #8
"DanSteph" <Da******@caram ail.com> wrote in message news:<40******* *************** @news.free.fr>. ..
hello all,

I have a problem, I have a DOUBLE value like that

4065 4000 0000 0000

and I want to convert it to a DWORD like that:
4065 4000

if I do result=(DWORD)M yDouble it end with AA(170)
wich I don't want.

Believe me or not but I spent 4 hours on this,
I cannot shift the bits, the compiler complaint with double,
it say: "bad left operand" "bad right operand" while it work
well with DWORD.

I'm puzzled, any help appreciated (VC++ 5.0)

Dan


Neither the Standard Library nor the language has any type with the
name "DWORD". If this is your own type, post the definition.

-- --
Abstraction is selective ignorance.
-Andrew Koenig
-- --
Jul 22 '05 #9
John Harrison wrote:
"Bill Seurer" <se****@us.ibm. com> wrote in message
news:cb******** **@news.rchland .ibm.com...
1. the dynamic type of the object,
2. a cv-qualified version of the declared type of the object,
3. a type that is the signed or unsigned type corresponding to the
declared type of the object,
4. a type that is the signed or unsigned type corresponding to a
cv-qualified version of the declared type of the object,
5. an aggregate or union type that includes one of the
aforementione d types among its members (including, recursively, a member
of a subaggregate or contained union),
6. a type that is a (possibly cv-qualified) base class type of the
declared type of the object,
7. a char or unsigned char type.


Could you give me the C++ standard section? I looked up aliasing in the
index but it didn't help.


In the April 1995 working paper (which I qouted above) it was section
5.0, paragraph 13
Jul 22 '05 #10

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

Similar topics

4
47072
by: cindy liu | last post by:
Hi, In .Net, how to convert a string to a double? Thanks in advance! Cindy
12
9892
by: Sydex | last post by:
When I compile code I get error C2664: 'Integration::qgaus' : cannot convert parameter 1 from 'double (double)' to 'double (__cdecl *)(double)' in this part : double Integration::quad2d(double (*func)(double,double)) { nfunc = func ; return qgaus(f1,x1,x2);//error there
2
4964
by: Pascal | last post by:
Why does this not work, and how should i do this convert in stead: string x = double.MinValue.ToString(); double y = Convert.ToDouble(x); i get this exception: An unhandled exception of type 'System.OverflowException' occurred in mscorlib.dll Additional information: Value was either too large or too small for a Double. Pascal
1
4548
by: Sam | last post by:
How do I convert Julian Date to Calendar Date in ASP.Net 1.1 based on following guideline found at Internet? To convert Julian date to Gregorian date: double JD = 2299160.5; double Z = Math.Floor(JD+0.5); double W = Math.Floor((Z - 1867216.25)/36524.25); double X = Math.Floor(W/4);
4
11114
by: Daniel Walzenbach | last post by:
Hi, I wonder if somebody could explain me the difference between Double.Parse and Convert.ToDouble. If I'm not mistaken they are implemented differently (I though for a moment they might be the same like cint(anInt) and cType(anInt, System.Int32) but I checked with ildasm) - if I didn't made a mistake. So when to use which syntax? Is there any performance penalty when using the one over the other or does anybody knows any differences? '...
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...
3
2259
by: Eric BOUXIROT | last post by:
hi, i must convert all of these eVC++ prototypes to use with VB.NET.... DLLEXPORT long F_BDO_MessageBoxOK(char *IN_title, char *IN_msg ); DLLEXPORT long F_BDO_MessageBoxOUINON(char *IN_title, char *IN_msg ); DLLEXPORT long F_BDO_CalculAXplusB(short int *IN_Tab_entree ,int IN_taille , double *OUT_Tab_sortie_freq , double *OUT_Tab_sortie, double IN_A,
3
7715
by: PeterK | last post by:
I am trying to set Public overridable CreditlimitS() as System.Data.SqlTypes.SqlMoney to Creditlimit as Double like CreditLimitS=creditlimit and get this error "Value of type double cannot be converted to System.Data.SqlTypes.SqlMoney " How do I get creditlimit into creditlimitS? There seems to be no conversion function. TIA
4
4524
by: Edwin Knoppert | last post by:
In my code i use the text from a textbox and convert it to a double value. I was using Convert.ToDouble() but i'm used to convert comma to dot. This way i can assure the text is correct. However it seems this convert is determined by the local settings and comma is indeed used as decimal separator. Is there another way to convert a dotted value to a double variable? Like 1234.5 and not 1234,5
0
10313
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, 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...
0
9946
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...
0
8968
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, 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...
0
6735
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
5378
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...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4044
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
3643
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2875
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 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...

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.