![]() |
|
#1
|
|||
|
|||
combining localtime() members into a single long longHi,
I am trying to create one long integer to use as a timestamp for user input enties rather that store several values which can be obtained from localtime(). ie: 200705091335 which would be handy for sorting and only use 4 bytes. Things were moving along well until I ran into a problem with the long integer. I think it's my printf specifiers, but not sure, when I go over limit I get 20 place! (see below) I can't figure what else I could change there. The longest I could think of doing was a printf( %llu ) as below. Of course you may also know a better way of trying to get that int! Heres what I'm trying: CPP / C++ / C Code:
|
|
#2
|
|||
|
|||
Re: combining localtime() members into a single long longawww man,,, I only had to cast it correctly!....
CPP / C++ / C Code:
|
|
#3
|
||||
|
||||
Re: combining localtime() members into a single long longOut of curiosity, what do you use to compile?
I get 'forbidden' when using a back-2-back long long, except in one environment, see below: VC++ 6: Quote:
Quote:
Code:
Quote:
__________________
Use the force...read the source!! WYCIWYG -- what you code is what you get! |
|
#4
|
|||
|
|||
Re: combining localtime() members into a single long longQuote:
There are proprietary data types for 64-bit integers. Look in the documentation. Printing them out also requires proprietary format specifiers. I am not sitting at a Windows machine so I can't check it out exactly, but I remember a thread a long time ago where I enumerated and gave examples of them. I think the data types were __int64, and the format to print them was "%IA64", but I could be mis-remembering them. Look in compiler documentation and/or msdn for details. Quote:
With recent GNU compilers, use a command-line switch of '-std=c99', and they will cheerfully accept long long (even with -pedantic) and it will cheerfully print with "%ll" format, as specified in the C99 standard. No pesky warnings. Quote:
Additional information: Old Borland compilers that I used (before 2006) had _int64, I think, instead of the Microsoft __int64. I don't remember offhand what the format specifer was. Recent Borland compilers have "long long" and also have <stdint.h>, where it does typedefs such as uint64_t for 64 bit integers (it's the same as long long). GNU gcc/g++ also have had the C99 <stdint.h> data types for several revisions now (since 3.4.something at least). uint32_t specifies a 32-bit unsigned int; uint64_t specifies a 64-bit unsigned, etc. In my programs these days, where size is important, I use things like uint64_t for declarations. If the compiler doesn't have <stdint.h>, then I make my own typedefs to int64_t using whatever proprietary types are native to that compiler. That way, porting the program to other compilers and other systems involves at most a few typedefs at the beginning. The c99 header <inttypes.h> also has macros defined to print them out. They take a little getting used to, but could help portability also. If you are a regular user of gcc/g++, I suggest you read up on <stdint.h> and <inttypes.h> (Actually <inttypes.h> includes <stdint.h>) Regards, Dave Footnote: This is the C forum, but I mentioned C++ a couple of times. I should also mention that "long long", <stdint.h> and <inttypes.h> are part of the C standard (the C99 Standard, that is), not C++. C compilers that I use for desktop applications are also C++ compilers, and I note that GNU compilers and current versions of Borland bcc32 also compile C++ programs with "long long" and <stdint.h> just dandy, although the "-pedantic" flag with g++ gives warnings. It's not an issue with the modest embedded applications that I have participated in, since they are C. |
|
#5
|
|||
|
|||
Re: combining localtime() members into a single long longInteresting discussion.
In windows 98 I am using the Digital Mars 'dmc' C/C++ Compiler Version 8.49. I just got back from linux gcc 3.??? I forget... redhat 9 era. I don't get any warnings using either compiler. I see dmc has both stdint.h and inttypes.h and I see the: typedef unsigned long long uint64_t; I'll have to give that a try and remember for the future! btw , here's how I finished that experiment. Pretty fun! : CPP / C++ / C Code:
|
|
#6
|
||||
|
||||
Re: combining localtime() members into a single long longMore info:
In that cygwin test I tried, was actually with g++ 3.4.4 (not realizing the forum I was under, duh), with the -pedantic gave that message. Without -pedantic, there were no errors/warnings. Thanks for the info Dave! -PT __________________
Use the force...read the source!! WYCIWYG -- what you code is what you get! |
|
#7
|
|||
|
|||
Re: combining localtime() members into a single long longQuote:
Here's something else to explore. When evaluating polynomials, knowledgeable programmers use a method sometimes called Horner's nesting scheme. Rather than describing it in words, I'll give an example using your program. (With polynomials whose coefficients are in an array, the program is a really simple loop). CPP / C++ / C Code:
By printing out each step, you can see how the previous value is shifted left two decimal digits before adding the next one. Code:
What's the advantage (other than the "fun" part and the elegance of a consistent sequence of similar operations)? Well, suppose you decide you don't want seconds. Then just leave off the last operation; none of the previous program statements have to be changed. Just a thought. Regards, Dave |
|
#8
|
|||
|
|||
Re: combining localtime() members into a single long long...and they said there would be no math... Wow... after looking at a few googled pages on polynomials, I remember why I struggled so much with algebraI. That's as far as I got! I choked and graduated 12th in '73 with a general applied math course.... Nevertheless, I've been trying to get an idea of what you're trying to illustrate.
Are you saying to place those tm values into an array and loop through the array? Then again I'm thinking of 'nested' as being something like this: CPP / C++ / C Code:
But don't ask me to put it into a poynomial and reduce to lowest terms Thanks Dave! Every day something new. ++Howard; |
|
#9
|
|||
|
|||
Re: combining localtime() members into a single long longQuote:
Experience in one area can be helpful in solving problems in other areas, and if you think about it, the positional notation of our number systems is an example of a polynomial in powers of 10. Your example used powers of 100 to place the individual elements of the struct into a single number, and the math is exactly the same as evaluating a polynomial. Suppose the four coefficients of a third degree polynomial in x are in an array: a[0], a[1], a[2], a[3], where a[0] is the constant term (coefficient of x to the zero power) a[1] is the coefficient of x to the first power a[2] is the coefficient of x to the second power a[3] is the coefficient of x to the third power Let n = 4 (the number of coefficients) Naive programmers would probably do something like the following to evaluate the polynomial: CPP / C++ / C Code:
or, equivalently CPP / C++ / C Code:
Astute programmers, would more likely use the nesting scheme: CPP / C++ / C Code:
Or even more efficiently CPP / C++ / C Code:
That's known as 'using your head'. (It's also known as Horner's nesting scheme.) Why go through the expense of calling a mathematical function for each power of x as you go through the loop? You just start with the most significant and then each time through the loop you take the previous value and "shift left" by multiplying by the base and then add the new one. This is exactly what I showed for my take on your datecode calculation (except I didn't bother to put them into an array and make a loop). In numerical analysis, sometimes problems have to evaluate polynomials millions of times (or more). The waste of using the power function is intolerable. Furthermore, what if the polynomials are for integer powers? The library funciton pow() is a floating point function (you could write your own integer power function, of course). The idea of needlessly calling a floating point function, and incurring the expense of converting arguments and return values back and forth between ints and doubles, not to mention the possibility of introducing roundoff error, simply isn't acceptable. (Not to me, at least.) In your loop, you started with the most significant part in the zeroth element of the array, so the loop started at zero. Actually I like your way better for some cases, but mathematicians like the other way around. The important point is that the math is exactly the same for your datecode as the polynomial evaluation. Now, you know everything about polynomials! Regards, Dave |
|
#10
|
|||
|
|||
Re: combining localtime() members into a single long longHoward's original question -
mktime returns a time_t (implementation dependent) datatype. On HPUX 11.0it is a 32 bit unsigned long, for example. All of our Linux boxes have it defined as 64 bit. It seems to me that would work for sorting. Plus, localtime starts out using that same time_t value, so you could skip the intermediate steps. Or did you need to have it in human readable form? |
Recent GIDBlog
Halfway done! by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Strange C++ code memory leakage problem | gaoanyu | C++ Forum | 7 | 04-Nov-2005 08:09 |
| Quick, Insertion, and Partition | silicon | C++ Forum | 0 | 18-May-2005 20:49 |
| Merge and Heap...which is really faster | silicon | C++ Forum | 0 | 10-May-2005 13:46 |
| Insertion Sort Problem | silicon | C++ Forum | 2 | 08-May-2005 16:13 |
| Sorting Algorithms by Time | silicon | C++ Forum | 4 | 02-May-2005 21:54 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The