Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2006-05

real to integer

13 messages · 5 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Thomas Miller

I am frustrated by a very simple problem. I am trying to convert real to integer, using free pascal for mac. I am an old Think Pascal user who just recently found free pascal and, while thrilled to have a pascal compiler for mac OSX, I am still a bit lost. In Think Pascal, I would have just used "round", but this doesn't seem to work.

x : integer;
y : real;
x := round(y);

gives me an error saying "got "Double" expected "SmallInt"". This occurs no matter how I define x: integer, smallint, etc. In fact, if I set x to real, then the program compiles and works, simply converting decimal values to 0 but not deleting them.

I suspect I am making some simple error. Any advice?

Reply by email

Vincent Snijders
to Thomas Miller
Thomas Miller wrote 17 lines
I am frustrated by a very simple problem. I am trying to convert real to integer, using free pascal for mac. I am an old Think Pascal user who just recently found free pascal and, while thrilled to have a pascal compiler for mac OSX, I am still a bit lost. In Think Pascal, I would have just used "round", but this doesn't seem to work. x : integer; y : real; x := round(y); gives me an error saying "got "Double" expected "SmallInt"". This occurs no matter how I define x: integer, smallint, etc. In fact, if I set x to real, then the program compiles and works, simply converting decimal values to 0 but not deleting them. I suspect I am making some simple error. Any advice?

I don't know what is going wrong.
I just created a sample app below on windows and that compiles without errors. Maybe you can try that.

program Project1;
{$mode objfpc}{$H+}

uses
Classes, SysUtils

   { add your units here };
var
x : integer;
y : real;
begin
   y := pi;
   x := round(y);
end.

Reply by email

Jonas Maebe
to Thomas Miller: I am frustrated by a very simple problem. I am trying to convert real to integer, using…
Thomas Miller wrote 18 lines
I am frustrated by a very simple problem. I am trying to convert real to integer, using free pascal for mac. I am an old Think Pascal user who just recently found free pascal and, while thrilled to have a pascal compiler for mac OSX, I am still a bit lost. In Think Pascal, I would have just used "round", but this doesn't seem to work. x : integer; y : real; x := round(y); gives me an error saying "got "Double" expected "SmallInt"". This occurs no matter how I define x: integer, smallint, etc. In fact, if I set x to real, then the program compiles and works, simply converting decimal values to 0 but not deleting them. I suspect I am making some simple error. Any advice?

The problem is that Apple's universal interfaces also include a "round" function, which returns a real instead of an integral type. There are two solutions:

a) use "system.round(y)" so the compiler uses the round function from the system unit instead of from your carbon unit
b) switch to a new version of the Universal Interfaces translations (where that "round" function has been renamed "roundd"), available from http://www.elis.ugent.be/~jmaebe/nobackup/fpc/
FPCPInterfaces060428.zip

Note that in case b), instead of the Carbon unit, you have to use the "fpcmacosall" unit instead. It also fixes several bugs in the UI translations, so it's a good idea to upgrade them in either case.

Jonas

Reply by email

Пётр Косаревский
to Jonas Maebe
The problem is that Apple's universal interfaces also include a "round" function, which returns a real instead of an integral type.

How can one find such a thing himself without trial&error?

Some time ago I had a similar problem with "bool DeleteFile(PChar)" function in Windows API and sysutils function "DeleteFile(string):boolean".

"Good" program:
uses windows,sysutils;
const B:shortstring='1.txt';
begin DeleteFile(B); end.
"Bad" program: // Error: PChar expected
uses sysutils,windows;
const B:shortstring='1.txt';
begin DeleteFile(B); end.

While compiler gives a hint about "true" declaration, it does not point to the problematic unit.

Reply by email

Vincent Snijders
to Пётр Косаревский
Пётр Косаревский wrote 18 lines
> The problem is that Apple's universal interfaces also include a > "round" function, which returns a real instead of an integral type. How can one find such a thing himself without trial&error? Some time ago I had a similar problem with "bool DeleteFile(PChar)" function in Windows API and sysutils function "DeleteFile(string):boolean". "Good" program: uses windows,sysutils; const B:shortstring='1.txt'; begin DeleteFile(B); end. "Bad" program: // Error: PChar expected uses sysutils,windows; const B:shortstring='1.txt'; begin DeleteFile(B); end. While compiler gives a hint about "true" declaration, it does not point to the problematic unit.

Use a IDE with code tools that support find declaration.

In your case Lazarus would for example have jumped to an include file that that contains the wrong declaration. Search include directive once or twice would have brought you to the main unit file.

Vincent.

Reply by email

Пётр Косаревский
to Vincent Snijders
Show quoted text 6 lines
> While compiler gives a hint about "true" declaration, it does not point to the problematic unit. Use a IDE with code tools that support find declaration. In your case Lazarus would for example have jumped to an include file that that contains the wrong declaration. Search include directive once or twice would have brought you to the main unit file. Vincent.

Probably you are right. I'll wait for Lazarus 1.0 though (I tried two previous releases and still fear it).

Reply by email

Tony Pelton
to Vincent Snijders: Use a IDE with code tools that support find declaration. In your case Lazarus would for…
Vincent Snijders wrote
Use a IDE with code tools that support find declaration.

that is a terrible answer.

this isn't the "IDE" list, it is the Free Pascal "compiler" list.

i would have beat my head against a wall for days on a problem like that.

i would suggest that this is an opportunity to think about error reporting in the compiler.

i would suggest, as a compiler enhancement, maybe ...

a) the compiler could report ambigous symbols as a warning b) the error could report fully qualifed symbols on errors

