473,414 Members | 1,716 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

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)MyDouble 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 2286

"DanSteph" <Da******@caramail.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)MyDouble 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******@caramail.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)MyDouble 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_cast-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_cast-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*)&FsStationCrew[0].Weight; // this is double
Ptrweightshort+=1;
DWORD weightshort=*Ptrweightshort;

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_cast-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_cast-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******@caramail.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)MyDouble 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
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.


In the April 1995 working paper (which I qouted above) it was section
5.0, paragraph 13
Jul 22 '05 #10
DanSteph wrote:
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)MyDouble 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.


Hi, maybe this can help (used union to split 4 bytes into 2x2 bytes:

#include <iostream>
using std::cout;
using std::endl;

union mysplit {
unsigned int d;
unsigned short int i [2];
};

void splitme (unsigned int myint, unsigned short int &low, unsigned
short int &high) {

mysplit temp;
temp.d=myint;
low=temp.i[0];
high=temp.i[1];
}

int main () {
unsigned short int testint1, testint2;
unsigned int testint = 2864434397;

//AA BB CC DD ... BUT COMPILER WARNS (C90 compatible only)
//is just for testing

splitme (testint, testint1, testint2);
cout << testint1 << " ; " << testint2 << endl;
/* testint1: CCDD = 52445,
testint2: AABB = 43707 */

return 0;
}

regards marbac
Jul 22 '05 #11
DanSteph wrote:
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


Hi, maybe this can help (used union to split 4 bytes into 2x2 bytes)
But i did it with int and short int:

#include <iostream>
using std::cout;
using std::endl;

union mysplit {
unsigned int d;
unsigned short int i [2];
};

void splitme (unsigned int myint, unsigned short int &low, unsigned
short int &high) {

mysplit temp;
temp.d=myint;
low=temp.i[0];
high=temp.i[1];
}

int main () {
unsigned short int testint1, testint2;
unsigned int testint = 2864434397;

//AA BB CC DD ... BUT COMPILER WARNS (C90 compatible only)
//is just for testing

splitme (testint, testint1, testint2);
cout << testint1 << " ; " << testint2 << endl;
/* testint1: CCDD = 52445,
testint2: AABB = 43707 */

return 0;
}

regards marbac
Jul 22 '05 #12
DanSteph wrote:
hello all, 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.

Hi, maybe this can help (used union to split 4 bytes into 2x2 bytes)
But i did it with int and short int.
#include <iostream>
using std::cout;
using std::endl;

union mysplit {
unsigned int d;
unsigned short int i [2];
};

void splitme (unsigned int myint, unsigned short int &low, unsigned
short int &high) {

mysplit temp;
temp.d=myint;
low=temp.i[0];
high=temp.i[1];
}

int main () {
unsigned short int testint1, testint2;
unsigned int testint = 2864434397;

//AA BB CC DD ... BUT COMPILER WARNS (C90 compatible only)
//is just for testing

splitme (testint, testint1, testint2);
cout << testint1 << " ; " << testint2 << endl;
/* testint1: CCDD = 52445,
testint2: AABB = 43707 */

return 0;
}
I hope that i understood you correctly.

double has a sign, "mantisse" and an exponent ... maybe you want to
extract those?

regards marbac?
Jul 22 '05 #13

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

Similar topics

4
by: cindy liu | last post by:
Hi, In .Net, how to convert a string to a double? Thanks in advance! Cindy
12
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...
2
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...
1
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 =...
4
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...
17
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...
3
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,...
3
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...
4
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
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,...
0
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...

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.