00001 #include "xtended.hh"
00002 #include "Text.hh"
00003 #include <math.h>
00004
00005 #include "floats.hh"
00006
00007
00008 class SqrtPrim : public xtended
00009 {
00010
00011 public:
00012
00013 SqrtPrim() : xtended("sqrt") {}
00014
00015 virtual unsigned int arity () { return 1; }
00016
00017 virtual bool needCache () { return true; }
00018
00019 virtual Type infereSigType (const vector<Type>& args)
00020 {
00021 assert (args.size() == 1);
00022 Type t = args[0];
00023 interval i = t->getInterval();
00024 if (i.valid && i.lo >=0) {
00025 return castInterval(floatCast(t), interval(sqrt(i.lo), sqrt(i.hi)));
00026 } else {
00027 return castInterval(floatCast(t), interval());
00028 }
00029 }
00030
00031 virtual void sigVisit (Tree sig, sigvisitor* visitor) {}
00032
00033 virtual int infereSigOrder (const vector<int>& args) {
00034 return args[0];
00035 }
00036
00037
00038 virtual Tree computeSigOutput (const vector<Tree>& args) {
00039
00040 num n;
00041 if (isNum(args[0],n)) {
00042 return tree(sqrt(double(n)));
00043 } else {
00044 return tree(symbol(), args[0]);
00045 }
00046 }
00047
00048 virtual string generateCode (Klass* klass, const vector<string>& args, const vector<Type>& types)
00049 {
00050 assert (args.size() == arity());
00051 assert (types.size() == arity());
00052
00053 return subst("sqrt$1($0)", args[0], isuffix());
00054 }
00055
00056 virtual string generateLateq (Lateq* lateq, const vector<string>& args, const vector<Type>& types)
00057 {
00058 assert (args.size() == arity());
00059 assert (types.size() == arity());
00060
00061 return subst("\\sqrt{$0}", args[0]);
00062 }
00063
00064 };
00065
00066
00067 xtended* gSqrtPrim = new SqrtPrim();
00068
00069