?
Solved

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

Posted on 1998-04-20
8
Medium Priority
?
154 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

800 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