Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CONVERT A REAL VALUE TO AN INTEGER

Status
Not open for further replies.

PROFESSORSPARKIE

Instructor
Oct 24, 2002
181
0
0
US
HOW DO I CONVERT A REAL VALUE TO AN INTEGER?

VAR
LOC : LONGINT;
WORK1 : REAL

LOC := INT(WORK1);

GIVES AN ERROR 75 DATA MISMATCH

I KNOW IT MUST BE SIMPLE BUT I O NOT HAVE A GOOD MANUAL & I HAVE TRIED MANY THINGS. ANY HELP WILL BE APPRECIATED.

THANKS IN ADVANCE, I DO SIMPLE THINGS LIKE THIS IN MANY OTHER LANGUAGES BUT NOT IN PASCAL.
 

You can either use TRUNC() which will strip off anything after the decimal point (so it always rounds down) or you can use ROUND() which rounds to the nearest integer.
 
Sorry, I should have made my self clear. I am going to use the value to seek in a direct binary file which requires a longint or integer in the SEEK procedure. It will not accept a real value.

I am developing the location by doing a square of the key, dividing by the number of allocated slots and going directly to the record. I also use a spill forward with space wrap around for synonyms.

I teach and am using a qbasic program to load the file, pascal to update it and cobol to list it with a perl program supporting a cgi inquiry. The class is a program language surver course. This is the only part I am having trouble with.
 
Perhaps you should clarify your requirements. Stackdump provided the proper answer, by all indications.
 
I am going to use SEEK and BLOCKREAD PROCEDURES TO BE ABLE TO RANDOM READ FROM A FIXED LENGTH RECORD FILE. THE SEEK REQUIRES INTEGER VALUE FOR THE RECORD LOCATION based on a randomized location

LOC : LONGINT;
BUFFER : ARRAY [1..43] OF CHAR;{MAX_BUF] OF CHAR;}
BYTES_READ : WORD;
REALWORK1 : REAL;
SLOTS : REAL;


LOC := (INT(REALWORK1 / SLOTS) * SLOTS ) + 1.0;
SEEK(EMP_FILE, LOC);
BYTES_READ := 0;
BLOCKREAD(EMP_FILE, buffer, 1, BYTES_READ);

LOC must be integer type but my calculations are in real numeric values. This type of processing is often used in high performance requirements where record retrieval need to approach 1 SEEK 1 READ per record. Using 1 or more index reads can raise the cost of read performance greatly.

Other languages handle this more easily.
 
The question is easy. How do I take a REAL variable value and put the integer part in an INTEGER variable.
 
VAR
LOC : LONGINT;
WORK1 : REAL

LOC := trunc(WORK1);
 
glenn, THAT WORKED!!!!
Thanks

I wonder why
LOC := INT(WORK1);

Since the value on INT is an integer. So goes the nature of programming languages. I should have tried the answer that stackdump gave me. I was so sure that INT & TRUNC worked the same.

I am sorry stackdump. THANKS to ALL.
 
Int() returns an extended/real value.

loc := Int(3.14157);

loc has to be extended/real, and the answer will be 3.000000000.
 
Really, before truncating it, you should be asking yourself why this variable was ever real in the first place. You can never have a location of 3.14 in a file.
 
To glenn9999,

None of the references I had available said that INT returned a long integer. In other languages I have used an INT function took a real/float and returned an integer value and I let myself go down that path.

lionelhill,

A routine I had just converted from another language that did not have a remainder function took the key and divided by the number of slots to get a relative file location in a file space. I have done this type of high performance processing for over 40 years. You get a real value and just round off to an integer.

As I said early on I have manuals that are short on internals details. Any recommendations for a great text? There arn't many options now like with perl, java, etc.

Again THANKS to all. It had been many years since I wrote programs in Pascal except for "Hello World" or simple report listings. It has been interesting. Direct(binary) file processing requires a detailed understanding of disk file management.

ProfessorSparkie
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top