real to integer

classic Classic list List threaded Threaded
13 messages Options
Reply | Threaded
Open this post in threaded view
|

real to integer

Thomas Miller-5
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?
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: real to integer

Vincent Snijders
Thomas Miller schreef:

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

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: real to integer

Jonas Maebe-2
In reply to this post by Thomas Miller-5

On 29 mei 2006, at 14:46, Thomas Miller wrote:

> 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
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re[2]: real to integer

Пётр Косаревский
> 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.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: real to integer

Vincent Snijders
Пётр Косаревский schreef:

>> 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.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re[2]: real to integer

Пётр Косаревский
> > 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).
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: real to integer

Tony Pelton
In reply to this post by Vincent Snijders
On 5/29/06, Vincent Snijders <[hidden email]> 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/
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: real to integer

Vincent Snijders
Tony Pelton schreef:
> On 5/29/06, Vincent Snijders <[hidden email]> 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.

>
> 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
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: real to integer

Jonas Maebe-2

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
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: real to integer

Vincent Snijders
Jonas Maebe schreef:

>
> 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
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re[2]: real to integer

Пётр Косаревский
In reply to this post by Vincent Snijders
> > 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.)
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re[2]: real to integer

Пётр Косаревский
In reply to this post by Jonas Maebe-2
> 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?
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Re[2]: real to integer

Jonas Maebe-2

On 30 mei 2006, at 12:10, Пётр Косаревский wrote:

>> 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_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal