473,669 Members | 2,421 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 6318
Richard Harnden <ri************ *@googlemail.co mwrites:
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_M AX + 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
3597
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 example, consider this four-bit number: 1100 from this number I want to extract two numbers: 1000 and 100 had the four-bit number been 0101 I would want to extract 100 and 1. How should I do this? I wish I had some code to post but I don't...
29
19587
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 gcc compiler that comes with RedHat linux 9. If not, what data type will yield the largest unsigned integer for me? Thanks for your help,
12
5439
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 addition overflows or underflows? Thanks, -Peter
16
5118
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. And K&R2 mentions "signed extension" everywhere. Reading some old clc posts, I've beginning to realize that these books are over-generalizing the topic. I am just wondering what the difference between the following pairs of terms are: 1)...
5
9136
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 really see anything that I could use. -- Daniel Rudy Email address has been base64 encoded to reduce spam
9
3943
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
5043
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 involved. The problem is that comparisons between signed and unsigned values are machine- dependent, because they depend on the sizes of the various integer types. For example, suppose that int is 16 bits
20
3216
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 <stdio.h> main(){ unsigned char c, temp; c = temp = 0; printf("the range of unsigned char is from %d to ", c);
1
7987
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 understanding why and how a double can be represented in bits and bytes and which of the following can allow us to view the binary representation, unsigned char representation and then convert back to int please... #include<iostream> #include<iomanip>...
0
8465
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
8809
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
8588
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
8658
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
4206
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
4386
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2797
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
2032
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1788
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.