Pascal

Moderators: None (Apply to moderate this forum)
Number of threads: 4098
Number of posts: 14002

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
How to create function that will raise x to y (x^y=z) Posted by AW10 on 15 May 2011 at 2:43 PM
This code is not working:
program hw13;
uses crt;
var number,exponent:integer;
function PowerFn (number, exponent: integer): integer;
begin
PowerFn := Exp(exponent*Ln(number));
end;
begin
     clrscr;
     write('Enter number: ');
     readln(number);
     write('Enter exponent: ');
     readln(exponent);
     writeln(number,'^',exponent,' = ',PowerFn(number,exponent));
     readln;
end.


It gives me this error: Incompatible types: got "S80REAL" expected "LONGINT"

Report
Re: How to create function that will raise x to y (x^y=z) Posted by quikcarl on 16 May 2011 at 2:12 PM
That's because Exp is a real function and not integer. So change
function PowerFn (number, exponent: integer): real;
or change
PowerFn := Trunc( Exp(exponent*Ln(number)) )

Report
Re: How to create function that will raise x to y (x^y=z) Posted by blackr1234 on 11 Jun 2011 at 2:18 AM
Actually, ou can use ** to represent the operand of ^ instead of using functions like those in your example.

PROGRAM x;
USES crt;
BEGIN
writeln(13**5); {show 13^5}
readkey
END.
Report
Re: How to create function that will raise x to y (x^y=z) Posted by Wind_NarSas on 25 Jun 2011 at 9:21 PM
The program does not run on
Report
Re: How to create function that will raise x to y (x^y=z) Posted by blackr1234 on 12 Jul 2011 at 7:47 AM
Sorry that I forget to add the math unit.

Change the second line to
<code>UWSES crt, math;</code>,
so you could use x**y to show x to the power of y
Report
Re: How to create function that will raise x to y (x^y=z) Posted by daogiahieu on 22 Jul 2011 at 1:44 AM
May be, you forgot a unit.
That unit is maths unit (if i don't forgot !).
Report
Re: How to create function that will raise x to y (x^y=z) Posted by daogiahieu on 22 Jul 2011 at 1:57 AM
May be, you forgot a unit.
That unit is maths unit (if i don't forgot !).



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.