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

#### RubenMG

• New member
• 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.
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.
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.
31.
32.   TotalPositivo:=(-b+Raiz)/(2*a);
33.   WriteLn(TotalPositivo:4:3);
34.   TotalNegativo:=(-b-Raiz)/(2*a);
35.   WriteLn(TotalNegativo:4:3);
37.
38. end.
39.
40.

Best regards, Rubén.

#### engkin

• Hero Member
• Posts: 2196
##### 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: 700
##### 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.....
Development Installation Lazarus 1.3, FPC 2.7.1,Windows 7/8 32/64, OSX, *nix

Test Environment Lazarus & FPC Trunk on Windows and OSX (Cocoa Mainly on OSX). Testing also Crosscompile windows to OSX..
Any posts made from 2015 will be based on Lazarus Trunk.

#### RubenMG

• New member
• 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: 1134
##### 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 »

#### josh

• Hero Member
• Posts: 700
##### 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
Development Installation Lazarus 1.3, FPC 2.7.1,Windows 7/8 32/64, OSX, *nix

Test Environment Lazarus & FPC Trunk on Windows and OSX (Cocoa Mainly on OSX). Testing also Crosscompile windows to OSX..
Any posts made from 2015 will be based on Lazarus Trunk.

#### jamie

• Hero Member
• Posts: 1134
##### 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

#### engkin

• Hero Member
• Posts: 2196
##### 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: 1134
##### 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!

#### Bart

• Hero Member
• Posts: 3247
##### 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

• Hero Member
• Posts: 7344
##### 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?
Brexit. My Indonesian and Dutch friends know what " Tempo doeloe" means....There is no empire.

#### Bart

• Hero Member
• Posts: 3247
##### 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

• Hero Member
• Posts: 7344
##### 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 »
Brexit. My Indonesian and Dutch friends know what " Tempo doeloe" means....There is no empire.

#### Bart

• Hero Member
• Posts: 3247
##### Re: Strange error while trying my program.
« Reply #13 on: February 11, 2018, 05:49:49 pm »
If the price I pay is having to use system.sqrt instead of sqrt, then just sticking with csqrt is less effort.

Bart