print of double and polynom fixed. 40/15140/4
Cedric Delamarre [Tue, 2 Sep 2014 12:14:24 +0000 (14:14 +0200)]
Change-Id: Ifbaccd34df183a237665dfdbeaf39356e9c1d983

scilab/modules/ast/includes/types/tostring_common.hxx
scilab/modules/ast/src/cpp/types/double.cpp
scilab/modules/ast/src/cpp/types/polynom.cpp
scilab/modules/ast/src/cpp/types/singlepoly.cpp
scilab/modules/ast/src/cpp/types/tostring_common.cpp

index c44003a..410f8b9 100644 (file)
 #include "os_swprintf.h"
 #include "dynlib_ast.h"
 
-#define SIZE_BETWEEN_TWO_VALUES         2
-#define SPACE_BETWEEN_TWO_VALUES        L"  "
-#define SIZE_BETWEEN_REAL_COMPLEX       1
-#define SPACE_BETWEEN_REAL_COMPLEX      L" "
+#define SIZE_BETWEEN_TWO_VALUES         1
+#define SPACE_BETWEEN_TWO_VALUES        L" "
 #define SIZE_BOOL                       1
 #define SIZE_BETWEEN_BOOL               1
 #define SPACE_BETWEEN_BOOL              L" "
 #define SIGN_LENGTH                     2
-#define NO_SIGN                         L" "
-#define MINUS_STRING                    L"-"
-#define PLUS_STRING                     L"+"
-#define MINUS_STRING_INT                L" -"
+#define NO_SIGN                         L"  "
+#define MINUS_STRING                    L" -"
+#define PLUS_STRING                     L" +"
 #define SYMBOL_I                        L"i"
 
 
