Solved

(1998*100)+1 = 3193 !!!

Posted on 1998-04-20
8
147 Views
Last Modified: 2010-04-06
I am working on an accounting system and I need to have a feild which combines the year and month.  I set up a function to get the value required:

function FetchMonth(Dt: TDateTime): LongInt;
var Y,M,D: Word;
begin
     DecodeDate(Dt,Y,M,D);
     Result := (Y*100)+M;
end;

The result, given the date 01/01/1998, is 3193 instead of 199801.  Why???  and how do I get the correct answer?

I tried using a LongInt variable to capture the answer before the result line, but I still got the same incorrect answer.

I am using D1 and Win 3.11.
0
Comment
Question by:jdthedj
8 Comments
 
LVL 5

Expert Comment

by:julio011597
ID: 1335288
Would you try this:

Result := Longint(Y) * 100 + M;

If this works (i've not Delphi1 to test), please let me know, and i'll submit an answer with full explanation.
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1335289
In D2 and it works ....

In D1 it doesn't .... Y*100+M gives wrong result?
   I tried double, but same result....

I know D1 & D2 calculate there date different, but this has nothing to do with it....

Julio, your code also doesn't work...

Regards, Zif.

0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1335290
Sorry JULIO's code DOES Work!!!
0
 
LVL 5

Expert Comment

by:inter
ID: 1335291
Hi,
Julio is right, give him the points please. Let me explain the reason just for colobrating. The reason is the size of the integer in Delphi 1( or in 16 bit windows) the integer is 16 bits wide which you have max number be 65535(unsinged).
However
1998*100+1 = 199801 which is larger than 65535 gives an overflow.
In this case the program performs the following(naturally discards the rest)
(1998*100+1) mod 65536 = 3193
So, by rewriting it as
Longint(1998) * 100 + 1 you tell compiler that the operations be carried out in longint(32 bit type) basis which can hold upto 2^32 unsigned.
Regards,
Igor
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 5

Accepted Solution

by:
julio011597 earned 50 total points
ID: 1335292
Thank you guys... you sometimes make me feel quite at home :)

There's nothing to add to inter's explanation but a detail as to how i see it going on and why the Integer size matters here:

when the compiler parses this expression "Y * 100 + M", it encounters in turn:

1. Y which is Word
2. 100 which is Integer, and since there's a "*" between Y and 100, it will promote Y to Integer as well and take the result as to be Integer
3. M which is Word, and since there's a "+" between M and the previous result, it will promote M to Integer and take the result as to be Integer.

Longint(Y) instead casts Y to Longint, and promotes all subsequent operands and results to Longint as well.

Ok, i hope i didn't get in troubles now :)
0
 

Expert Comment

by:jianl
ID: 1335293
You can also use Delphi 3 that you can find it
work right,but under Delphi 1 ,it seems you must
use longint(y) instead of y
0
 
LVL 5

Expert Comment

by:julio011597
ID: 1335294
jianl, there's nothing going wrong under a technical point of view... did you understand the answer?

BTW, jdthedj, there's another way in order to keep using integers:

var Enc: Integer;
var Y, M: Word;

--/ [encoding] /--
Enc := (Integer(Y) shl 4) and M;
--//--

--/ [decoding] /--
M := Enc and $000F;
Y := (Enc shr 4) and $0FFF;
--//--

For this code to work, i'm assuming: M < 16 and Y < 4096, otherwise they just wouldn't fit in 16 bits.
You'll have an application with Y4K problems :)

BTW, anyone knows if, in that assignment to Y, the "and $0FFF" is really needed?
I.e. has Delphi/OOP the same concerns as C when right-shifting a signed type?
0
 
LVL 3

Author Comment

by:jdthedj
ID: 1335295
Thanks for all the help guys!!  I was beginning to think it was a Delphi bug.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Simple Delphi Question 9 83
Comparing dates in Delphi. Greater than/ Less than 3 119
Delphi Firemonkey Need Sample for Online Shopping Example. 2 120
How to debug For loops? 3 46
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now