[prev in list] [next in list] [prev in thread] [next in thread]
List: kde-commits
Subject: Re: [analitza/imaginary] analitza: Fix some issues for complex numbers
From: Percy_Camilo_Triveño_Aucahuasi <percy.camilo.ta () gmail ! com>
Date: 2014-06-15 23:07:10
Message-ID: 8363D5AA-C281-4EDF-B248-AA405A1DD787 () gmail ! com
[Download RAW message or body]
Sorry for that, was late, next time I'll wait and think better about commit messages. \
Details are:
- Fix print style of complex numbers.
- Change to double as base type for complex, rationale: more precision and keep \
consistence with semantic of Cn::value (which already has double as type)
Percy
On Jun 15, 2014, at 4:11 PM, Aleix Pol <aleixpol@kde.org> wrote:
> La descripción de este commit es de lejos incompleta. De hecho lo deberías haber \
> hecho por lo menos en 3 commits que cada uno cambie una cosa distinta. Aparte \
> cambia de float a double el imaginaryPart sin ninguna razón aparente?
> Aleix
>
>
> On Sun, Jun 15, 2014 at 5:25 AM, Percy Camilo Triveño Aucahuasi \
> <percy.camilo.ta@gmail.com> wrote: Git commit \
> 33bac90b347ce5424117e09b24a6d2a4de9dcdf9 by Percy Camilo Triveño Aucahuasi. \
> Committed on 15/06/2014 at 03:25. Pushed by aucahuasi into branch 'imaginary'.
>
> Fix some issues for complex numbers
>
> M +31 -2 analitza/htmlexpressionwriter.cpp
> M +1 -0 analitza/htmlexpressionwriter.h
> M +7 -7 analitza/operations.cpp
> M +24 -2 analitza/stringexpressionwriter.cpp
> M +1 -0 analitza/stringexpressionwriter.h
> M +14 -1 analitza/tests/analitzatest.cpp
> M +3 -3 analitza/value.cpp
> M +7 -7 analitza/value.h
>
> http://commits.kde.org/analitza/33bac90b347ce5424117e09b24a6d2a4de9dcdf9
>
> diff --git a/analitza/htmlexpressionwriter.cpp b/analitza/htmlexpressionwriter.cpp
> index 87db178..ab5a21a 100644
> --- a/analitza/htmlexpressionwriter.cpp
> +++ b/analitza/htmlexpressionwriter.cpp
> @@ -32,6 +32,8 @@
>
> using namespace Analitza;
>
> +const double HtmlExpressionWriter::MIN_PRINTABLE_VALUE = 0.0000000000001; // since \
> the precision we choose for 'G' is 12 +
> //we use the one in string*
> QMap<Operator::OperatorType, QString> initOperators();
>
> @@ -83,10 +85,37 @@ QVariant HtmlExpressionWriter::visit(const List* vec)
>
> QVariant HtmlExpressionWriter::visit(const Cn* var)
> {
> + QString innerhtml;
> if(var->isBoolean())
> - return QString("<span class='var'>"+QString(var->isTrue() ? "true" \
> : "false")+"</span>"); + innerhtml = var->isTrue() ? "true" : \
> "false"; + else if(var->isCharacter())
> + innerhtml = QString(var->character());
> + else if(var->isComplex()) {
> + QString realpart;
> + QString imagpart;
> + bool realiszero = false;
> + if (qAbs(var->complexValue().real()) > MIN_PRINTABLE_VALUE)
> + realpart = QString::number(var->complexValue().real(), 'g', \
> 12); + else
> + realiszero = true;
> +
> + if (var->complexValue().imag() != 1) {
> + if (qAbs(var->complexValue().imag()) > MIN_PRINTABLE_VALUE) \
> { + if (!realiszero && var->complexValue().imag()>0.)
> + realpart += QLatin1String("+");
> + imagpart = \
> QString::number(var->complexValue().imag(), 'g', 12); + \
> imagpart += QLatin1String("*i"); + }
> + } else {
> + imagpart = QLatin1String("i");
> + }
> +
> + innerhtml = realpart+imagpart;
> + }
> else
> - return QString("<span class='num'>"+QString::number(var->value(), \
> 'g', 12)+"</span>"); + innerhtml = QString::number(var->value(), 'g', \
> 12); +
> + return QString("<span class='num'>"+innerhtml+"</span>");
> }
>
> QVariant HtmlExpressionWriter::visit(const Analitza::Ci* var)
> diff --git a/analitza/htmlexpressionwriter.h b/analitza/htmlexpressionwriter.h
> index 0b2660c..0456a21 100644
> --- a/analitza/htmlexpressionwriter.h
> +++ b/analitza/htmlexpressionwriter.h
> @@ -54,6 +54,7 @@ class HtmlExpressionWriter : public AbstractExpressionVisitor
> template <class T>
> static QStringList allValues(T it, const T& itEnd, AbstractExpressionVisitor* \
> writer); QVariant m_result;
> + static const double MIN_PRINTABLE_VALUE;
> };
>
> }
> diff --git a/analitza/operations.cpp b/analitza/operations.cpp
> index cdd0027..f14e463 100644
> --- a/analitza/operations.cpp
> +++ b/analitza/operations.cpp
> @@ -151,19 +151,19 @@ Cn* reduceRealReal(enum Operator::OperatorType op, Cn *oper, \
> double a, double b, return oper;
> }
>
> -static bool operator<(complex<float> a, complex<float> b)
> +static bool operator<(complex<double> a, complex<double> b)
> { return a.real() < b.real() || (a.real() == b.real() && a.imag()<b.imag()); }
>
> -static bool operator>(complex<float> a, complex<float> b)
> +static bool operator>(complex<double> a, complex<double> b)
> { return a.real() > b.real() || (a.real() == b.real() && a.imag()>b.imag()); }
>
> -static bool operator<=(complex<float> a, complex<float> b)
> +static bool operator<=(complex<double> a, complex<double> b)
> { return a.real() <= b.real() || (a.real() == b.real() && a.imag()<=b.imag()); }
>
> -static bool operator>=(complex<float> a, complex<float> b)
> +static bool operator>=(complex<double> a, complex<double> b)
> { return a.real() >= b.real() || (a.real() == b.real() && a.imag()>=b.imag()); }
>
> -Cn* reduceComplexComplex(enum Operator::OperatorType op, Cn *oper, complex<float> \
> a, complex<float> b, QString** correct) +Cn* reduceComplexComplex(enum \
> Operator::OperatorType op, Cn *oper, complex<double> a, complex<double> b, \
> QString** correct) {
> switch(op) {
> case Operator::plus:
> @@ -274,7 +274,7 @@ Cn* reduceComplexComplex(enum Operator::OperatorType op, Cn \
> *oper, complex<float Cn* Operations::reduceValueValue(enum Operator::OperatorType \
> op, Cn *oper, const Cn *oper1, QString** correct) {
> if(KDE_ISUNLIKELY(oper->isComplex() || oper1->isComplex())) {
> - const complex<float> a=oper->complexValue(), \
> b=oper1->complexValue(); + const complex<double> \
> a=oper->complexValue(), b=oper1->complexValue(); return reduceComplexComplex(op, \
> oper, a, b, correct); } else {
> const double a=oper->value(), b=oper1->value();
> @@ -292,7 +292,7 @@ Cn* Operations::reduceUnaryValue(Operator::OperatorType op, Cn* \
> oper, QString**
> Cn* Operations::reduceUnaryComplex(Operator::OperatorType op, Cn* val, QString** \
> correct) {
> - const complex<float> a=val->complexValue();
> + const complex<double> a=val->complexValue();
>
> switch(op) {
> case Operator::minus:
> diff --git a/analitza/stringexpressionwriter.cpp \
> b/analitza/stringexpressionwriter.cpp index 4f434aa..50012b3 100644
> --- a/analitza/stringexpressionwriter.cpp
> +++ b/analitza/stringexpressionwriter.cpp
> @@ -30,6 +30,8 @@
>
> using namespace Analitza;
>
> +const double StringExpressionWriter::MIN_PRINTABLE_VALUE = 0.0000000000001; // \
> since the precision we choose for 'G' is 12 +
> template <class T>
> QStringList StringExpressionWriter::allValues(T it, const T& itEnd, \
> AbstractExpressionVisitor* writer) {
> @@ -105,8 +107,28 @@ QVariant StringExpressionWriter::visit(const Cn* var)
> return var->isTrue() ? "true" : "false";
> else if(var->isCharacter())
> return QString(var->character());
> - else if(var->isComplex())
> - return \
> QVariant::fromValue<QString>(QString::number(var->complexValue().real(), 'g', 12) + \
> QLatin1String("+i*") + QString::number(var->complexValue().imag(), 'g', 12)); + \
> else if(var->isComplex()) { + QString realpart;
> + QString imagpart;
> + bool realiszero = false;
> + if (qAbs(var->complexValue().real()) > MIN_PRINTABLE_VALUE)
> + realpart = QString::number(var->complexValue().real(), 'g', \
> 12); + else
> + realiszero = true;
> +
> + if (var->complexValue().imag() != 1) {
> + if (qAbs(var->complexValue().imag()) > MIN_PRINTABLE_VALUE) \
> { + if (!realiszero && var->complexValue().imag()>0.)
> + realpart += QLatin1String("+");
> + imagpart = \
> QString::number(var->complexValue().imag(), 'g', 12); + \
> imagpart += QLatin1String("*i"); + }
> + } else {
> + imagpart = QLatin1String("i");
> + }
> +
> + return QVariant::fromValue<QString>(realpart+imagpart);
> + }
> else
> return QString::number(var->value(), 'g', 12);
> }
> diff --git a/analitza/stringexpressionwriter.h b/analitza/stringexpressionwriter.h
> index 523de30..6f9fb98 100644
> --- a/analitza/stringexpressionwriter.h
> +++ b/analitza/stringexpressionwriter.h
> @@ -58,6 +58,7 @@ class StringExpressionWriter : public AbstractExpressionVisitor
> static QStringList allValues(T it, const T& itEnd, AbstractExpressionVisitor* \
> writer);
> QVariant m_result;
> + static const double MIN_PRINTABLE_VALUE;
> };
>
> }
> diff --git a/analitza/tests/analitzatest.cpp b/analitza/tests/analitzatest.cpp
> index 47d3c84..fcec6e8 100644
> --- a/analitza/tests/analitzatest.cpp
> +++ b/analitza/tests/analitzatest.cpp
> @@ -115,6 +115,7 @@ void AnalitzaTest::testTrivialCalculate_data()
> QTest::newRow("cpx1") << "i" << Cn(0, 1);
> QTest::newRow("cpx2") << "i*i" << Cn(-1);
> QTest::newRow("cpx3") << "2+i*i" << Cn(1);
> + QTest::newRow("complex number") << "3+4*(5-6*i)" << Cn(23, -24);
> }
>
> void AnalitzaTest::testTrivialCalculate()
> @@ -142,8 +143,14 @@ void AnalitzaTest::testTrivialEvaluate_data()
> {
> QTest::addColumn<QString>("expression");
> QTest::addColumn<QString>("result");
> -
> +
> QTest::newRow("simple value") << "2" << "2";
> + QTest::newRow("simple complex value") << "6*(2+i)" << "12+6*i";
> + QTest::newRow("complex irreductibility") << "i" << "i";
> + QTest::newRow("from complex value") << "i*i" << "-1";
> + QTest::newRow("from power complex") << "power(i, 2)" << "-1";
> + QTest::newRow("sin complex") << "sin(i)" << "1.17520119364*i";
> + QTest::newRow("cos complex") << "cos(5-9*i)" << \
> "1149.26926545-3885.12187972*i"; QTest::newRow("simple addition") << "2+2" << "4";
> QTest::newRow("simple addition with var") << "2+x" << "x+2";
> QTest::newRow("minus irreductibility") << "-x" << "-x";
> @@ -314,6 +321,12 @@ void AnalitzaTest::testCorrection_data()
> QTest::addColumn<QString>("result");
>
> QStringList script;
> +
> + script.clear();
> + script << "f:=y->y*y";
> + script << "f(i)";
> + QTest::newRow("from complex function") << script << "-1";
> +
> script.clear();
> script << "n:=2";
> script << "n+1";
> diff --git a/analitza/value.cpp b/analitza/value.cpp
> index 8f567f1..fe0b8f3 100644
> --- a/analitza/value.cpp
> +++ b/analitza/value.cpp
> @@ -116,12 +116,12 @@ void Cn::setValue(bool v)
> m_imaginaryPart = 0;
> }
>
> -std::complex<float> Cn::complexValue() const
> +std::complex<double> Cn::complexValue() const
> {
> - return std::complex<float>(m_value, m_imaginaryPart);
> + return std::complex<double>(m_value, m_imaginaryPart);
> }
>
> -void Cn::setValue(std::complex<float> v)
> +void Cn::setValue(std::complex<double> v)
> {
> if(v.imag() == 0)
> setValue(v.real());
> diff --git a/analitza/value.h b/analitza/value.h
> index ab60dc0..176c825 100644
> --- a/analitza/value.h
> +++ b/analitza/value.h
> @@ -63,8 +63,8 @@ class ANALITZA_EXPORT Cn : public Object
> explicit Cn(const QChar& c) : Object(Object::value), m_char(c.unicode()), \
> m_imaginaryPart(0), m_format(Char) {}
> /** Constructor. Creates a value that represents a complex. */
> - explicit Cn(float value, float imaginaryPart) : \
> Object(Object::value), m_value(value), m_imaginaryPart(imaginaryPart), \
> m_format(Complex) {qDebug() << "xxxxxxxxxx" << toString();}
> -
> + explicit Cn(double value, double imaginaryPart) : \
> Object(Object::value), m_value(value), m_imaginaryPart(imaginaryPart), \
> m_format(Complex) {} +
> /**
> * Extracts the number from the @p e Dom element and saves it.
> */
> @@ -78,7 +78,7 @@ class ANALITZA_EXPORT Cn : public Object
> void setValue(int v);
> void setValue(uint v);
> void setValue(bool v);
> - void setValue(std::complex<float> v);
> + void setValue(std::complex<double> v);
>
> /**
> * Returns the value.
> @@ -149,16 +149,16 @@ class ANALITZA_EXPORT Cn : public Object
> QChar character() const { Q_ASSERT(m_format==Char); return QChar(m_char); }
>
> /** @returns whether the value has an imaginary part */
> - bool isComplex() const { return m_format == Complex; }
> + bool isComplex() const { return m_format == Complex && \
> m_imaginaryPart!=0.; }
> virtual QVariant accept(AbstractExpressionVisitor*) const;
> - virtual bool isZero() const { return m_value==0. && \
> m_imaginaryPart==0.f; } + virtual bool isZero() const { return \
> m_value==0. && m_imaginaryPart==0.; }
> virtual bool matches(const Object* exp, QMap< QString, const Object* >* found) \
> const; /*/** Sets whether it is a correct Cn.
> void setCorrect(bool b) {m_correct = b; }*/
>
> - std::complex<float> complexValue() const;
> + std::complex<double> complexValue() const;
>
> virtual Object* copy() const;
>
> @@ -167,7 +167,7 @@ class ANALITZA_EXPORT Cn : public Object
> static Cn euler();
> private:
> union { double m_value; ushort m_char; };
> - float m_imaginaryPart;
> + double m_imaginaryPart;
> enum ValueFormat m_format;
> };
>
>
>
[Attachment #3 (unknown)]
<html><head></head><body bgcolor="#FFFFFF"><div><div><div>Sorry for that, was late, \
next time I'll wait and think better about commit \
messages. </div><div><br></div><div>Details are:</div><div>- Fix print style of \
complex numbers.</div><div>- Change to double as base type for complex, rationale: \
more precision and keep consistence with semantic of Cn::value (which already has \
double as type)</div><div><br></div><div>Percy</div><div><br>On Jun 15, 2014, at 4:11 \
PM, Aleix Pol <<a href="mailto:aleixpol@kde.org">aleixpol@kde.org</a>> \
wrote:<br><br></div><div></div><blockquote type="cite"><div><div dir="ltr">La \
descripción de este commit es de lejos incompleta. De hecho lo deberías haber hecho \
por lo menos en 3 commits que cada uno cambie una cosa distinta. Aparte cambia de \
float a double el imaginaryPart sin ninguna razón aparente?<div>
<br></div><div>Aleix</div></div><div class="gmail_extra"><br><br><div \
class="gmail_quote">On Sun, Jun 15, 2014 at 5:25 AM, Percy Camilo Triveño Aucahuasi \
<span dir="ltr"><<a href="mailto:percy.camilo.ta@gmail.com" \
target="_blank">percy.camilo.ta@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc \
solid;padding-left:1ex">Git commit 33bac90b347ce5424117e09b24a6d2a4de9dcdf9 by Percy \
Camilo Triveño Aucahuasi.<br> Committed on 15/06/2014 at 03:25.<br>
Pushed by aucahuasi into branch 'imaginary'.<br>
<br>
Fix some issues for complex numbers<br>
<br>
M +31 -2 analitza/htmlexpressionwriter.cpp<br>
M +1 -0 analitza/htmlexpressionwriter.h<br>
M +7 -7 analitza/operations.cpp<br>
M +24 -2 analitza/stringexpressionwriter.cpp<br>
M +1 -0 analitza/stringexpressionwriter.h<br>
M +14 -1 analitza/tests/analitzatest.cpp<br>
M +3 -3 analitza/value.cpp<br>
M +7 -7 analitza/value.h<br>
<br>
<a href="http://commits.kde.org/analitza/33bac90b347ce5424117e09b24a6d2a4de9dcdf9" \
target="_blank">http://commits.kde.org/analitza/33bac90b347ce5424117e09b24a6d2a4de9dcdf9</a><br>
<br>
diff --git a/analitza/htmlexpressionwriter.cpp \
b/analitza/htmlexpressionwriter.cpp<br> index 87db178..ab5a21a 100644<br>
--- a/analitza/htmlexpressionwriter.cpp<br>
+++ b/analitza/htmlexpressionwriter.cpp<br>
@@ -32,6 +32,8 @@<br>
<br>
using namespace Analitza;<br>
<br>
+const double HtmlExpressionWriter::MIN_PRINTABLE_VALUE = 0.0000000000001; // since \
the precision we choose for 'G' is 12<br> +<br>
//we use the one in string*<br>
QMap<Operator::OperatorType, QString> initOperators();<br>
<br>
@@ -83,10 +85,37 @@ QVariant HtmlExpressionWriter::visit(const List* vec)<br>
<br>
QVariant HtmlExpressionWriter::visit(const Cn* var)<br>
{<br>
+ QString innerhtml;<br>
if(var->isBoolean())<br>
- return QString("<span \
class='var'>"+QString(var->isTrue() ? "true" : "false")+"</span>");<br> + \
innerhtml = var->isTrue() ? \
"true" : "false";<br> + else if(var->isCharacter())<br>
+ innerhtml = \
QString(var->character());<br> + else if(var->isComplex()) \
{<br> + QString realpart;<br>
+ QString imagpart;<br>
+ bool realiszero = false;<br>
+ if \
(qAbs(var->complexValue().real()) > MIN_PRINTABLE_VALUE)<br> + \
realpart = \
QString::number(var->complexValue().real(), 'g', 12);<br> + \
else<br> + \
realiszero = true;<br> +<br>
+ if (var->complexValue().imag() \
!= 1) {<br> + \
if (qAbs(var->complexValue().imag()) > MIN_PRINTABLE_VALUE) {<br> + \
\
if (!realiszero && \
var->complexValue().imag()>0.)<br> + \
\
realpart += QLatin1String("+");<br> + \
imagpart = \
QString::number(var->complexValue().imag(), 'g', 12);<br> + \
\
imagpart += QLatin1String("*i");<br> + \
}<br> + \
} else {<br> + \
imagpart = QLatin1String("i");<br> + \
}<br> +<br>
+ innerhtml = realpart+imagpart;<br>
+ }<br>
else<br>
- return QString("<span \
class='num'>"+QString::number(var->value(), 'g', 12)+"</span>");<br> + \
innerhtml = \
QString::number(var->value(), 'g', 12);<br> +<br>
+ return QString("<span \
class='num'>"+innerhtml+"</span>");<br> }<br>
<br>
QVariant HtmlExpressionWriter::visit(const Analitza::Ci* var)<br>
diff --git a/analitza/htmlexpressionwriter.h b/analitza/htmlexpressionwriter.h<br>
index 0b2660c..0456a21 100644<br>
--- a/analitza/htmlexpressionwriter.h<br>
+++ b/analitza/htmlexpressionwriter.h<br>
@@ -54,6 +54,7 @@ class HtmlExpressionWriter : public AbstractExpressionVisitor<br>
template <class T><br>
\
static QStringList allValues(T it, const T& itEnd, AbstractExpressionVisitor* \
writer);<br> QVariant \
m_result;<br> + static const double \
MIN_PRINTABLE_VALUE;<br> };<br>
<br>
}<br>
diff --git a/analitza/operations.cpp b/analitza/operations.cpp<br>
index cdd0027..f14e463 100644<br>
--- a/analitza/operations.cpp<br>
+++ b/analitza/operations.cpp<br>
@@ -151,19 +151,19 @@ Cn* reduceRealReal(enum Operator::OperatorType op, Cn *oper, \
double a, double b,<br> return oper;<br>
}<br>
<br>
-static bool operator<(complex<float> a, complex<float> b)<br>
+static bool operator<(complex<double> a, complex<double> b)<br>
{ return a.real() < b.real() || (a.real() == b.real() && \
a.imag()<b.imag()); }<br> <br>
-static bool operator>(complex<float> a, complex<float> b)<br>
+static bool operator>(complex<double> a, complex<double> b)<br>
{ return a.real() > b.real() || (a.real() == b.real() && \
a.imag()>b.imag()); }<br> <br>
-static bool operator<=(complex<float> a, complex<float> b)<br>
+static bool operator<=(complex<double> a, complex<double> b)<br>
{ return a.real() <= b.real() || (a.real() == b.real() && \
a.imag()<=b.imag()); }<br> <br>
-static bool operator>=(complex<float> a, complex<float> b)<br>
+static bool operator>=(complex<double> a, complex<double> b)<br>
{ return a.real() >= b.real() || (a.real() == b.real() && \
a.imag()>=b.imag()); }<br> <br>
-Cn* reduceComplexComplex(enum Operator::OperatorType op, Cn *oper, \
complex<float> a, complex<float> b, QString** correct)<br> +Cn* \
reduceComplexComplex(enum Operator::OperatorType op, Cn *oper, complex<double> \
a, complex<double> b, QString** correct)<br> {<br>
switch(op) {<br>
case Operator::plus:<br>
@@ -274,7 +274,7 @@ Cn* reduceComplexComplex(enum Operator::OperatorType op, Cn \
*oper, complex<float<br> Cn* Operations::reduceValueValue(enum \
Operator::OperatorType op, Cn *oper, const Cn *oper1, QString** correct)<br> \
{<br> if(KDE_ISUNLIKELY(oper->isComplex() || \
oper1->isComplex())) {<br>
- const complex<float> \
a=oper->complexValue(), b=oper1->complexValue();<br> + \
const complex<double> a=oper->complexValue(), \
b=oper1->complexValue();<br> \
return reduceComplexComplex(op, oper, a, b, correct);<br> \
} else {<br> const \
double a=oper->value(), b=oper1->value();<br> @@ -292,7 +292,7 @@ Cn* \
Operations::reduceUnaryValue(Operator::OperatorType op, Cn* oper, QString**<br> <br>
Cn* Operations::reduceUnaryComplex(Operator::OperatorType op, Cn* val, \
QString** correct)<br> {<br>
- const complex<float> a=val->complexValue();<br>
+ const complex<double> a=val->complexValue();<br>
<br>
switch(op) {<br>
case Operator::minus:<br>
diff --git a/analitza/stringexpressionwriter.cpp \
b/analitza/stringexpressionwriter.cpp<br> index 4f434aa..50012b3 100644<br>
--- a/analitza/stringexpressionwriter.cpp<br>
+++ b/analitza/stringexpressionwriter.cpp<br>
@@ -30,6 +30,8 @@<br>
<br>
using namespace Analitza;<br>
<br>
+const double StringExpressionWriter::MIN_PRINTABLE_VALUE = 0.0000000000001; // since \
the precision we choose for 'G' is 12<br> +<br>
template <class T><br>
QStringList StringExpressionWriter::allValues(T it, const T& itEnd, \
AbstractExpressionVisitor* writer)<br> {<br>
@@ -105,8 +107,28 @@ QVariant StringExpressionWriter::visit(const Cn* var)<br>
return var->isTrue() ? \
"true" : "false";<br> else if(var->isCharacter())<br>
return \
QString(var->character());<br>
- else if(var->isComplex())<br>
- return \
QVariant::fromValue<QString>(QString::number(var->complexValue().real(), \
'g', 12) + QLatin1String("+i*") + QString::number(var->complexValue().imag(), 'g', \
12));<br>
+ else if(var->isComplex()) {<br>
+ QString realpart;<br>
+ QString imagpart;<br>
+ bool realiszero = false;<br>
+ if \
(qAbs(var->complexValue().real()) > MIN_PRINTABLE_VALUE)<br> + \
realpart = \
QString::number(var->complexValue().real(), 'g', 12);<br> + \
else<br> + \
realiszero = true;<br> +<br>
+ if (var->complexValue().imag() \
!= 1) {<br> + \
if (qAbs(var->complexValue().imag()) > MIN_PRINTABLE_VALUE) {<br> + \
\
if (!realiszero && \
var->complexValue().imag()>0.)<br> + \
\
realpart += QLatin1String("+");<br> + \
imagpart = \
QString::number(var->complexValue().imag(), 'g', 12);<br> + \
\
imagpart += QLatin1String("*i");<br> + \
}<br> + \
} else {<br> + \
imagpart = QLatin1String("i");<br> + \
}<br> +<br>
+ return \
QVariant::fromValue<QString>(realpart+imagpart);<br> + \
}<br> else<br>
return \
QString::number(var->value(), 'g', 12);<br> }<br>
diff --git a/analitza/stringexpressionwriter.h \
b/analitza/stringexpressionwriter.h<br> index 523de30..6f9fb98 100644<br>
--- a/analitza/stringexpressionwriter.h<br>
+++ b/analitza/stringexpressionwriter.h<br>
@@ -58,6 +58,7 @@ class StringExpressionWriter : public AbstractExpressionVisitor<br>
\
static QStringList allValues(T it, const T& itEnd, AbstractExpressionVisitor* \
writer);<br> <br>
QVariant m_result;<br>
+ static const double \
MIN_PRINTABLE_VALUE;<br> };<br>
<br>
}<br>
diff --git a/analitza/tests/analitzatest.cpp b/analitza/tests/analitzatest.cpp<br>
index 47d3c84..fcec6e8 100644<br>
--- a/analitza/tests/analitzatest.cpp<br>
+++ b/analitza/tests/analitzatest.cpp<br>
@@ -115,6 +115,7 @@ void AnalitzaTest::testTrivialCalculate_data()<br>
QTest::newRow("cpx1") << "i" << Cn(0, 1);<br>
QTest::newRow("cpx2") << "i*i" << Cn(-1);<br>
QTest::newRow("cpx3") << "2+i*i" << \
Cn(1);<br> + QTest::newRow("complex number") << \
"3+4*(5-6*i)" << Cn(23, -24);<br> }<br>
<br>
void AnalitzaTest::testTrivialCalculate()<br>
@@ -142,8 +143,14 @@ void AnalitzaTest::testTrivialEvaluate_data()<br>
{<br>
QTest::addColumn<QString>("expression");<br>
QTest::addColumn<QString>("result");<br>
-<br>
+<br>
QTest::newRow("simple value") << "2" << \
"2";<br> + QTest::newRow("simple complex value") << \
"6*(2+i)" << "12+6*i";<br> + QTest::newRow("complex \
irreductibility") << "i" << "i";<br> + \
QTest::newRow("from complex value") << "i*i" << "-1";<br> + \
QTest::newRow("from power complex") << "power(i, 2)" << "-1";<br> \
+ QTest::newRow("sin complex") << "sin(i)" << \
"1.17520119364*i";<br> + QTest::newRow("cos complex") << \
"cos(5-9*i)" << "1149.26926545-3885.12187972*i";<br> \
QTest::newRow("simple addition") << "2+2" << "4";<br> \
QTest::newRow("simple addition with var") << "2+x" \
<< "x+2";<br> QTest::newRow("minus \
irreductibility") << "-x" << "-x";<br> @@ -314,6 +321,12 @@ void \
AnalitzaTest::testCorrection_data()<br> \
QTest::addColumn<QString>("result");<br> <br>
QStringList script;<br>
+<br>
+ script.clear();<br>
+ script << "f:=y->y*y";<br>
+ script << "f(i)";<br>
+ QTest::newRow("from complex function") << script \
<< "-1";<br> +<br>
script.clear();<br>
script << "n:=2";<br>
script << "n+1";<br>
diff --git a/analitza/value.cpp b/analitza/value.cpp<br>
index 8f567f1..fe0b8f3 100644<br>
--- a/analitza/value.cpp<br>
+++ b/analitza/value.cpp<br>
@@ -116,12 +116,12 @@ void Cn::setValue(bool v)<br>
m_imaginaryPart = 0;<br>
}<br>
<br>
-std::complex<float> Cn::complexValue() const<br>
+std::complex<double> Cn::complexValue() const<br>
{<br>
- return std::complex<float>(m_value, \
m_imaginaryPart);<br> + return \
std::complex<double>(m_value, m_imaginaryPart);<br> }<br>
<br>
-void Cn::setValue(std::complex<float> v)<br>
+void Cn::setValue(std::complex<double> v)<br>
{<br>
if(v.imag() == 0)<br>
setValue(v.real());<br>
diff --git a/analitza/value.h b/analitza/value.h<br>
index ab60dc0..176c825 100644<br>
--- a/analitza/value.h<br>
+++ b/analitza/value.h<br>
@@ -63,8 +63,8 @@ class ANALITZA_EXPORT Cn : public Object<br>
explicit Cn(const QChar& \
c) : Object(Object::value), m_char(c.unicode()), m_imaginaryPart(0), m_format(Char) \
{}<br> <br>
/** Constructor. Creates a \
value that represents a complex. */<br>
- explicit Cn(float value, float \
imaginaryPart) : Object(Object::value), m_value(value), \
m_imaginaryPart(imaginaryPart), m_format(Complex) {qDebug() << "xxxxxxxxxx" \
<< toString();}<br>
-<br>
+ explicit Cn(double value, double \
imaginaryPart) : Object(Object::value), m_value(value), \
m_imaginaryPart(imaginaryPart), m_format(Complex) {}<br> +<br>
/**<br>
* \
Extracts the number from the @p e Dom element and saves it.<br> \
*/<br> @@ -78,7 +78,7 @@ class \
ANALITZA_EXPORT Cn : public Object<br> \
void setValue(int v);<br> \
void setValue(uint v);<br> \
void setValue(bool v);<br>
- void \
setValue(std::complex<float> v);<br> + \
void setValue(std::complex<double> v);<br> <br>
/**<br>
* \
Returns the value.<br> @@ -149,16 +149,16 @@ class ANALITZA_EXPORT Cn : public \
Object<br> QChar character() \
const { Q_ASSERT(m_format==Char); return QChar(m_char); }<br> <br>
/** @returns whether the \
value has an imaginary part */<br>
- bool isComplex() const { return \
m_format == Complex; }<br> + bool \
isComplex() const { return m_format == Complex && m_imaginaryPart!=0.; }<br> \
<br> virtual QVariant \
accept(AbstractExpressionVisitor*) const;<br>
- virtual bool isZero() const { \
return m_value==0. && m_imaginaryPart==0.f; }<br> + \
virtual bool isZero() const { return m_value==0. \
&& m_imaginaryPart==0.; }<br> <br>
virtual bool matches(const \
Object* exp, QMap< QString, const Object* >* found) const;<br> \
/*/** Sets whether it is a correct Cn.<br> \
void setCorrect(bool b) \
{m_correct = b; }*/<br> <br>
- std::complex<float> \
complexValue() const;<br> + \
std::complex<double> complexValue() const;<br> <br>
virtual Object* copy() \
const;<br> <br>
@@ -167,7 +167,7 @@ class ANALITZA_EXPORT Cn : public Object<br>
static Cn euler();<br>
private:<br>
union { double m_value; \
ushort m_char; };<br>
- float m_imaginaryPart;<br>
+ double m_imaginaryPart;<br>
enum ValueFormat \
m_format;<br> };<br>
<br>
<br>
</blockquote></div><br></div>
</div></blockquote></div><div><span></span></div></div><div><span></span></div></body></html>
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic