Recent

Author Topic: Strange error while trying my program.  (Read 8215 times)

RubenMG

  • Newbie
  • Posts: 5
Strange error while trying my program.
« on: February 10, 2018, 05:08:37 pm »
Hi all !

First of all, thanks for your time!

I'm  learning programming with Pascal and i did a simple program to solve Quadratic equations, if i put low numbers it will run,but if i provide big numbers suchs as 12 (wow.. so big..!) it will give me this error:

'EXTERNAL:SIGFPE'.

I put you the code:

Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. {Ec:SegGrado}
  4.  
  5. var
  6.  
  7.   a : Integer;
  8.   b : Integer;
  9.   c : Integer;
  10.   TotalPositivo : Extended;
  11.   TotalNegativo : Extended;
  12.   Raiz : Extended;
  13.  
  14.  
  15. begin
  16.  
  17.   WriteLn('Introduce las variables: a,b,c');
  18.  
  19.   readLn(a);
  20.   readLn(b);
  21.   readLn(c);
  22.  
  23.   Raiz:=sqrt((b*b)-4*(a*c));
  24.  
  25.   if b > 0 then
  26.      WriteLn('La ecuacion es: ', a, 'x^2 +',b, 'x ', c)
  27.   else
  28.       WriteLn('La ecuacion es: ', a, 'x^2 ',b, 'x ', c);
  29.  
  30.   WriteLn('Los resultados son:');
  31.  
  32.   TotalPositivo:=(-b+Raiz)/(2*a);
  33.   WriteLn(TotalPositivo:4:3);
  34.   TotalNegativo:=(-b-Raiz)/(2*a);
  35.   WriteLn(TotalNegativo:4:3);
  36.   readLn();
  37.  
  38. end.
  39.  
  40.  

Best regards, Rubén.

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Strange error while trying my program.
« Reply #1 on: February 10, 2018, 05:16:31 pm »
Make sure no negative number is passed to sqrt. Use abs to get the absolute value.

Josh

  • Hero Member
  • *****
  • Posts: 1274
Re: Strange error while trying my program.
« Reply #2 on: February 10, 2018, 05:19:28 pm »
Hi

Welcome to forum.
I have not tried your code but what jumps out is sqrt of a negative number.
 
Make sure that this does not evaluate to a negative number  (b*b)-4*(a*c)

Ooops beaten to the post.....
The best way to get accurate information on the forum is to post something wrong and wait for corrections.

RubenMG

  • Newbie
  • Posts: 5
Re: Strange error while trying my program.
« Reply #3 on: February 10, 2018, 05:46:56 pm »
Make sure no negative number is passed to sqrt. Use abs to get the absolute value.

I see !! Ok, thank you very much!!
Hi

Welcome to forum.
I have not tried your code but what jumps out is sqrt of a negative number.
 
Make sure that this does not evaluate to a negative number  (b*b)-4*(a*c)

Ooops beaten to the post.....

Anyway, thank you very much!!


jamie

  • Hero Member
  • *****
  • Posts: 6130
Re: Strange error while trying my program.
« Reply #4 on: February 10, 2018, 07:50:13 pm »
First if all, you should be in a mode other than the default, currently you are limited to 16 bit integers.

at the top of your source code after Program Name line;

{$mode objfpc}
  This will give you much larger integer numbers etc..

Secondly :
   
 I've ran into the case of Sqrt not dealing with -(numbers) but I needed the results to remain that way. I did this.

 Function MySqrt(num:double):Double;
  Begin
    If num < 0 Then Result := -(Sqrt(abs(num)))
      else
   Result := sqrt(abs(num));
 end;

 It may not be the standard but it gets you out of the hole with your equation.
« Last Edit: February 10, 2018, 07:53:54 pm by jamie »
The only true wisdom is knowing you know nothing

Josh

  • Hero Member
  • *****
  • Posts: 1274
Re: Strange error while trying my program.
« Reply #5 on: February 10, 2018, 08:04:29 pm »
Hi

Just an observation.

I din't think you could sqrt a negative number, as you cannot get back to the starting number by multiplying itself.
ie
sqrt(16)*sqrt(16)=16

