Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is my formula wrong or the intrinsic function "abs" doesn't work?

Status
Not open for further replies.

maki1337

Technical User
Sep 9, 2012
4
PH
I am using f95. Can anyone help me

program Quadraticr
integer A,B,C
print*,"This program computes for the roots of a polynomial"
print*,"Please input the coefficients of your polynomial A,B,and C,where A,B, and C is in the form AX^2+BX+C "
read*, A,B,C
print*,"You have inputed:"
print*, A,"X^2+",B,"X+",C,"=0"
call quad(A,B,C)
end

subroutine quad(A,B,C)
integer A,B,C,x,y
x=(-1*B+((abs((B**2)-(4*A*C)))**(1/2)))/(2*A)
y=(-1*B-((abs((B**2)-(4*A*C)))**(1/2)))/(2*A)

print*,"The roots are:"
print*,x,"and",y
end
 
program Quadraticr
integer A,B,C
print*,"This program computes for the roots of a polynomial"
print*,"Please input the coefficients of your polynomial A,B,and C,where A,B, and C is in the form AX^2+BX+C "
read*, A,B,C
print*,"You have inputed:"
print*, A,"X^2+",B,"X+",C,"=0"
call quad(A,B,C)
end

subroutine quad(A,B,C)
integer A,B,C,x,y
x=(-1*B + ((abs((B**2)-(4*A*C)))**(1/2)))/(2*A)
y=(-1*B - ((abs((B**2)-(4*A*C)))**(1/2)))/(2*A)

print*,"The roots are:"
print*,x,"and",y
end
 
Hi, maki1337. I can see you sorted out whatever problem you had. But I'm just curious about why you
should specify the roots of your quadratic as "integer" (instead of "real"). I don't think you can
always know before hand if you'll get integer roots or otherwise. Unless you're only interested in
the integral part of each root (not the fractional part), or unless you always choose the coefficients
such that the resulting roots are integers.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top