45 c = th != 0 &&
std::fabs(
c/th) <= Expression::epsilon<T>() ? 0 :
c;
46 s = th != 0 &&
std::fabs(s/th) <= Expression::epsilon<T>() ? 0 : s;
64 static inline T privateFloatSetSign(
T f,
bool negative) {
73 if (digits ==
nullptr) {
77 const char * digit = digits;
78 for (
int i = 0; i < length; i++) {
88 const char * fractionalPart,
int fractionalPartLength,
89 const char * exponent,
int exponentLength,
bool exponentNegative) {
90 T i = digitsToFloat<T>(integralPart, integralPartLength);
91 T j = digitsToFloat<T>(fractionalPart, fractionalPartLength);
92 T l = privateFloatSetSign<T>(digitsToFloat<T>(exponent, exponentLength), exponentNegative);
148 T a = m_a == 0 ? 0 : m_a;
152 result = result - 2*
M_PI;
189 return m_a < 0.0 || (m_a == 0.0 && m_b < 0.0);
190 case Type::Subtraction:
193 return m_a < 0.0 || m_b < 0.0 || (m_a != 0.0 && m_b != 0.0);
194 case Type::Factorial:
197 return m_a < 0.0 || m_b != 0.0;
213 ExpressionLayout * Complex<T>::privateCreateLayout(
PrintFloat::Mode floatDisplayMode, Expression::ComplexFormat complexFormat)
const {
214 assert(floatDisplayMode != PrintFloat::Mode::Default);
216 return createPolarLayout(floatDisplayMode);
218 return createCartesianLayout(floatDisplayMode);
222 Expression * Complex<T>::CreateDecimal(
T f) {
224 return new Undefined();
226 int e = IEEE754<T>::exponentBase10(f);
227 int64_t mantissaf = f *
std::pow((
T)10, -e+PrintFloat::k_numberOfStoredSignificantDigits+1);
228 return new Decimal(Integer(mantissaf), e);
232 Expression * Complex<T>::shallowReduce(Context & context, AngleUnit angleUnit) {
233 Expression * a = CreateDecimal(m_a);
234 Expression * b = CreateDecimal(m_b);
236 Addition * add =
new Addition(a, m,
false);
237 a->shallowReduce(context, angleUnit);
238 b->shallowReduce(context, angleUnit);
239 m->shallowReduce(context, angleUnit);
240 return replaceWith(add,
true)->shallowReduce(context, angleUnit);
245 Complex<U> * Complex<T>::templatedApproximate(Context& context, Expression::AngleUnit angleUnit)
const {
250 int Complex<T>::convertComplexToText(
char * buffer,
int bufferSize,
int numberOfSignificantDigits,
PrintFloat::Mode displayMode, Expression::ComplexFormat complexFormat,
char multiplicationSpecialChar)
const {
251 assert(displayMode != PrintFloat::Mode::Default);
252 int numberOfChars = 0;
254 return PrintFloat::convertFloatToText<T>(
NAN, buffer, bufferSize, numberOfSignificantDigits, displayMode);
257 if (r() != 1 || th() == 0) {
258 numberOfChars = PrintFloat::convertFloatToText<T>(r(), buffer, bufferSize, numberOfSignificantDigits, displayMode);
259 if (r() != 0 && th() != 0 && bufferSize > numberOfChars+1) {
260 buffer[numberOfChars++] = multiplicationSpecialChar;
262 buffer[numberOfChars] = 0;
265 if (r() != 0 && th() != 0) {
266 if (bufferSize > numberOfChars+3) {
268 buffer[numberOfChars++] =
'^';
269 buffer[numberOfChars++] =
'(';
271 buffer[numberOfChars] = 0;
273 numberOfChars += PrintFloat::convertFloatToText<T>(th(), buffer+numberOfChars, bufferSize-numberOfChars, numberOfSignificantDigits, displayMode);
274 if (bufferSize > numberOfChars+3) {
275 buffer[numberOfChars++] = multiplicationSpecialChar;
277 buffer[numberOfChars++] =
')';
278 buffer[numberOfChars] = 0;
281 return numberOfChars;
284 if (m_a != 0 || m_b == 0) {
285 numberOfChars = PrintFloat::convertFloatToText<T>(m_a, buffer, bufferSize, numberOfSignificantDigits, displayMode);
286 if (m_b > 0 && !
std::isnan(m_b) && bufferSize > numberOfChars+1) {
287 buffer[numberOfChars++] =
'+';
289 buffer[numberOfChars] = 0;
292 if (m_b != 1 && m_b != -1 && m_b != 0) {
293 numberOfChars += PrintFloat::convertFloatToText<T>(m_b, buffer+numberOfChars, bufferSize-numberOfChars, numberOfSignificantDigits, displayMode);
294 buffer[numberOfChars++] = multiplicationSpecialChar;
296 if (m_b == -1 && bufferSize > numberOfChars+1) {
297 buffer[numberOfChars++] =
'-';
299 if (m_b != 0 && bufferSize > numberOfChars+1) {
301 buffer[numberOfChars] = 0;
303 return numberOfChars;
307 ExpressionLayout * Complex<T>::createPolarLayout(
PrintFloat::Mode floatDisplayMode)
const {
308 char bufferBase[PrintFloat::k_maxFloatBufferLength+2];
309 int numberOfCharInBase = 0;
310 char bufferSuperscript[PrintFloat::k_maxFloatBufferLength+2];
311 int numberOfCharInSuperscript = 0;
314 numberOfCharInBase = PrintFloat::convertFloatToText<T>(
NAN, bufferBase, PrintFloat::k_maxComplexBufferLength, Preferences::sharedPreferences()->numberOfSignificantDigits(), floatDisplayMode);
315 return new StringLayout(bufferBase, numberOfCharInBase);
317 if (r() != 1 || th() == 0) {
318 numberOfCharInBase = PrintFloat::convertFloatToText<T>(r(), bufferBase, PrintFloat::k_maxFloatBufferLength, Preferences::sharedPreferences()->numberOfSignificantDigits(), floatDisplayMode);
319 if (r() != 0 && th() != 0) {
323 if (r() != 0 && th() != 0) {
325 bufferBase[numberOfCharInBase] = 0;
328 if (r() != 0 && th() != 0) {
329 numberOfCharInSuperscript = PrintFloat::convertFloatToText<T>(th(), bufferSuperscript, PrintFloat::k_maxFloatBufferLength, Preferences::sharedPreferences()->numberOfSignificantDigits(), floatDisplayMode);
332 bufferSuperscript[numberOfCharInSuperscript] = 0;
334 if (numberOfCharInSuperscript == 0) {
335 return new StringLayout(bufferBase, numberOfCharInBase);
337 return new BaselineRelativeLayout(
new StringLayout(bufferBase, numberOfCharInBase),
new StringLayout(bufferSuperscript, numberOfCharInSuperscript), BaselineRelativeLayout::Type::Superscript);
341 ExpressionLayout * Complex<T>::createCartesianLayout(
PrintFloat::Mode floatDisplayMode)
const {
342 char buffer[PrintFloat::k_maxComplexBufferLength];
344 return new StringLayout(buffer, numberOfChars);
347 template class Complex<float>;
348 template class Complex<double>;
349 template Complex<double>* Complex<double>::templatedApproximate<
double>(Context&, Expression::AngleUnit)
const;
350 template Complex<float>* Complex<double>::templatedApproximate<
float>(Context&, Expression::AngleUnit)
const;
351 template Complex<double>* Complex<float>::templatedApproximate<
double>(Context&, Expression::AngleUnit)
const;
352 template Complex<float>* Complex<float>::templatedApproximate<
float>(Context&, Expression::AngleUnit)
const;
Expression::Type type() const override
static Preferences * sharedPreferences()
static Complex< T > Cartesian(T a, T b)
int writeTextInBuffer(char *buffer, int bufferSize, int numberOfSignificantDigits=PrintFloat::k_numberOfStoredSignificantDigits) const override
static Complex< T > Polar(T r, T theta)
constexpr Expression::ComplexFormat Cartesian
Complex< T > * clone() const override
Complex & operator=(const Complex &other)
Complex< T > conjugate() const
static Complex< T > Float(T x)
constexpr Event Multiplication
bool needParenthesisWithParent(const Expression *e) const override
constexpr Expression::ComplexFormat Polar
T digitsToFloat(const char *digits, int length)
virtual Type type() const =0