index d9de6d9..b931ba9 100644 (file)
@@ -405,17 +405,17 @@ bool Double::subMatrixToString(wostringstream& ostr, int* _piDims, int _iDims)
 
         if (isComplex() == false)
         {
+            int iLen = 0;
             for (int i = m_iCols1PrintState ; i < getCols() ; i++)
             {
-                int iLen = 0;
                 _piDims[0] = 0;
                 _piDims[1] = i;
                 int iPos = getIndex(_piDims);
 
                 DoubleFormat df;
                 getDoubleFormat(ZeroIsZero(m_pRealData[iPos]), &df);
-                iLen = df.iWidth + static_cast<int>(ostemp.str().size());
-                if (iLen > iLineLen)
+                iLen = static_cast<int>(ostemp.str().size()) + SIZE_BETWEEN_TWO_VALUES + df.iSignLen + df.iWidth;
+                if (iLen >= iLineLen - 1)
                 {
                     //Max length, new line
                     iCurrentLine += 4; //"column x to Y" + empty line + value + empty line
@@ -445,52 +445,25 @@ bool Double::subMatrixToString(wostringstream& ostr, int* _piDims, int _iDims)
         }
         else //complex case
         {
+            int iTotalLen = 0;
+            int iLen = 0;
+
             for (int i = m_iCols1PrintState ; i < getCols() ; i++)
             {
-                int iLen = 0;
                 _piDims[0] = 0;
                 _piDims[1] = i;
-                int iTotalLen = 0;
                 int iPos = getIndex(_piDims);
 
                 DoubleFormat dfR, dfI;
                 getComplexFormat(ZeroIsZero(m_pRealData[iPos]), ZeroIsZero(m_pImgData[iPos]), &iTotalLen, &dfR, &dfI);
-
-                if (isRealZero(m_pImgData[iPos]))
-                {
-                    if (isRealZero(m_pRealData[iPos]))
-                    {
-                        iLen += 1; //"0"
-                    }
-                    else
-                    {
-                        iLen        += dfR.iWidth;
-                        dfI.iWidth    = 0;
-                    }
-                }
-                else
-                {
-                    if (isRealZero(m_pRealData[iPos]))
-                    {
-                        iLen        += dfI.iWidth;
-                        dfR.iWidth    = 0;
-                    }
-                    else
-                    {
-                        iLen += dfR.iWidth;
-                        iLen += SIZE_BETWEEN_REAL_COMPLEX;
-                        iLen += dfI.iWidth;
-                    }
-                }
-
-                if (iLen > iLineLen)
+                iLen = static_cast<int>(ostemp.str().size()) + SIZE_BETWEEN_TWO_VALUES + iTotalLen;
+                if (iLen >= iLineLen - 1)
                 {
                     //Max length, new line
                     iCurrentLine += 4; //"column x to Y" + empty line + value + empty line
                     if ((iMaxLines == 0 && iCurrentLine >= MAX_LINES) || (iMaxLines != 0 && iCurrentLine >= iMaxLines))
                     {
                         m_iCols1PrintState = iLastVal;
-                        ostr << endl << endl;
                         return false;
                     }
 
@@ -501,7 +474,7 @@ bool Double::subMatrixToString(wostringstream& ostr, int* _piDims, int _iDims)
                 }
 
                 ostemp << SPACE_BETWEEN_TWO_VALUES;
-                addDoubleComplexValue(&ostemp, ZeroIsZero(m_pRealData[iPos]), ZeroIsZero(m_pImgData[iPos]), iLen, &dfR, &dfI);
+                addDoubleComplexValue(&ostemp, ZeroIsZero(m_pRealData[iPos]), ZeroIsZero(m_pImgData[iPos]), iTotalLen, &dfR, &dfI);
             }
 
             if (iLastVal != 0)
index 32d5843..ab40233 100644 (file)
@@ -743,7 +743,7 @@ wstring Polynom::getRowString(int* _piDims, int _iDims, bool _bComplex)
             get(iPos)->toStringReal(getVariableName(), &listExpR, &listCoefR);
         }
 
-        if (iLen != 0 && static_cast<int>(iLen + listExpR.front().size()) > iLineLen)
+        if (iLen != 0 && static_cast<int>(listExpR.front().size()) + iLen >= iLineLen - 1)
         {
             //flush strean
             if (i == iLastFlush + 1)
@@ -755,12 +755,12 @@ wstring Polynom::getRowString(int* _piDims, int _iDims, bool _bComplex)
                 ostr << endl << L"         Column " << iLastFlush + 1 /* 2 is better than 1, no ? */ << L" to " << i << endl << endl;
             }
 
-            iLastFlush    = i;
-            iLen                = 0;
+            iLastFlush = i;
+            iLen = 0;
             ostr << osExp.str() << endl;
             ostr << osCoef.str() << endl;
-            osExp.str(L"");
-            osCoef.str(L"");
+            osExp.str(L" ");
+            osCoef.str(L" ");
         }
 
         if (listCoefR.size() > 1)
index 8e921d6..b9abdc3 100644 (file)
@@ -348,90 +348,71 @@ void SinglePoly::toStringInternal(double *_pdblVal, wstring _szVar, list<wstring
     wostringstream ostemp;
     wostringstream ostemp2;
 
-    ostemp << L"  ";
-    ostemp2 << L"";
-
-    //to add exponant value a the good place
-    int *piIndexExp = new int[m_iSize];
+    ostemp << L" ";
+    ostemp2 << L" ";
 
     int iLen = 0;
     int iLastFlush = 2;
     for (int i = 0 ; i < m_iSize ; i++)
     {
-        piIndexExp[i] = 0;
         if (isRealZero(_pdblVal[i]) == false)
         {
             DoubleFormat df;
             getDoubleFormat(_pdblVal[i], &df);
 
-            if (iLen + df.iWidth + 2 >= iLineLen)
+            if (iLen + df.iWidth + df.iSignLen >= iLineLen - 1)
             {
-                //flush
-                for (int j = iLastFlush ; j < i ; j++)
-                {
-                    if (piIndexExp[j] == 0)
-                    {
-                        continue;
-                    }
-
-                    addSpaces(&ostemp2, piIndexExp[j] - static_cast<int>(ostemp2.str().size()));
-                    if (isRealZero(_pdblVal[j]) == false)
-                    {
-                        ostemp2 << j;
-                    }
-                }
                 iLastFlush = i;
                 _pListExp->push_back(ostemp2.str());
                 ostemp2.str(L""); //reset stream
-                addSpaces(&ostemp2, 12); //take from scilab ... why not ...
+                addSpaces(&ostemp2, 11); //take from scilab ... why not ...
 
                 _pListCoef->push_back(ostemp.str());
                 ostemp.str(L""); //reset stream
-                addSpaces(&ostemp, 12); //take from scilab ... why not ...
+                addSpaces(&ostemp, 11); //take from scilab ... why not ...
             }
 
-            bool bFirst = ostemp.str().size() == 2;
+            bool bFirst = ostemp.str().size() == 1;
 
             // In scientific notation case bExp == true, so we have to print point (2.000D+10s)
             // In other case don't print point (2s)
             df.bPrintPoint = df.bExp;
-            df.bPrintPlusSign = ostemp.str().size() != 2;
+            df.bPrintPlusSign = bFirst == false;
             df.bPrintOne = i == 0;
             addDoubleValue(&ostemp, _pdblVal[i], &df);
 
-            if (i != 0)
+            if (i == 0)
             {
-                ostemp << _szVar;
-                piIndexExp[i] = static_cast<int>(ostemp.str().size());
+                iLen = static_cast<int>(ostemp.str().size());
             }
-            ostemp << L" ";
-            iLen = static_cast<int>(ostemp.str().size());
-        }
-    }
-
-    if (iLastFlush != 0)
-    {
-        for (int j = iLastFlush ; j < m_iSize ; j++)
-        {
-            if (piIndexExp[j] == 0)
+            else if (i == 1)
             {
-                continue;
+                // add polynom name
+                ostemp << _szVar;
+                iLen = static_cast<int>(ostemp.str().size());
             }
-
-            addSpaces(&ostemp2, piIndexExp[j] - static_cast<int>(ostemp2.str().size()));
-            if (isRealZero(_pdblVal[j]) == false)
+            else
             {
-                ostemp2 << j;
+                // add polynom name and exponent
+                ostemp << _szVar;
+                iLen = static_cast<int>(ostemp.str().size());
+                addSpaces(&ostemp2, iLen - static_cast<int>(ostemp2.str().size()));
+                ostemp2 << i;
+                int iSize = static_cast<int>(ostemp2.str().size()) - iLen;
+                addSpaces(&ostemp, iSize);
             }
         }
+    }
 
-        if (ostemp.str() == L"  ")
+    if (iLastFlush != 0)
+    {
+        if (ostemp.str() == L" ")
         {
             ostemp << L"  0";
             addSpaces(&ostemp2, static_cast<int>(ostemp.str().size()));
         }
 
-        if (ostemp2.str() == L"")
+        if (ostemp2.str() == L" ")
         {
             addSpaces(&ostemp2, static_cast<int>(ostemp.str().size()));
         }
@@ -440,7 +421,6 @@ void SinglePoly::toStringInternal(double *_pdblVal, wstring _szVar, list<wstring
         _pListCoef->push_back(ostemp.str());
     }
 
-    delete[] piIndexExp;
     return;
 }
 
index d5dacd7..8a6101c 100644 (file)
@@ -175,29 +175,36 @@ void getComplexFormat(double _dblR, double _dblI, int *_piTotalWidth, DoubleForm
     getDoubleFormat(_dblI, _pDFI);
 
     *_piTotalWidth = 0;
-    if (_dblI == 0)
+    if (isRealZero(_dblI))
     {
-        if (_dblR == 0)
+        if (isRealZero(_dblR))
         {
             *_piTotalWidth += 1;    //"0"
+            _pDFI->iWidth = 0;
+            _pDFI->iSignLen = 0;
         }
         else
         {
-            *_piTotalWidth += _pDFR->iWidth;
+            *_piTotalWidth += _pDFR->iWidth + _pDFR->iSignLen;
             _pDFI->iWidth = 0;
+            _pDFI->iSignLen = 0;
         }
     }
     else
     {
-        if (_dblR == 0)
+        if (isRealZero(_dblR))
         {
-            *_piTotalWidth += _pDFI->iWidth;
+            *_piTotalWidth += _pDFI->iWidth + _pDFI->iSignLen;
             _pDFR->iWidth = 0;
         }
         else
         {
-            *_piTotalWidth += _pDFR->iWidth + SIGN_LENGTH + _pDFI->iWidth;
+            *_piTotalWidth += _pDFR->iWidth + _pDFR->iSignLen + _pDFI->iWidth + _pDFI->iSignLen;
         }
+
+        // i character
+        _pDFI->iWidth += 1;
+        *_piTotalWidth += 1;
     }
 }
 
@@ -234,12 +241,12 @@ void addDoubleValue(std::wostringstream * _postr, double _dblVal, DoubleFormat *
         if (ISNAN(_dblVal))
         {
             //NaN
-            os_swprintf(pwstOutput, 32, L"%ls%*ls", pwstSign, _pDF->iPrec, L"Nan");
+            os_swprintf(pwstOutput, 32, L"%ls %*ls", pwstSign, _pDF->iPrec, L"Nan");
         }
         else if (!finite(_dblVal))
         {
             //Inf
-            os_swprintf(pwstOutput, 32, L"%ls%*ls", pwstSign, _pDF->iPrec, L"Inf");
+            os_swprintf(pwstOutput, 32, L"%ls %*ls", pwstSign, _pDF->iPrec, L"Inf");
         }
         else if (_pDF->bExp)
         {
@@ -262,11 +269,11 @@ void addDoubleValue(std::wostringstream * _postr, double _dblVal, DoubleFormat *
 
             if (_pDF->bPrintPoint)
             {
-                os_swprintf(pwstFormat, 32, L"%ls%%#.0f%%0%d.0fD%%+d", pwstSign, _pDF->iPrec);
+                os_swprintf(pwstFormat, 32, L"%ls %%#.0f%%0%d.0fD%%+d", pwstSign, _pDF->iPrec);
             }
             else
             {
-                os_swprintf(pwstFormat, 32, L"%ls%%.0f%%0%d.0fD%%+d", pwstSign, _pDF->iPrec);
+                os_swprintf(pwstFormat, 32, L"%ls %%.0f%%0%d.0fD%%+d", pwstSign, _pDF->iPrec);
             }
 
             os_swprintf(pwstOutput, 32, pwstFormat, dblEnt, dblDec, (int)dblTemp);
@@ -275,11 +282,11 @@ void addDoubleValue(std::wostringstream * _postr, double _dblVal, DoubleFormat *
         {
             if (_pDF->bPrintPoint)
             {
-                os_swprintf(pwstFormat, 32, L"%ls%%#-%d.%df", pwstSign, _pDF->iWidth - 1, _pDF->iPrec);
+                os_swprintf(pwstFormat, 32, L"%ls %%#-%d.%df", pwstSign, _pDF->iWidth - 1, _pDF->iPrec);
             }
             else
             {
-                os_swprintf(pwstFormat, 32, L"%ls%%-%d.%df", pwstSign, _pDF->iWidth - 2, _pDF->iPrec);  //-2 no point needed
+                os_swprintf(pwstFormat, 32, L"%ls %%-%d.%df", pwstSign, _pDF->iWidth - 2, _pDF->iPrec);  //-2 no point needed
             }
 
             os_swprintf(pwstOutput, 32, pwstFormat, fabs(_dblVal));
@@ -289,7 +296,7 @@ void addDoubleValue(std::wostringstream * _postr, double _dblVal, DoubleFormat *
     }
     else if (wcslen(pwstSign) != 0)
     {
-        os_swprintf(pwstOutput, 32, L"%ls", pwstSign);
+        os_swprintf(pwstOutput, 32, L"%ls ", pwstSign);
         *_postr << pwstOutput;
     }
 }
@@ -377,7 +384,6 @@ void addDoubleComplexValue(wostringstream * _postr, double _dblR, double _dblI,
             df.bExp = _pDFR->bExp;
 
             addDoubleValue(&ostemp, _dblR, &df);
-            ostemp << SPACE_BETWEEN_REAL_COMPLEX;
 
             //I
             df.iPrec = _pDFI->iPrec;