Merge remote-tracking branch 'origin/master' into windows
[scilab.git] / scilab / modules / elementary_functions / sci_gateway / cpp / sci_round.cpp
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2014 - Scilab Enterprises - Sylvain Genin
4  *
5  * Copyright (C) 2012 - 2016 - Scilab Enterprises
6  *
7  * This file is hereby licensed under the terms of the GNU GPL v2.0,
8  * pursuant to article 5.3.4 of the CeCILL v.2.1.
9  * This file was originally licensed under the terms of the CeCILL v2.1,
10  * and continues to be available under such terms.
11  * For more information, see the COPYING file which you should have received
12  * along with this program.
13  *
14  */
15 /*--------------------------------------------------------------------------*/
16 #include "elem_func_gw.hxx"
17 #include "function.hxx"
18 #include "double.hxx"
19 #include "polynom.hxx"
20 #include "overload.hxx"
21
22 extern "C"
23 {
24 #include "Scierror.h"
25     extern double C2F(danints)(double*);
26 }
27 /*--------------------------------------------------------------------------*/
28 types::Function::ReturnValue sci_round(types::typed_list &in, int _iRetCount, types::typed_list &out)
29 {
30     if (in.size() != 1)
31     {
32         Scierror(77, _("%s: Wrong number of input argument(s): At least %d expected.\n"), "round", 1);
33         return types::Function::Error;
34     }
35
36     if (_iRetCount > 1)
37     {
38         Scierror(78, _("%s: Wrong number of output argument(s): %d expected.\n"), "round", 1);
39         return types::Function::Error;
40     }
41
42     if (in[0]->isPoly())
43     {
44         /***** get data *****/
45         types::Polynom* pPolyIn = in[0]->getAs<types::Polynom>();
46         int* piRankPolyIn = new int[pPolyIn->getSize()];
47         pPolyIn->getRank(piRankPolyIn);
48
49         types::Polynom* pPolyOut = new types::Polynom(pPolyIn->getVariableName(), pPolyIn->getDims(), pPolyIn->getDimsArray(), piRankPolyIn);
50         pPolyOut->setComplex(pPolyIn->isComplex());
51
52         /***** perform operation *****/
53         for (int compterElem = 0; compterElem < pPolyIn->getSize(); compterElem++)
54         {
55             types::SinglePoly* pSPIn  = pPolyIn->get(compterElem);
56             types::SinglePoly* pSPOut = pPolyOut->get(compterElem);
57
58             double* pRealIn  = pSPIn->get();
59             double* pRealOut = pSPOut->get();
60
61             if (pPolyIn->isComplex())
62             {
63                 double* pImgIn  = pSPIn->getImg();
64                 double* pImgOut = pSPOut->getImg();
65                 for (int i = 0; i < piRankPolyIn[compterElem] + 1; i++)
66                 {
67                     pRealOut[i] = C2F(danints)(pRealIn + i);
68                     pImgOut[i]  = C2F(danints)(pImgIn + i);
69                 }
70             }
71             else
72             {
73                 for (int i = 0; i < piRankPolyIn[compterElem] + 1; i++)
74                 {
75                     pRealOut[i] = C2F(danints)(pRealIn + i);
76                 }
77             }
78         }
79
80         delete[] piRankPolyIn;
81
82         /***** return data *****/
83         out.push_back(pPolyOut);
84         return types::Function::OK;
85     }
86     else if (in[0]->isDouble())
87     {
88         /***** get data *****/
89         types::Double* pDblIn  = in[0]->getAs<types::Double>();// double
90         types::Double* pDblOut = new types::Double(pDblIn->getDims(), pDblIn->getDimsArray(), pDblIn->isComplex());
91
92         int size = pDblIn->getSize();
93         /***** perform operation *****/
94         double* pdblInReal  = pDblIn->get();
95         double* pDblOutReal = pDblOut->get();
96         if (pDblIn->isComplex())
97         {
98             double* pdblInImg  = pDblIn->getImg();
99             double* pDblOutImg = pDblOut->getImg();
100             for (int i = 0; i < size; i++)
101             {
102                 pDblOutReal[i] = C2F(danints)(pdblInReal + i);
103                 pDblOutImg[i]  = C2F(danints)(pdblInImg + i);
104             }
105         }
106         else
107         {
108             for (int i = 0; i < size; i++)
109             {
110                 pDblOutReal[i] = C2F(danints)(pdblInReal + i);
111             }
112         }
113
114         /***** return data *****/
115         out.push_back(pDblOut);
116         return types::Function::OK;
117     }
118     else
119     {
120         std::string stFuncName = "%" + in[0]->getShortTypeStr() + "_round";
121         return Overload::call(stFuncName, in, _iRetCount, out);
122     }
123 }
124 /*--------------------------------------------------------------------------*/