Post Reply 
[Help: CAS Code] Comparing exact with approx expressions
11-05-2016, 02:57 PM (This post was last modified: 01-04-2017 04:32 AM by compsystems.)
Post: #1
[Help: CAS Code] Comparing exact with approx expressions
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)

PHP Code:
#cas
  
exactSameApprox(expr1expr2):=
  
begin
    
//print(delDot(expr1) == delDot(expr2)); wait();
    
if eq(delDot(expr1),delDot(expr2)) == true then
      
return true;
    else
      return 
false;
    
end;
  
end;

  
delDotmExpr ):=
  
begin
    local exprStr 
:= stringmExpr ); 
    if 
fpapproxmExpr ) ) == 0 then
      exprStr
:= left(exprStrinstring(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
:= stringquote(mexpr) ); // -> exprstr:=" 'mexpr' " and not 'rcl(mexpr)', 

Thanks
Find all posts by this user
Quote this message in a reply
11-05-2016, 08:39 PM (This post was last modified: 11-05-2016 09:17 PM by compsystems.)
Post: #2
RE: [Help: CAS Code] Comparing exact with approx expressions
sorry for my bad English

expr0:=4/2 [enter] 2 ok
string(expr0) [enter] "2" ok

expr1:=quote(4/2) [enter] 4/2 ok
string(expr1) [enter] "2" =( I want to display as "4/2"
now
string(eval(var1,1)) [enter] "4/2" ok =) but within a program, how I make so that the entry is stored as quote?

PHP Code:
compare1(expr1expr2):=
begin
  local str1
:= string(eval(expr1,1));
  
local str2:= string(eval(expr2,1));
  
local rtn;
  print();
  print(
str1); wait();
  print(
str2); wait();
  if 
eq(str1,str2then
      rtn
:= true;
  else
      
rtn:= false;
  
end;
  print(
rtn); wait();
  return 
rtn;
end

compare1(2,4/2) true =(
compare1('2','4/2') false =) but forcing the entry as '' the idea is that the program will become ''

I think that an object between single quotes '' must be stored as such and not evaluate it, otherwise the QUOTE command has not purpose, are you agree?

expr2:='4/2'

type(expr2) -> DOM_QUOTEDEXPR // without evaluate
Find all posts by this user
Quote this message in a reply
11-05-2016, 11:27 PM (This post was last modified: 11-06-2016 05:08 AM by CH3791.)
Post: #3
RE: [Help: CAS Code] Comparing exact with approx expressions
(11-05-2016 08:39 PM)compsystems Wrote:  I think that an object between single quotes '' must be stored as such and not evaluate it, otherwise the QUOTE command has not purpose, are you agree?
This is a very good proposal. However, one thing I don't understand is your use of two arguments for the eval command eg. string(eval(expr1,1)). None of the documentation or manuals state anything about what a second argument will do.

EDIT: Oops, I see you've already discussed it here. http://www.hpmuseum.org/forum/thread-7162.html
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 2 Guest(s)