11-05-2016, 02:57 PM
I want to print in the terminal window, a numeric or symbolic value, depending on whether, is the same or different in their approximate or exact value, to avoid print two the same result. For this I am writing a program that compares two expressions exactSameApprox(expr1, expr2)
terms
1: If the approximate value is the same that exact return "TRUE"
exactSameApprox(5, 5) -> true
"5" same "5" -> true
numbers with fractional part == 0, are considered equals, it requires a function to remove the fractional part delDot( mExpr )
exactSameApprox(1, 1.0) -> true or
exactSameApprox(1, 1.) -> true or
exactSameApprox(1.0, 1) -> true or
1.0 -> 1
"1" same "1" -> true
2: if the exact value is different from the approximate value must be return FALSE, BUT MY CODE FAILS.
exactSameApprox(2, 4/2) -> true // FAIL =( must be return FALSE
"2" same "4/2" -> false
Who can help me improve the code above?
the problem is that when storing, or convert to string a mathematical expression, this first is evaluated.
Using QUOTE (not to recall the argument of the input arg) to convert string =(
Thanks
PHP Code:
#cas
exactSameApprox(expr1, expr2):=
begin
//print(delDot(expr1) == delDot(expr2)); wait();
if eq(delDot(expr1),delDot(expr2)) == true then
return true;
else
return false;
end;
end;
delDot( mExpr ):=
begin
local exprStr := string( mExpr );
if fp( approx( mExpr ) ) == 0 then
exprStr:= left(exprStr, instring(exprStr,".")-1); // the decimal dot is deleted when the fractional part is zero
end;
return exprStr;
end;
#end
terms
1: If the approximate value is the same that exact return "TRUE"
exactSameApprox(5, 5) -> true
"5" same "5" -> true
numbers with fractional part == 0, are considered equals, it requires a function to remove the fractional part delDot( mExpr )
exactSameApprox(1, 1.0) -> true or
exactSameApprox(1, 1.) -> true or
exactSameApprox(1.0, 1) -> true or
1.0 -> 1
"1" same "1" -> true
2: if the exact value is different from the approximate value must be return FALSE, BUT MY CODE FAILS.
exactSameApprox(2, 4/2) -> true // FAIL =( must be return FALSE
"2" same "4/2" -> false
Who can help me improve the code above?
the problem is that when storing, or convert to string a mathematical expression, this first is evaluated.
Using QUOTE (not to recall the argument of the input arg) to convert string =(
PHP Code:
deldot(mexpr):=
begin
local exprstr:= string( quote(mexpr) ); // -> exprstr:=" 'mexpr' " and not 'rcl(mexpr)',
Thanks