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