R
Richard Harnden
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 + (wrapped * (double) max);
if ( value < prior_value )
{
wrapped++;
value += (double) max;
}
printf("%ld -> %g\n", testme, value);
prior_value = value;
}
return 0;
}
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 + (wrapped * (double) max);
if ( value < prior_value )
{
wrapped++;
value += (double) max;
}
printf("%ld -> %g\n", testme, value);
prior_value = value;
}
return 0;
}