Recent

Author Topic: Compiling the code  (Read 732 times)

Sneg

  • New member
  • *
  • Posts: 5
Compiling the code
« 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.

MarkMLl

  • Hero Member
  • *****
  • Posts: 2063
Re: Compiling the code
« Reply #1 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
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.

cdbc

  • Sr. Member
  • ****
  • Posts: 322
    • http://www.cdbc.dk
Re: Compiling the code
« Reply #2 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
If it ain't broke, don't fix it ;)

wildfire

  • Jr. Member
  • **
  • Posts: 78
Re: Compiling the code
« Reply #3 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.
Windows 10 64bit version 1809
Laz 2.0.0 FPC 3.0.4

Sneg

  • New member
  • *
  • Posts: 5
Re: Compiling the code
« Reply #4 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.]
« Last Edit: November 28, 2020, 07:12:31 am by Sneg »

lucamar

  • Hero Member
  • *****
  • Posts: 3595
Re: Compiling the code
« Reply #5 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.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.10/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

jamie

  • Hero Member
  • *****
  • Posts: 4196
Re: Compiling the code
« Reply #6 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  >:(
The only true wisdom is knowing you know nothing

Sneg

  • New member
  • *
  • Posts: 5
Re: Compiling the code
« Reply #7 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. ""

lucamar

  • Hero Member
  • *****
  • Posts: 3595
Re: Compiling the code
« Reply #8 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 ;)
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.10/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

jamie

  • Hero Member
  • *****
  • Posts: 4196
Re: Compiling the code
« Reply #9 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.

 
The only true wisdom is knowing you know nothing

Paolo

  • Full Member
  • ***
  • Posts: 147
Re: Compiling the code
« Reply #10 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.  

lucamar

  • Hero Member
  • *****
  • Posts: 3595
Re: Compiling the code
« Reply #11 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. :)
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.10/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

 

TinyPortal © 2005-2018