For Programmers: Free Programming Magazines  


Home > Archive > Fortran > September 2006 > ERROR in resoult of a program









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author ERROR in resoult of a program
sandrus

2006-09-20, 4:01 am

Hello I'm italian so excuse me for my english.
i'm trying to make a program called "radici" that can solve math
expression like ax^2+bx+c=0

I'm having some problems with the resoults that this program gives.
Here's my program:

PROGRAM radici
IMPLICIT NONE
REAL :: a,b,c,discriminante
REAL :: parte_immaginaria
REAL :: parte_reale
REAL :: x1,x2
WRITE(*,*) 'Digita i coefficenti a,b e c'
READ(*,*) a,b,c
WRITE(*,*) 'I coefficenti sono: ', a, b, c
discriminante=b**2-4*a*c
if (discriminante>0) then
x1= ((-b+discriminante)/(2*a))
x2= ((-b-discriminante)/(2*a))
WRITE(*,*) 'Questa equazione ha radici reali'
WRITE(*,*) 'x1= ',X1,'x2',X2
ELSE IF (discriminante<0) then
parte_reale=(-b)/(2*a)
parte_immaginaria=(abs(discriminante)/(2*a))
WRITE(*,*) 'Questa equazione ha radici complesse'
WRITE(*,*) 'x1= ',parte_reale, '+1',parte_immaginaria
WRITE(*,*) 'x2= ',parte_reale, '-1',parte_immaginaria
ELSE IF (discriminante==0) then
x1=(-b)/(2*a)
WRITE(*,*) 'Questa equazione ha due radici identiche'
WRITE(*,*) 'X1=X2=',X1
ELSE
END IF
STOP
END PROGRAM radici


The problem is that using a simple expression as x^2 + 3x -4 (resoults
-4 and 1) the program gives -14 and 11: first case it exceeds of -10 ,
second of +10.

What do i have to do?
I'm very interested in the part of delta>0 and <0, but if u find some
mistakes in the program, tell me. Tnx!

Michel Olagnon

2006-09-20, 4:01 am

sandrus wrote:
> Hello I'm italian so excuse me for my english.
> i'm trying to make a program called "radici" that can solve math
> expression like ax^2+bx+c=0
>
> I'm having some problems with the resoults that this program gives.
> Here's my program:
>
> PROGRAM radici
> IMPLICIT NONE
> REAL :: a,b,c,discriminante
> REAL :: parte_immaginaria
> REAL :: parte_reale
> REAL :: x1,x2
> WRITE(*,*) 'Digita i coefficenti a,b e c'
> READ(*,*) a,b,c
> WRITE(*,*) 'I coefficenti sono: ', a, b, c
> discriminante=b**2-4*a*c
> if (discriminante>0) then
> x1= ((-b+discriminante)/(2*a))


x1= ((-b+ SQRT (discriminante)/(2*a)) might seem better.

Michel Olagnon

2006-09-20, 4:01 am

Michel Olagnon wrote:
> sandrus wrote:
>
>
>
> x1= ((-b+ SQRT (discriminante)/(2*a)) might seem better.
>



x1= (-b+ SQRT (discriminante))/(2*a) of course.

Ron Shepard

2006-09-20, 7:01 pm

In article <451100C4.6060303@ifremer-a-oter.fr>,
Michel Olagnon <molagnon@ifremer-a-oter.fr> wrote:

> Michel Olagnon wrote:
>
>
> x1= (-b+ SQRT (discriminante))/(2*a) of course.


And, as everyone knows, you only want to compute this when "b" is
negative. If "b" is positive, then you want the other sign in the
numerator. That is, you do not want to subtract two values that are
nearly equal, but it is alright to add them.

$.02 -Ron Shepard
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com