|
I thought it was, "the first caveman who threw a spear".
|
|
|
|
|
My mathematical skills are weak but I need to calculate a week number based on a date. The week number will be either 1, 2, 3 or 4.
I have a base date for a week 1 within the calendar year, which is always a monday, for example 15th Jan 2007.
Therefore, if I enter a date of 22nd Jan 2007 I need a return value of 2 representing week 2, likewise for 23rd through to 28th Jan 2007. 29th Jan 2007 through to 4th feb 2007 would return week 3 and so on.
Just to clarify my calendar is divided in 4 weekly cycles, and I need the week within the 4 weekly cycle that a date falls in.
I would be grateful for any assistance on creating a formula to provide this information.
Steve Jowett
-------------------------
Sometimes a man who deserves to be looked down upon because he is a fool, is only despised only because he is an 'I.T. Consultant'
|
|
|
|
|
Exactly how you do this depends on the language / support for calculating differences between dates. The formula you want is:
week number = ( ( (day(date) - day(base)) / 7 ) % 4 ) + 1
where day(date) - day(base) is the number of days difference between 'date' and 'base', and is assumed to be an integer.
e.g.: day(tomorrow) - day(today) = 1
% is the remainder operator in C/C++, so 4%4 = 0, 5%4 = 1, 6%4 = 2, 7%4 = 3 and so on
Peter
"Until the invention of the computer, the machine gun was the device that enabled humans to make the most mistakes in the smallest amount of time."
|
|
|
|
|
Thanks Peter, you're the best.
Steve Jowett
-------------------------
Sometimes a man who deserves to be looked down upon because he is a fool, is only despised only because he is an 'I.T. Consultant'
|
|
|
|
|
I have a 64 bit integer (C# long). Apparently the file format I'm writing (Internet Explorer cookies) wants the "high part" and the "low part." I'm pretty sure that the low part is just thingy >> 32. What about the high part...?
My integer is signed.
Thanks!
-Domenic Denicola- [CPUA 0x1337]
“I was born human. But this was an accident of fate—a condition merely of time and place. I believe it's something we have the power to change…”
|
|
|
|
|
Hi,
that would be wrong.
the low part is the result of anding with a mask containing the bits you want,
for 64-bit that would be 0x00000000FFFFFFFFL (L suffix for ulong).
[Addition: the alternative is to cast to (uint)]
the high part is what you get if you right-shift an unsigned long
over 32 positions;
or simply cast to uint
The fact the Intel and the like store numbers the other way around has nothing
to do with it, since you are performing mathematical operations here, not
memory access operations.
I trust you could have figured this out yourself with a couple of small experiments ?
-- modified at 3:40 Friday 3rd August, 2007
|
|
|
|
|
Interestingly, my experiments don't exactly follow your predictions...
long ticks = (new DateTime(2003, 3, 18, 21, 22, 10, DateTimeKind.Local) - new DateTime(1601, 1, 1, 0, 0, 0, DateTimeKind.Utc)).Ticks;
ticks >> 32: 29552020
(ulong)ticks >> 32: 29552020
ticks & 0x00000000FFFFFFFFL: 1869262080
(uint)((ulong)ticks): 1869262080
(uint)ticks: 1869262080
In particular casting to uint seems to give the same result as the bitmask, and not as the shifting, and signed versus unsigned doesn't seem to matter much (probably because all my numbers are positive and not too large?)
Also, strangely enough, the original source for which I'm working on this (see here) seems to want to want to multiply the high part by 2? Any reason why it would do this?
(Backstory: I have a Cookie object in .NET, and I want to write it to the Internet Explorer cookies file. Which has this wierd date format based on the high- and low- parts of a 64-bit ticks integer. But, as you can see above, apparently they multiply the high part by 2 before storing it.)
-Domenic Denicola- [CPUA 0x1337]
“I was born human. But this was an accident of fate—a condition merely of time and place. I believe it's something we have the power to change…”
|
|
|
|
|
Sorry, I added the "or simply cast to uint" at the wrong position.
Obviously it holds the lower half, it couldnt possibly hold the upper half.
signed >> replicates sign bit, so resulting value does not equal high part.
Anyway, you now grasped the value of a little experiment.
In future, when interested in bit-related stuff, I suggest:
- you start with a known value, a very good one is 0x0123456789ABCDEFL and the like;
- you print everything in hex format e.g.with ToString("X16") or something similar.
Then it is quite apparant what is going on. Decimal printout completely hides
the bits, hex shows them much better.
|
|
|
|
|
Luc Pattyn wrote: the high part is what you get if:
- you right-shift an unsigned long over 32 positions;
- or simply cast to uint
The above statements are incompatible. IMHO performing a uint cast provides the low part.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
|
|
|
|
|
Of course, that was a lapsus; it got spotted and rectified by the previous two
messages. I now have modified the original post.
Greetings.
|
|
|
|
|
Luc Pattyn wrote: it got spotted and rectified by the previous two
messages
I quikly read that messages and missed the point
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
|
|
|
|
|
:-D I want a method for getting "patches of information for extraction images" & how can I get it.My project is 3d image recognition & it is in matlab.Thank you.
zz
|
|
|
|
|
Salam zainab did u searched "3D image processing" book or article if u did't then search it and i think it will be founded in the book named "3D game programming in C++" or "game theory with C++" and "advance 3D graphics and 3D image processing". there is a site "www.scopus.org or .net or .com" which contains millions of research generals u will find ur solution from that site. And also u can develope this method in assembly i think there is a book "The art of assembly" it will help u in this journey.
habib bhutto
|
|
|
|
|
Guys,
I have a telephone number, i wanna detect if that number inculdes two, three or four consequtive numbers, or generaly, detecting any kind of pattern !
Example: 23458745, 236321236 , 35570123, 72462138
I have used an array of that number(after splitting) and started to do some work on sorting and searching but it's getting very complex
Is there any way for solving this?
Thank you
ThaScorpion
|
|
|
|
|
ThaScorpion wrote: generaly, detecting any kind of pattern !
A tall order!
For the sorts of patterns you have illustrated, you could just study the differences between digits:
- 0 means repeated digits (e.g. 2,2,2 gives differences 0,0)
- 1 means consecutive digits (e.g. 2,3,4 gives differences 1,1, and 4,3,2 gives differences -1,-1)
- repeated differences higher that 1 means a pattern (e.g. 2,4,6 gives differences 2,2)
Peter
"Until the invention of the computer, the machine gun was the device that enabled humans to make the most mistakes in the smallest amount of time."
|
|
|
|
|
Thank you for your fast reply.
cp9876 wrote: A tall order!
Is this a name for an algorithm??
Thanks again
cheers
ThaScorpion
|
|
|
|
|
ThaScorpion wrote: cp9876 wrote:
A tall order!
Is this a name for an algorithm?
No, "tall order" basically means that you are asking for alot.
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
|
|
|
|
|
that question made me smile.
|
|
|
|
|
Maybe someone should do a Wiki entry for "tall order" algorithm. A greedy algorithm that asks for alot to be done in little or no time. Generally has a time/space order complexity in the same neighborhood of any typical NP-Complete problem.
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
|
|
|
|
|
Yeah, that sounds about right!
|
|
|
|
|
Isn't that what most of the posters in the C# / ASP.NET and web dev forums are asking for :- A lot, urgently, without them doing anything themselves?
"More functions should disregard input values and just return 12. It would make life easier." - comment posted on WTF
|
|
|
|
|
There does seem to be an unfortunate rash of laziness around some of these forums lately
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
|
|
|
|
|
You could consider using regular expressions.
Steve
|
|
|
|
|
Yep. Not sure what kind of regular expression he could use, but maybe Expresso[^] could be useful
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
|
|
|
|
|
Hi,
this could be very easy, depends on your definition of pattern.
if your example 236321236 contains two patterns for you, that is 23 (twice),
then the following will do. If the fact that 236 appears twice is a pattern too,
then it is harder.
Here is my algorithm in pseudo-code (i.e. approx C#); some details may
be incorrect, but you will get the general idea; it is quadratic which
is good enough for phone numbers I guess.
string s="my text to analyze";
int sLength=s.Length;
for (int i=0; i<sLength-1; i++) {
char c1=s[i];
char c2=s[i+1];
int diff=c2-c1;
if (diff==0) log("double char");
if (diff*diff==1) log("two char series");
for (int len=3; len<sLength-i; len++) {
c2+=diff;
char c=s[i+len-1];
if (c2==c) log("series extends to "+len+" chars");
else break;
}
}
As you can see, nothing fancy, no tricks, no regex...
|
|
|
|