Numworks Epsilon  1.4.1
Graphing Calculator Operating System
randint.cpp
Go to the documentation of this file.
1 #include <poincare/randint.h>
2 #include <poincare/random.h>
3 #include <ion.h>
4 
5 extern "C" {
6 #include <assert.h>
7 }
8 #include <cmath>
9 
10 namespace Poincare {
11 
13  return Type::Randint;
14 }
15 
17  Randint * a = new Randint(m_operands, true);
18  return a;
19 }
20 
21 template <typename T> Expression * Randint::templateApproximate(Context & context, AngleUnit angleUnit) const {
22  Expression * aInput = operand(0)->approximate<T>(context, angleUnit);
23  Expression * bInput = operand(1)->approximate<T>(context, angleUnit);
24  if (aInput->type() != Type::Complex || bInput->type() != Type::Complex) {
25  return new Complex<T>(Complex<T>::Float(NAN));
26  }
27  T a = static_cast<Complex<T> *>(aInput)->toScalar();
28  T b = static_cast<Complex<T> *>(bInput)->toScalar();
29  delete aInput;
30  delete bInput;
31  if (std::isnan(a) || std::isnan(b) || a != std::round(a) || b != std::round(b) || a > b) {
32  return new Complex<T>(Complex<T>::Float(NAN));
33 
34  }
35  T result = std::floor(Random::random<T>()*(b+1.0-a)+a);
36  return new Complex<T>(Complex<T>::Float(result));
37 }
38 
39 }
#define NAN
Definition: math.h:30
Expression * approximate(Context &context, AngleUnit angleUnit=AngleUnit::Default) const
Definition: expression.cpp:338
Expression * clone() const override
Definition: randint.cpp:16
#define T(x)
Definition: events.cpp:26
#define round(x)
Definition: math.h:192
const Expression * m_operands[T]
#define isnan(x)
Definition: math.h:43
static Complex< T > Float(T x)
Definition: complex.cpp:23
#define floor(x)
Definition: math.h:179
const Expression * operand(int i) const
Definition: expression.cpp:78
virtual Type type() const =0
Type type() const override
Definition: randint.cpp:12