Blame view

Giac_maj/epsilon-giac/poincare/src/factorial.cpp 1.45 KB
6663b6c9   adorian   projet complet av...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
  #include <poincare/factorial.h>
  #include "layout/string_layout.h"
  #include "layout/horizontal_layout.h"
  extern "C" {
  #include <assert.h>
  }
  #include <cmath>
  
  namespace Poincare {
  
  Factorial::Factorial(Expression * argument, bool clone) :
    Function("fact")
  {
    setArgument(&argument, 1, clone);
  }
  
  Expression::Type Factorial::type() const {
    return Type::Factorial;
  }
  
  Expression * Factorial::cloneWithDifferentOperands(Expression** newOperands,
          int numberOfOperands, bool cloneOperands) const {
    assert(newOperands != nullptr);
    Factorial * f = new Factorial(newOperands[0], cloneOperands);
    return f;
  }
  
  template<typename T>
  Complex<T> Factorial::templatedComputeComplex(const Complex<T> c) const {
    T n = c.a();
    if (c.b() != 0 || std::isnan(n) || n != (int)n || n < 0) {
      return Complex<T>::Float(NAN);
    }
    T result = 1;
    for (int i = 1; i <= (int)n; i++) {
      result *= (T)i;
      if (std::isinf(result)) {
        return Complex<T>::Float(result);
      }
    }
    return Complex<T>::Float(std::round(result));
  }
  
  ExpressionLayout * Factorial::privateCreateLayout(FloatDisplayMode floatDisplayMode, ComplexFormat complexFormat) const {
    assert(floatDisplayMode != FloatDisplayMode::Default);
    assert(complexFormat != ComplexFormat::Default);
    ExpressionLayout * childrenLayouts[2];
    childrenLayouts[0] = m_args[0]->createLayout(floatDisplayMode, complexFormat);
    childrenLayouts[1] = new StringLayout("!", 1);
    return new HorizontalLayout(childrenLayouts, 2);
  }
  
  }