Lazarus

Free Pascal => Beginners => Topic started by: Sneg on November 27, 2020, 05:19:19 pm

Title: Compiling the code
Post by: Sneg on November 27, 2020, 05:19:19 pm
Hello everyone, I was given a task that I have no idea how to do. Need your help.
Task: Create a program of a branching structure for calculating a given function.
Title: Re: Compiling the code
Post by: MarkMLl on November 27, 2020, 05:46:44 pm
Code: Pascal  [Select][+][-]
  1. if x <= -3 then
  2.  ...
  3. else
  4.   if x > 0 then
  5. ...
  6.   else
  7. ...
  8.  

MarkMLl
Title: Re: Compiling the code
Post by: cdbc on November 27, 2020, 05:55:49 pm
Hi
I think, I would use 4 functions:
One for each line and then a sum function at the end to sum it all up.
Break down the task into parts and solve them one by one  :)
Regards Benny
Title: Re: Compiling the code
Post by: wildfire on November 27, 2020, 06:07:05 pm
Hi
I think, I would use 4 functions:
One for each line and then a sum function at the end to sum it all up.

There's nothing to sum up, each line is mutually exclusive.
Title: Re: Compiling the code
Post by: Sneg on November 27, 2020, 08:00:15 pm
Code: Pascal  [Select][+][-]
  1. var x,y:double;
  2. begin
  3. x:=StrToFloat(Edit1.Text);
  4. if x<=-3 then
  5. y:=abs((5*cos(power(x-7,0.2)/sin(power(x-7,0.2)))))
  6. else if x<=0 then
  7.  y:=5*power(x+1,2+x)+5*ln((x+2)/(x+3))
  8. else y:=exp(-4*x/(x+2))-7*x;
  9. Edit2.Text:=FloatToStr(y);
  10. end;  

What's wrong with the code?

[Edited to add code tags - see How to use the Forum (https://wiki.freepascal.org/Forum).]
Title: Re: Compiling the code
Post by: lucamar on November 27, 2020, 08:25:46 pm
Yo mean because what happens if x is negative? You didn't read the docs, did you? ;D

Quote from: RTL - Reference for unit Math: Power
power raises base to the power power. This is equivalent to exp(power*ln(base)). Therefore base should be non-negative.
Title: Re: Compiling the code
Post by: jamie on November 27, 2020, 08:33:10 pm
The cubic square of 5 on the first one seems to be a little out of in left field using COS/ SINS ? I don't know where that is coming from unless it's something you plugged in ?

 But this is what I would do

  Power(X-7 , 1/5);

  so basically an inverse to get Power to square instead.

  its been years since I've been in school  >:(
Title: Re: Compiling the code
Post by: Sneg on November 27, 2020, 08:41:15 pm
The cubic square of 5 on the first one seems to be a little out of in left field using COS/ SINS ? I don't know where that is coming from unless it's something you plugged in ?

 But this is what I would do

  Power(X-7 , 1/5);

  so basically an inverse to get Power to square instead.

  its been years since I've been in school  >:(

In the first field, the root is in place, the code was fixed, but at -3 and below it knocks out an error, and after the time of this program it closes with the following error
GDB command:
"-exec-run"
returned an error:
", msg =" During startup program exited with code 0xc0000022. ""
Title: Re: Compiling the code
Post by: lucamar on November 27, 2020, 09:28:57 pm
Here (Linux-gtk2) when you compile in "Release" mode and run "without debugging", any negative number (other than -2) will give you a NaN as result and that's because you're trying to Power() a base less than zero.

Note, though, that is not at startup, but after you set Edit1 and click the button*. If you're seeing an error at startup then there has to be another cause, possibly accessing a control which is not yet there, or some such thing.


* My test program had two edits an a "do it!" button with your code in its OnClick() handler ... though a little better formated ;)
Title: Re: Compiling the code
Post by: jamie on November 27, 2020, 10:44:05 pm
That's like the Sqrt(-1), your Mr. I number or "j" if your a electronics person..
imaginary numbers should of been kicked to the curbside before it hit print in the scrolls..


 In any case you can apply the  "-" outside to fix such issues.

 but then again if its in a variable that is unknown at compile time you then need to test for that and apply it afterwards.

 
Title: Re: Compiling the code
Post by: Paolo on November 28, 2020, 03:19:04 pm
@lucamar
Quote
do mean because what happens if x is negative? You didn't read the docs, did you? ;D

Quote from: RTL - Reference for unit Math: Power
power raises base to the power power. This is equivalent to exp(power*ln(base)). Therefore base should be non-negative.

power correctly handled negative number at base, provided that are exponent is "integer" othherwise the sign rules are undefinied

power(-10, -3);  works perfectly, as expected
power(-10, -3.1); doesn't work

here the code that correctly handles negative "integer" base intercepting integer exponents

Code: Pascal  [Select][+][-]
  1. function power(base,exponent : float) : float;
  2.   begin
  3.     if Exponent=0.0 then
  4.       result:=1.0
  5.     else if (base=0.0) and (exponent>0.0) then
  6.       result:=0.0
  7.     else if (abs(exponent)<=maxint) and (frac(exponent)=0.0) then
  8.       result:=intpower(base,trunc(exponent))
  9.     else
  10.       result:=exp(exponent * ln (base));
  11.   end;
  12.  
Title: Re: Compiling the code
Post by: lucamar on November 28, 2020, 04:28:33 pm
power correctly handled negative number at base, provided that are exponent is "integer" othherwise the sign rules are undefinied
[...]

A bug in the docs, then. Thanks for telling me. :)
TinyPortal © 2005-2018