Square root finding using prolog -


i worked on following coding find square root. didn't work. couldn't find error. finding value of y it's working. square root part not.

print('a = '), read(a), print('b = '), read(b), print('c = '), read(c), nl, nl, x 2*a, y (b^2 - 4*a*c), z sqrt(y),  r1 (-b+z)/x, r2 (-b-z)/x,  print('r1 = '), print(r1), nl, print('r2 = '), print(r2), nl. 

first, if experimenting in prolog, refrain using read/1 , other side-effectful built-ins. instead, type in values want try out. similarly, not need print results. prolog's toplevel you.

?- = 1, b = 2, c = 1,    x 2*a,    y (b^2 - 4*a*c),    z sqrt(y),    r1 (-b+z)/x,    r2 (-b-z)/x. = c, c = 1, b = x, x = 2, y = 0, z = 0.0, r1 = r2, r2 = -1.0. 

the answer looks fine me. let's @ error get:

error: sqrt/1: arithmetic: evaluation error: `undefined' 

what systems says here value computes sqrt/1 not defined. in

?- x sqrt(-1). error: sqrt/1: arithmetic: evaluation error: `undefined' 

the evaluable functor sqrt/1 defined floats - approximation real numbers. however, here rather expect imaginary number. that's why value undefined.

so, avoid error have add appropriate tests prior z sqrt(y).


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -