Assigning sin function response to the term sin (X) in prologue
I wrote a Prolog program to solve simple trigonometric equations. I wrote it to get the value of a trigonometric function. For example, I can get a value sin(45)
, but I cannot assign a value to a sin(45)
term sin(45)
. I tried the operators =,==,=:=
, but they didn't work. Actually, I want to pass a value sin(45)
to the following program codes instead of a term sin(45)
. Thank...
source to share
I have studied and understood that meanings cannot be assigned to a term using =
, ==
or =:=
as procedural languages. When we use these operators, the prologue simply compares the two values. Therefore, we must pass the value as a parameter. As an example,
isolax(1,sin(U) = V,U = R):-
getasin(V,R).
getatan(X,R):-R is atan(X).
Even if we do not define facts or rules for obtaining sin, cos, tan, proog answers to sin, cos, tan automatically.
source to share
arithmetic is done by some specialized built-in functions such as is / 2 or (<) / 2 that evaluate their right as an expression, and unify the numeric value on the left. The most common use is to assign a value to a free variable, for example in
?- X is sin(pi/2).
X = 1.0.
Note the sin / 1 it argument in radians. After evaluating, you pass the variable, now bound to a numeric value, right down to the following code.
source to share
Complex members in the prologue ("functors" or "structures") do not have any numeric "values". The Prolog arithmetic engine does not "assign" a computed value to such a term, it simply uses the computed value as an argument in an external operation, or constrains the computed value to a variable (as "there is a built-in predicate")
Y is sin(45), ... % Y is now now bound to the numeric result of sin(45), do with Y what you whant
If you want not to forget about the genesis of the sin value (45), you can create another term with additional parameters, for example
Y is sin(45), MyTerm=funcresult(sin,45,Y), ... % then pass MyTerm wherever you whant or return it to the caller
The MyTerm user can then combine
MyTerm= funcresult(F,X,Y),
and get also the function name (F = sin) and argument (X = 45) along with the result itself (Y)
you can even
assert(funcresult(sin,X,Y))
and provide a database for all the already calculated function values (don't forget
:-dynamic(funcresult/3).
in this case).
Also note: sin () usually takes radians, not estimates, so sin (45) probably gives what you don't expect
source to share