delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
500 Users Online NOW
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback





Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi



Loremo - the 1.5 liter car coming in 2009




Startblatt.de






Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (7)


Converting a floating point value to an integer valueFormat this article printer-friendly!Bookmark function is only available for registered users!
Real to Integer Casting
Product:
Delphi all versions
Category:
Algorithm
Skill Level:
Scoring:
Last Update:
06/29/2003
Search Keys:
delphi delphi3000 article borland vcl code-snippet casting data types real to integer conversion data type conversion
Times Scored:
6
Visits:
3050
Uploader: Jim Carter
Company: Xtronix Ltd
Reference: N/A
 
Question/Problem/Abstract:
Delphi does not allow "casting" between integer and real data types. This shows how that can be achieved.
Answer:



Delphi has a rich selection of data types as shown below:

Pascal Data Types
=================

Integer Types
=============
Type     Range                            Format
====     =====                               ======
Integer -2147483648..2147483647             signed 32-bit
Cardinal  0..4294967295                   unsigned 32-bit
Shortint -128..127                            signed 8-bit
Smallint -32768..32767                   signed 16-bit
Longint -2147483648..2147483647             signed 32-bit
Int64 -2^63..2^63                   signed 64-bit
Byte 0..255                            unsigned 8-bit
Word 0..65535                            unsigned 16-bit
Longword  0..4294967295                   unsigned 32-bit

Real Types
==========
Type     Range
====     =====
Real48   2.9 x 10^-39 .. 1.7 x 10^38          48 bits
Single   1.5 x 10^-45 .. 3.4 x 10^38          32 bits
Double   5.0 x 10^-324 .. 1.7 x 10^308          64 bits
Extended  3.6 x 10^-4951 .. 1.1 x 10^4932              80 bits
Comp -2^63+1 .. 2^63 -1 19–20          64 bits
Currency -922337203685477.5808.. 922337203685477.5807  64 bits

--------------------------------------------------------------------------

In Delphi it is possible to change the "type" of a variable by using the "cast" method.

For instance, you may have a character "tmpc" and an integer "tmpi", you cannot copy the contents of tmpi into tmpc as the compiler will complain. So, if you write;

--------------------------------------------------------------------------
procedure TForm1.Button1Click(Sender: TObject);
var
tmpi: integer;
tmpc: char;

begin
tmpi:= 1;
tmpc:= tmpi;
end;
--------------------------------------------------------------------------

When this is compiled, you will will get the following compiler error:

    [Error] Unit1.pas(66): Incompatible types: 'Char' and 'Integer'

However, if you use a "cast" to change the data type of "tmpi" as follows;

--------------------------------------------------------------------------
procedure TForm1.Button1Click(Sender: TObject);
var
tmpi: integer;
tmpc: char;

begin
tmpi:= 1;
tmpc:= char(tmpi);
end;
--------------------------------------------------------------------------

This will compile correctly.

However, if you try to "cast" between integer and real data types you will find that the compiler will not let you. So if you have an integer data type "tmpi" and a real data type "tmpf and you compile the following code;

--------------------------------------------------------------------------
procedure TForm1.Button1Click(Sender: TObject);
var
tmpi: integer;
tmpf: single;

begin
tmpf:= 1;
tmpi:= tmpf;
end;
-------------------------------------------------------------------------

you will get the following error;

    [Error] Unit1.pas(66): Incompatible types: 'Integer' and 'Single'

This is because the data types are real and integer. So, you could try to "cast" between the two as follows;

-------------------------------------------------------------------------
procedure TForm1.Button1Click(Sender: TObject);
var
tmpi: integer;
tmpf: single;

begin
tmpf:= 1;
tmpi:= integer(tmpf);
end;
-------------------------------------------------------------------------

Now you get the following compiler error;

    [Error] Unit1.pas(66): Invalid typecast

Note that although they are both 32 bit data types but Delphi does not allow the type cast from a floating point numbers (real) to an integer numbers.


So, the way to do this is to use a pointer to "point" to the real number memory location and then to "read" the value of that loaction into an integer variable.


The following function performs this task:

{-----------------------------------------------------------------------------
  Function:  TMainForm.FloatToLongword
  Author:    Jim Carter - Xtronix Ltd - www.xtronix.co.uk
  Date:      24-Mar-2003

  Arguments: Value: single (floating point number 32 bits)
  
  Result:    Longword - The float data is copied into a Longword (32 bits)
  
  Notes:     This is required as Delphi does not allow a cast between floating
             and integer numbers
-----------------------------------------------------------------------------}
function FloatToLongword(val: single): longword;

var
p: ^longword;          //assign pointer that is referenced to longwords

begin
  p:=@(val);           //point to user floating point data address (32 bits)
  result:=p^;          //copy the float data to the longword result(32 bits)

end;


============================================================================

Jim Carter - Xtronix Ltd









Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment
Why use this?
    Richard Winston (Jul 3 2003 5:51PM)

Why not just use tmpi:= Round(tmpf);
Respond

RE: Why use this?
Jim Carter (Jul 9 2003 9:22AM)

Why Use This?

Well, if you are only interested in the mathematical value of the floating point variable then you could, as you point out, use the function Round() to return the real part of the floating point variable as an integer.

However, if you wanted to send the complete floating point number over a serial interface you would need to break it down into bytes for sending. To do this you would need a method of converting the actual binary values (not the mathematical values) contained in the four bytes making up the single data type into an integer type which in turn could be operated on to get the byte data type suitable for sending over a serial interface.

Hope that answers your question.

Jim




Respond

RE: RE: Why use this?
Richard Winston (Jul 9 2003 4:44PM)

In that case, you should probably declare the integer as longint so that if and when we start using 64 bit operating systems, the code will still work.
Respond

RE: RE: RE: Why use this?
Richard Winston (Jul 9 2003 5:00PM)

Oops. You've already used longword which would work too.
Respond

RE: RE: RE: RE: Why use this?
Jim Carter (Jul 9 2003 8:43PM)

Hi;

Yes you could use a 64 bit integer. I used a 32 bit integer as it matched the 32 bit float.

Regards

Jim

Respond

RE: RE: RE: RE: RE: Why use this?
shaun stock (Dec 1 2005 3:20PM)

Thanks Jim, this is just what I needed!
I can now cast (using another function "longwordtobin") a real number directly into a binary string without having to "bit fiddle" and juggle with mantissa's, exponents and signs!
Thankyou very much!

Respond

RE: RE: RE: RE: RE: RE: Why use this?
Jim Carter (Dec 1 2005 6:23PM)

Hi Shaun;

Sounds good - best of luck

Jim

Respond














 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
M. Maes
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)