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

Home Posts Topics Members FAQ

long -> double -> long

long i = nnn;
long j;
double d;
d = i;
j = ( long )d;

in this case, i == j ?

thanks.

Aug 28 '07 #1
11 2655
On Tue, 28 Aug 2007 03:09:37 -0000, Steven Woody
<na********@gma il.comwrote in comp.lang.c:
long i = nnn;
long j;
double d;
d = i;
j = ( long )d;
The cast is completely unnecessary, and completely useless. The
conversion is performed automatically on assignment, and cast does not
change that. If the value of the double is outside the range of
values representable in an unsigned long, the behavior is undefined,
with or without the cast.
in this case, i == j ?
Maybe.
thanks.
You're welcome.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Aug 28 '07 #2
In article <11************ *********@z24g2 000prh.googlegr oups.com>,
Steven Woody <na********@gma il.comwrote:
>long i = nnn;
long j;
double d;
d = i;
j = ( long )d;
>in this case, i == j ?
Not necessarily. double pretty much has to be at least 64 bits,
including the sign and exponent; you end up with about 52 bits
of mantisa as the minimum. If the nnn that you are storing
is more than the radix to the power of (1 more than #bits in mantisa)
(e.g., 2^(1+52), then nnn cannot be stored exactly unless
the last (64-(1+#bits in mantisa)) happen to be 0.

This does come up in practice; on SGI and Sun 64 bit machines
programs compiled in 64 bit mode have 64 bit doubles and 64 bit longs.
((1L<<53)+1L) is too large to be stored in a double in such a system.
--
"law -- it's a commodity"
-- Andrew Ryan (The Globe and Mail, 2005/11/26)
Aug 28 '07 #3
On Aug 28, 8:09 am, Steven Woody <narkewo...@gma il.comwrote:
long i = nnn;
long j;
double d;
d = i;
j = ( long )d;

in this case, i == j ?

thanks.
i==j yes it is true.

Aug 28 '07 #4

<ha************ **@gmail.comwro te in message
news:11******** **************@ m37g2000prh.goo glegroups.com.. .
On Aug 28, 8:09 am, Steven Woody <narkewo...@gma il.comwrote:
>long i = nnn;
long j;
double d;
d = i;
j = ( long )d;

in this case, i == j ?

thanks.

i==j yes it is true.
Normally, yes. On some systems all integers representable by a long will be
representable exactly by a double, and so i will always equal j. Change the
double to a float and put it a very high value, and assuming four bytes for
each, you will see that j is now usually approximate.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Aug 28 '07 #5
Malcolm McLean wrote:
>
<ha************ **@gmail.comwro te in message
news:11******** **************@ m37g2000prh.goo glegroups.com.. .
>On Aug 28, 8:09 am, Steven Woody <narkewo...@gma il.comwrote:
>>long i = nnn;
long j;
double d;
d = i;
j = ( long )d;

in this case, i == j ?

thanks.

i==j yes it is true.
Normally, yes. On some systems all integers representable by a long will
be representable exactly by a double, and so i will always equal j.
Change the double to a float and put it a very high value, and assuming
four bytes for each, you will see that j is now usually approximate.
Given a sufficiently high value in that case, j must be approximate.

--
Ian Collins.
Aug 28 '07 #6
On Aug 28, 8:09 am, Steven Woody <narkewo...@gma il.comwrote:
long i = nnn;
long j;
double d;
d = i;
j = ( long )d;

in this case, i == j ?

thanks.
you have nnn to i by using long data type. the output of both i and j
will be 0 because it is not poible to output character through long.

Aug 28 '07 #7
On Aug 28, 11:49 am, rober...@ibd.nr c-cnrc.gc.ca (Walter Roberson)
wrote:
In article <1188270577.057 465.25...@z24g2 000prh.googlegr oups.com>,
Steven Woody <narkewo...@gma il.comwrote:
long i = nnn;
long j;
double d;
d = i;
j = ( long )d;
in this case, i == j ?

Not necessarily. double pretty much has to be at least 64 bits,
including the sign and exponent; you end up with about 52 bits
of mantisa as the minimum. If the nnn that you are storing
is more than the radix to the power of (1 more than #bits in mantisa)
(e.g., 2^(1+52), then nnn cannot be stored exactly unless
the last (64-(1+#bits in mantisa)) happen to be 0.

This does come up in practice; on SGI and Sun 64 bit machines
programs compiled in 64 bit mode have 64 bit doubles and 64 bit longs.
((1L<<53)+1L) is too large to be stored in a double in such a system.
--
"law -- it's a commodity"
-- Andrew Ryan (The Globe and Mail, 2005/11/26)
thanks for all your inputs. i now understan, if nnn is so large that
it can not be presented in a double with zero exponent, it becomes
unexactly. since my system has a 16bit integer and 32bit double, so i
believe this will not happend and i in above code always equals to j.

thanks again.

Aug 28 '07 #8
In article <11************ **********@r23g 2000prd.googleg roups.com>,
Steven Woody <na********@gma il.comwrote:
>On Aug 28, 11:49 am, rober...@ibd.nr c-cnrc.gc.ca (Walter Roberson)
wrote:
>In article <1188270577.057 465.25...@z24g2 000prh.googlegr oups.com>,
Steven Woody <narkewo...@gma il.comwrote:
>long i = nnn;
long j;
double d;
d = i;
j = ( long )d;
in this case, i == j ?
>thanks for all your inputs. i now understan, if nnn is so large that
it can not be presented in a double with zero exponent, it becomes
unexactly.
Ummm, not exactly.

The below relates to the most common means of representing floating
point numbers. There are other schemes that differ a bit in the
details:

Take the integer and write it out in binary, with no leading 0's.
Count the number of bits and subtract 1; the result will be the
"unbiased" exponent used. Internally, a constant will be added to
this exponent to produce a "biased exponent" that will actually
be stored (the reasons for this have to do with storing numbers less
than 1.) Thus, the unbiased exponent will be non-zero for any integer
greater than 1. Now discard the leading 1 bit from the binary
representation of the integer, *leaving any leading 0s there*.
Start storing this binary number into the available
mantissa digits (e.g., 23 bits for IEEE 32 bit floats),
starting from the "left" (highest bit position) and progressing
towards the right. If you run out of binary digits before you
run out of available bits, then you were able to store the integer
exactly in the floating point number; pad any remaining mantissa
bits out with binary 0's. If you run out of available mantissa
bits before you run out of binary digits, then you were not
able to store the integer exactly.

Or, as a simpler wording: count the number of binary digits in
the representation of the integer, and subtract 1 from that count.
If the result is more than the number of mantissa bits available,
then you cannot store the integer exactly.

>since my system has a 16bit integer and 32bit double, so i
believe this will not happend and i in above code always equals to j.
There are two problems with that.

A) Your code is written around -long-, not around -int-, and it is not
valid in C for a long to be as little as 16 bits. long in C requires at
least 32 bits.

B) Secondly, in any conforming C program, it is not valid for
double to be as little as 32 bits: 32 bits is not enough to
achieve the requirement that DBL_DIG (the number of decimal
digits that can be reliably stored) be at least 10;
32 bits is only enough for 6 decimal digits of reliable storage.
The minimum number of bits needed to meet the C90 constraints
on the range and precision of double, is 43.

--
"No one has the right to destroy another person's belief by
demanding empirical evidence." -- Ann Landers
Aug 28 '07 #9
On Aug 29, 1:33 am, rober...@ibd.nr c-cnrc.gc.ca (Walter Roberson)
wrote:
In article <1188319538.100 977.318...@r23g 2000prd.googleg roups.com>,
Steven Woody <narkewo...@gma il.comwrote:
On Aug 28, 11:49 am, rober...@ibd.nr c-cnrc.gc.ca (Walter Roberson)
wrote:
In article <1188270577.057 465.25...@z24g2 000prh.googlegr oups.com>,
Steven Woody <narkewo...@gma il.comwrote:
long i = nnn;
long j;
double d;
d = i;
j = ( long )d;
in this case, i == j ?
thanks for all your inputs. i now understan, if nnn is so large that
it can not be presented in a double with zero exponent, it becomes
unexactly.

Ummm, not exactly.

The below relates to the most common means of representing floating
point numbers. There are other schemes that differ a bit in the
details:

Take the integer and write it out in binary, with no leading 0's.
Count the number of bits and subtract 1; the result will be the
"unbiased" exponent used. Internally, a constant will be added to
this exponent to produce a "biased exponent" that will actually
be stored (the reasons for this have to do with storing numbers less
than 1.) Thus, the unbiased exponent will be non-zero for any integer
greater than 1. Now discard the leading 1 bit from the binary
representation of the integer, *leaving any leading 0s there*.
Start storing this binary number into the available
mantissa digits (e.g., 23 bits for IEEE 32 bit floats),
starting from the "left" (highest bit position) and progressing
towards the right. If you run out of binary digits before you
run out of available bits, then you were able to store the integer
exactly in the floating point number; pad any remaining mantissa
bits out with binary 0's. If you run out of available mantissa
bits before you run out of binary digits, then you were not
able to store the integer exactly.

thanks!
>
Or, as a simpler wording: count the number of binary digits in
the representation of the integer, and subtract 1 from that count.
If the result is more than the number of mantissa bits available,
then you cannot store the integer exactly.
it seems it conflits with above description. by above, you seem to
compare the bits numbers of the integer substracting ( 1 + bits of
leading 1's ) with the bits of mantissa.
>
since my system has a 16bit integer and 32bit double, so i
believe this will not happend and i in above code always equals to j.

There are two problems with that.

A) Your code is written around -long-, not around -int-, and it is not
valid in C for a long to be as little as 16 bits. long in C requires at
least 32 bits.

B) Secondly, in any conforming C program, it is not valid for
double to be as little as 32 bits: 32 bits is not enough to
achieve the requirement that DBL_DIG (the number of decimal
digits that can be reliably stored) be at least 10;
32 bits is only enough for 6 decimal digits of reliable storage.
The minimum number of bits needed to meet the C90 constraints
on the range and precision of double, is 43.

--
"No one has the right to destroy another person's belief by
demanding empirical evidence." -- Ann Landers

Aug 29 '07 #10

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

Similar topics

17
12798
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...
67
3849
by: Serve La | last post by:
I asked if MS has any plans to support C99 in the next VisualC. This is their answer. I think we should whine more :-) We feel that C++ addresses this space sufficiently. In general we have no plans to add any C99 features that duplicate functionality in C++ or conflict with it. That also matches the feedback we have gotten from customers. In fact the non interest in C99 is the clearest feedback I have seen of any issue. The
4
6398
by: Josh Soref | last post by:
It seems that windows file systems can have files dated to 1617, and unfortunately if you call _findfirst/_findnext on such a directory, msvcr80d will assert. Is there some provision for changing msvcr80d so that this assert doesn't happen? Debug Assertion Failed! File: dtoxtm64.c Line: 67
0
910
by: yongauh | last post by:
The following code works properly in the release mode of VC++2005, but in debug mode, an extra bit is set: unsigned long long x, y; double d; unsigned long* ptr; x = 0x7FF7000000000000; d = *(double*)&x; y = *(unsigned long long*)&d;
11
3698
by: Ole Nielsby | last post by:
First, sorry if this is off-topic, not strictly being a C++ issue. I could not find a ng on numerics or serialization and I figure this ng is the closest I can get. Now the question: I want to serialize doubles in human-readable decimal form and be sure I get the exact same binary values when I read them back. (Right now, I don't care about NaN, infinities etc.)
17
4378
by: Terry | last post by:
I have some old VB5 functions that specify types of Long and Double that make calls to an unmanaged 3rd party DLL probably just as old. The source for the DLL is not available. I'm getting some warnings "PInvoke..unbalanced stack..." etc. Reading up a bit on this and trying to understand this particular warning, I find that part of the problem could be in the mismatch of allocated space for the variables, e.g. VB5 Long is not the same as...
3
2495
by: sparks | last post by:
After changing all the variables in a table from long to double. you get 9.1====becomes==== 9.19999980926514 ok I checked microsoft and they had a workaround. export the values to excel...then import them back into the new table. well I tried that and get the same thing. is there a work around for the work around LOL
2
3212
by: Markus Dehmann | last post by:
I have two integers i1 and i2, the second of which is guaranteed to be between 0 and 99, and I encode them into one double: double encoded = (double)i1 + (double)i2 / (double)100; So, for example, 324 and 2 become 324.02. Now I want to decode them using the function given below but it decodes the example as 324 and 1, instead of 324 and 2. Can anyone tell me what's wrong and how to do this right? (my code see
5
6312
by: Richard Harnden | last post by:
Hi, I've got a list of longs which I need to fix. Basically the numbers come from an unsigned long, which will wrap back to zero. I need them unwrapped. Also, my data will come from a database which doesn't have an unsigned long datatype ... they appear as long.
0
7996
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, 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...
0
7928
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8405
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8060
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6735
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...
1
5878
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 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...
0
5441
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();...
1
2430
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
1
1514
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.