473,511 Members | 16,260 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

unsigned long to double

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.

Does the following actually do what I hope it does?

#include <stdio.h>
#include <math.h>
#include <limits.h>

int main(void)
{
long testme[] = {
1649354396,
-1512698780,
-365331864,
856092536,
2076605992,
-1006418300,
191408540,
1462864659,
-1502144861,
-269042670,
910116600,
2098697218,
-971310087,
325728931,
1622242763,
-1378801075
};
size_t n = sizeof(testme) / sizeof(testme[0]);
size_t i;
unsigned long max = ULONG_MAX;
int wrapped = 0;
double value;
double prior_value = 0.0;

for (i=0; i<n; i++)
{
value = testme[i] + (wrapped * (double) max);

if ( value < prior_value )
{
wrapped++;
value += (double) max;
}

printf("%ld -%g\n", testme[i], value);

prior_value = value;
}

return 0;
}
Sep 4 '08 #1
5 6270
Richard Harnden <ri*************@googlemail.comwrites:
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.

Does the following actually do what I hope it does?
Not quite (I think). It was only by reading the code I could decode
what you intended and if the code was further off the mark I would
have had to guess even more. I may still have guessed wrong!

Here is what I think you are doing: you have some process that
produces a sequence of numbers that you know to be strictly
increasing. However, due to some code you can't change, all you see
is the result after the sequence has been converted to a fixed-size
signed integer (long in this case) and from these numbers you need to
re-create the original sequence.

You can't do this from the data you have unless you add in the
assumption that the increments are never more than the maximum value
of an unsigned long. Give that assumption, your code is almost
correct but:
#include <stdio.h>
#include <math.h>
#include <limits.h>

int main(void)
{
long testme[] = {
1649354396,
-1512698780,
-365331864,
856092536,
2076605992,
-1006418300,
191408540,
1462864659,
-1502144861,
-269042670,
910116600,
2098697218,
-971310087,
325728931,
1622242763,
-1378801075
};
size_t n = sizeof(testme) / sizeof(testme[0]);
size_t i;
unsigned long max = ULONG_MAX;
int wrapped = 0;
double value;
double prior_value = 0.0;

for (i=0; i<n; i++)
{
value = testme[i] + (wrapped * (double) max);
The value to multiply by should be max + 1. Clearly you need to write
that as + (wrapped * ((double)max + 1)) or some such.
if ( value < prior_value )
{
wrapped++;
value += (double) max;
and again you need (double)max + 1 here. I'd define a double holding
(double)ULONG_MAX + 1 and use that instead of max.
}

printf("%ld -%g\n", testme[i], value);

prior_value = value;
}

return 0;
}
There may also be problems using floating point arithmetic, especially
if the sequence is a long one. Can you use unsigned long long instead?

--
Ben.
Sep 4 '08 #2
On Thu, 04 Sep 2008 04:02:18 -0700, Richard Harnden wrote:
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.

Does the following actually do what I hope it does?
Doing this in floating point is a very bad idea. On most systems double
cannot represent all the integers in the range of long, so if you have
large numbers they will get rounded to the nearest multiple of two or
four or eight etc, depending on their mangitude.

It is also much slower.

Converting an unsigned long int to a long int produces implementation
defined results if is outside the range of long int, so you need to test
what the output of the system that put the numbers into the database, but
in most cases, it is sufficient to do:

{
long testme= FOO;
unsigned long corrected;

corrected= testme;
}

HTH
viza
Sep 4 '08 #3
viza wrote:
Doing this in floating point is a very bad idea. On most systems
double cannot represent all the integers in the range of long,
Are you thinking of systems where longs are 64-bits wide?
Sep 4 '08 #4
viza wrote:
... On most systems double
cannot represent all the integers in the range of long, so if you have
I agree that you should not write code which assumes that long
integers can be converted to double without loss of precision.
However, I'm think that you may be overstating your case by using the
word "most". I don't know exact numbers, but it seems to me that
implementations with 32-bit longs are still pretty common. The
standard permits "double" to have insufficient precision to represent
a 32-bit integer, but I doubt that implementations where this is the
case are common. Most implementations I'm familiar with use IEEE
format double precision, which has nearly twice that capacity.
Sep 4 '08 #5
On Thu, 04 Sep 2008 08:33:42 -0700, jameskuyper wrote:
viza wrote:
>On most systems double cannot represent all the integers in the
range of long

I agree that you should not write code which assumes that long integers
can be converted to double without loss of precision. However, I'm think
that you may be overstating your case by using the word "most".
perhaps ..."most systems (that I'm sitting at)"...
:-)
Sep 4 '08 #6

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

Similar topics

7
3581
by: William Payne | last post by:
Hello, I have a variable of type unsigned long. It has a number of bits set (with set I mean they equal one). I need to determine those bits and their position and create new numbers from them. For...
29
19556
by: Richard A. Huebner | last post by:
Is the unsigned long long primitive data type supported in ANSI standard C? I've tried using it a couple of times in standard C, but to no avail. I'm using both MS VIsual C++ 6, as well as the...
12
5415
by: Peter Ammon | last post by:
When I add an unsigned long long and an int, what type do each of the values get promoted to before the addition is performed? What is the type of the resulting expression? What occurs if the...
16
5100
by: TTroy | last post by:
Hello, I'm relatively new to C and have gone through more than 4 books on it. None mentioned anything about integral promotion, arithmetic conversion, value preserving and unsigned preserving. ...
5
9116
by: Daniel Rudy | last post by:
How does one covert a interger number in a unsigned long long int (64-bit) to long double (80-bit) storage? I looked at math.h and I found function that convert double to long long, but didn't...
9
3921
by: luke | last post by:
Hi everybody, please, can someone explain me this behaviour. I have the following piece of code: long long ll; unsigned int i = 2; ll = -1 * i; printf("%lld\n", ll);
7
5024
by: somenath | last post by:
Hi All, I am trying to undestand "Type Conversions" from K&R book.I am not able to understand the bellow mentioned text "Conversion rules are more complicated when unsigned operands are...
20
3189
by: Junmin H. | last post by:
Hello, I am trying to print the range of unsigned char and unsigned int. The one for char is working good, gives me a correct output, however the other one for int doesnt, why?? Thanks #include...
1
7945
by: krishna81m | last post by:
I am a newbie and have been trying to understand conversion from double to int and then back to int using the following code was posted on the c++ google group. Could someone help me out with...
0
7242
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
7138
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
7353
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,...
1
7075
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
7508
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
5662
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
5063
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
3212
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
446
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.