if you negate the result of sqrt(-16) by negating the sqrt(abs(-16))
sqrt(-16)*sqrt(-16) becomes -4*-4=+16 ( not -16).  You cannot get back to the -16 starting value
The best way to get accurate information on the forum is to post something wrong and wait for corrections.

jamie

  • Hero Member
  • *****
  • Posts: 6130
Re: Strange error while trying my program.
« Reply #6 on: February 10, 2018, 10:16:56 pm »
Yes,  if sign on the right is (-) it inverts the sign on the left.

 so if you want to maintain the sign in this state you use Abs(right-side);

So..

 MySqrt(-16)*Abs(Mysqrt(-16)) = -16;

 a basic short cut to making a number reverse sign is to simply multiply it by -1
 
 I am sure you've seen that somewhere ;)

at least that is how I understand it anyway.


 At least it should be
The only true wisdom is knowing you know nothing

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Strange error while trying my program.
« Reply #7 on: February 10, 2018, 11:08:44 pm »
I din't think you could sqrt a negative number, as you cannot get back to the starting number by multiplying itself.

There is an extension to the familiar real numbers based on the idea of sqrt of negative numbers.

jamie

  • Hero Member
  • *****
  • Posts: 6130
Re: Strange error while trying my program.
« Reply #8 on: February 11, 2018, 03:52:39 pm »
that hurts my head, brings back the old days of school!, Long, long ago! :P
The only true wisdom is knowing you know nothing

Bart

  • Hero Member
  • *****
  • Posts: 5290
    • Bart en Mariska's Webstek
Re: Strange error while trying my program.
« Reply #9 on: February 11, 2018, 04:59:38 pm »
IIRC...

Code: Pascal  [Select][+][-]
  1. uses
  2.   ucomplex;
  3.  
  4. var
  5.   x1, x2: complex;
  6.   a,b,c: integer;
  7. begin
  8.   //put some code here to init a,b and c
  9.  
  10.   x1 := -b + csqrt(csqr(b)-4*(a*c));
  11.   x2 := -b - csqrt(csqr(b)-4*(a*c));
  12.   writeln('x1 = ',cstr(x1));
  13.   writeln('x2 = ',cstr(x2));
  14. end;
  15.  

Bart

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: Strange error while trying my program.
« Reply #10 on: February 11, 2018, 05:06:20 pm »
IIRC...
Does the ucomplex or math unit still not have an operator overloaded for sqrt for complex? instead of csqrt?
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

Bart

  • Hero Member
  • *****
  • Posts: 5290
    • Bart en Mariska's Webstek
Re: Strange error while trying my program.
« Reply #11 on: February 11, 2018, 05:27:36 pm »
Does the ucomplex or math unit still not have an operator overloaded for sqrt for complex? instead of csqrt?

Operator for sqr/sqrt?

I tried to overload sqrt for complex type, but then compiling sqrt(integer) gives: main.pp(55,10) Error: Illegal expression.

sqr and sqrt are compilerprocs, I don't even know if you can overload them.

Bart

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: Strange error while trying my program.
« Reply #12 on: February 11, 2018, 05:42:31 pm »
I tried to overload sqrt for complex type, but then compiling sqrt(integer) gives: main.pp(55,10) Error: Illegal expression.
Yes you can. use
Code: [Select]
system.sqrt So fully qualified. Then you can add it to ucomplex.
You know what I mean.
« Last Edit: February 11, 2018, 05:50:51 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

Bart

  • Hero Member
  • *****
  • Posts: 5290
    • Bart en Mariska's Webstek
Re: Strange error while trying my program.
« Reply #13 on: February 11, 2018, 05:49:49 pm »
Doesn;t that kind of defeat the use of overloading?
If the price I pay is having to use system.sqrt instead of sqrt, then just sticking with csqrt is less effort.

Bart

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: Strange error while trying my program.
« Reply #14 on: February 11, 2018, 05:52:38 pm »
Doesn;t that kind of defeat the use of overloading?
If the price I pay is having to use system.sqrt instead of sqrt, then just sticking with csqrt is less effort.

Bart
No, system.sqrt is only necessary for the internal calculation of the overloaded sqrt in another place. E.g. the real part.
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

 

TinyPortal © 2005-2018