Solved

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

Posted on 1998-04-20
8
149 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
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
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
 
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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

828 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