i don't know how simple that is for the compiler, but it is simple to understand for the programmer.

Vincent.

Tony

X-SA user ? 0.5.1 is out ! XData 0.1 for X-SA is out ! http://x-plane.dsrts.com http://games.groups.yahoo.com/group/x-plane-foo/

Reply by email

Vincent Snijders
to Tony Pelton
Tony Pelton wrote 7 lines
On 5/29/06, Vincent Snijders wrote: > > Use a IDE with code tools that support find declaration. that is a terrible answer. this isn't the "IDE" list, it is the Free Pascal "compiler" list.

Indeed. But sometimes the compiler doesn't give enough information and other tools do.

Another view: An IDE is to help people write code, a compiler is to compile it and not to guess what it should have been, if some error occurs.

i would have beat my head against a wall for days on a problem like that.

Using the wrong tool for the wrong job, I would say. A compiler is to compiler, an IDE to write code.

Show quoted text 7 lines
i would suggest that this is an opportunity to think about error reporting in the compiler. i would suggest, as a compiler enhancement, maybe ... a) the compiler could report ambigous symbols as a warning

AFAIK in this case there were no ambiguous symbols. Only a procedure hidden by an other. Some languages don't have disambiguation rules (C# for example) for units/namespaces and always require you to add the unitname (e.g. SysUtils.DeleteFile). I don't like that solution.

b) the error could report fully qualifed symbols on errors

Yes, more verbose error messages are always nice (until they get too verbose).

i don't know how simple that is for the compiler, but it is simple to understand for the programmer.

Vincent

Reply by email

Jonas Maebe
to Vincent Snijders
Vincent Snijders wrote 5 lines
> i would have beat my head against a wall for days on a problem > like that. Using the wrong tool for the wrong job, I would say. A compiler is to compiler, an IDE to write code.

The compiler already gives unit information in case you have conflicting types for making it easier to debug such problems. I don't see why it shouldn't do the same for used function definitions.

Jonas

Reply by email

Vincent Snijders
to Jonas Maebe
Jonas Maebe wrote 12 lines
On 30 mei 2006, at 08:37, Vincent Snijders wrote: >> i would have beat my head against a wall for days on a problem like >> that. > > Using the wrong tool for the wrong job, I would say. A compiler is to > compiler, an IDE to write code. The compiler already gives unit information in case you have conflicting types for making it easier to debug such problems. I don't see why it shouldn't do the same for used function definitions.

Jonas, I agree.

Tony, I think I overreacted a bit, when I read "that is a terrible answer". Sorry.

Vincent

Reply by email

Пётр Косаревский
to Vincent Snijders: Indeed. But sometimes the compiler doesn't give enough information and other tools do.…
> i would have beat my head against a wall for days on a problem like that.

It was absolutely unexpected by me. You saw: unit order matter very much and both units may be without sources (people are not supposed to install sources every time).

Signalling error "wrong type", when I have correct function, is like nonsense.

Using the wrong tool for the wrong job, I would say. A compiler is to compiler, an IDE to write code.

If so, compiler would submit messages in human unreadable format to IDE or make.

> i would suggest that this is an opportunity to think about error > reporting in the compiler.

Well, that was what I wanted to say.

> i would suggest, as a compiler enhancement, maybe ... > a) the compiler could report ambigous symbols as a warning > b) the error could report fully qualifed symbols on errors

To now it reports "...found declaration...", but does not state where.

Yes, more verbose error messages are always nice (until they get too verbose).

I talk about "-va" mode: I get tons of messages about paths/files compiler looks at for units, but no good hint.

At first I was to submit bug report like "error in documentation: sysutils.deletefile has other declaration". (By the way, sysutils itself uses windows unit with FPC for win32.)

Reply by email

Пётр Косаревский
to Jonas Maebe: The compiler already gives unit information in case you have conflicting types for making…
The compiler already gives unit information in case you have conflicting types for making it easier to debug such problems. I don't see why it shouldn't do the same for used function definitions. Jonas
program A; // "Bad" program
uses sysutils,windows;
const B:shortstring='1.txt';
begin DeleteFile(B); end.

Last compiler messages ("-va"): (all dependencies, path searching etc are before) Searching file 1.pp... found
(A) Load from A (implementation) unit WINDOWS 1.pp(3,1) (A) Parsing implementation of 1.pp 1.pp(4,19) Error: Incompatible type for arg no. 1: Got "ShortString", expected "PChar" Hint: Found declaration: DeleteFile(PChar):LongBool

program C;
uses windows,sysutils;
const B:shortstring='1.txt';
begin DeleteFile(B); end.

Last compiler messages:
Searching file 2.pp... found
2.pp(3,1) (C) Parsing implementation of 2.pp 2.pp(2,13) Hint: Unit "windows" not used in C

I got nice hints: "Load from A (implementation) unit WINDOWS" and "Hint: Unit "windows" not used in C".

While the second hint helps me to understand, that if I wanted to run deletefile() from windows, I failed, I don't understand how to read first bunch of messages (there are lotsa "Load from A ..." messages).

Of course, if I use any function from windows unit, I don't get "Hint: ... not used ...".

This is fpc-pascal maillist, I don't insist, that I can read good. Where from had I to get my hint?

Reply by email

Jonas Maebe
to Пётр Косаревский
Пётр Косаревский wrote 6 lines
> The compiler already gives unit information in case you have > conflicting types for making it easier to debug such problems. I > don't see why it shouldn't do the same for used function definitions. This is fpc-pascal maillist, I don't insist, that I can read good. Where from had I to get my hint?

I didn't say the compiler already gives this hint, I said I didn't see a reason not to add it.

Jonas

Reply by email