Recent

Author Topic: [Solved]How do I write an exponent function (x^y)?  (Read 30507 times)

Yukiko

  • Jr. Member
  • **
  • Posts: 55
[Solved]How do I write an exponent function (x^y)?
« on: September 01, 2017, 07:52:28 am »
I want to be able to pass 2 integer values to a function, x and y, and have the function return the value of x^y.

I guess I'm pretty naive when it comes to Pascal because I assumed that if the '^' operator wasn't implemented that there would be some function available in the RTL but sadly I couldn't find one. I realize I can do it with a simple loop multiplying the value 'x' 'y' times but that seems rather ridiculous and frankly would look simplistic if anyone ever saw my program. :)

I read about overloading ** to use for my purpose but I couldn't make heads or tails of how to write the line of code to accomplish it. So I need help. :)

Can someone show me the way to do "x to the power of y" either by some function available in the RTL or by overloading the operator '**'? I need to understand overloading anyway so an explanation of that would be beneficial even if the RTL has an exponent function.
« Last Edit: September 07, 2017, 10:47:37 pm by Yukiko »

balazsszekely

  • Guest
Re: How do I write an exponent function (x^y)?
« Reply #1 on: September 01, 2017, 08:00:08 am »
Hi Yukiko,

Please read this: https://www.freepascal.org/docs-html/rtl/math/explogroutines.html
Code: Pascal  [Select][+][-]
  1. uses math;
  2. var
  3.   r: Float;
  4. begin
  5.   r := Power(3, 4); //should return 81
  6. end;

JdeHaan

  • Full Member
  • ***
  • Posts: 173
Re: How do I write an exponent function (x^y)?
« Reply #2 on: September 01, 2017, 08:32:08 am »
or use this:

program power;

uses
  math;

begin
  writeln(9**0.5);  // 3.000000000...
  writeln(3**4);    // 81
end.

Thaddy

  • Hero Member
  • *****
  • Posts: 19607
  • Glad to be alive.
Re: How do I write an exponent function (x^y)?
« Reply #3 on: September 01, 2017, 11:07:22 am »
Both answers are correct, but i'd like to stress you need to include the math unit in your uses clause. Like both answers did...
Any "programmer" that knows only one programming language is not a programmer

Yukiko

  • Jr. Member
  • **
  • Posts: 55
Re: How do I write an exponent function (x^y)?
« Reply #4 on: September 01, 2017, 07:43:05 pm »
Thank you JdeHaan, GetMem and Thaddy for responding so quickly to my question. My question felt like a newbie question though I am not new to Pascal. It has been quite a few years since I have done any programming with the language. So I appreciate your help. I did recognise the 'uses matn' statement Thaddy but thanks for pointing it out. I do apologise for not finding the information in the docs. I tried searching but I guess I didn't use the correct search terms.

As a side note, I am so glad the Lazarus project is here. There are some of us "old timers" who still love Pascal for its structure and strong "type" requirements. Particularly those of us who need the restrictions to keep us in line. :)
« Last Edit: September 01, 2017, 07:45:27 pm by Yukiko »

 

TinyPortal © 2005-2018