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