Solved

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

Posted on 1998-04-20
8
146 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

760 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

22 Experts available now in Live!

Get 1:1 Help Now