Merge branch 'master' of https://scm.cri.ensmp.fr/git/Faustine
[Faustine.git] / interpretor / preprocessor / faust-0.9.47mr3 / compiler / extended / logprim.cpp
1 #include "xtended.hh"
2 #include "Text.hh"
3 #include <math.h>
4
5 #include "floats.hh"
6
7 class LogPrim : public xtended
8 {
9
10 public:
11
12 LogPrim() : xtended("log") {}
13
14 virtual unsigned int arity () { return 1; }
15
16 virtual bool needCache () { return true; }
17
18 virtual Type infereSigType (const vector<Type>& args)
19 {
20 assert (args.size() == arity());
21 interval i = args[0]->getInterval();
22 if (i.valid & i.lo>0) {
23 return castInterval(floatCast(args[0]), interval(log(i.lo), log(i.hi)));
24 } else {
25 return floatCast(args[0]);
26 }
27 }
28
29 virtual void sigVisit (Tree sig, sigvisitor* visitor) {}
30
31 virtual int infereSigOrder (const vector<int>& args) {
32 assert (args.size() == arity());
33 return args[0];
34 }
35
36
37 virtual Tree computeSigOutput (const vector<Tree>& args) {
38 num n;
39 assert (args.size() == arity());
40 if (isNum(args[0],n)) {
41 return tree(log(double(n)));
42 } else {
43 return tree(symbol(), args[0]);
44 }
45 }
46
47 virtual string generateCode (Klass* klass, const vector<string>& args, const vector<Type>& types)
48 {
49 assert (args.size() == arity());
50 assert (types.size() == arity());
51
52 return subst("log$1($0)", args[0], isuffix());
53 }
54
55 virtual string generateLateq (Lateq* lateq, const vector<string>& args, const vector<Type>& types)
56 {
57 assert (args.size() == arity());
58 assert (types.size() == arity());
59
60 return subst("\\ln\\left( $0 \\right)", args[0]);
61 }
62
63 };
64
65
66 xtended* gLogPrim = new LogPrim();
67
68