9 Replies Latest reply on May 6, 2014 5:17 PM by userc_41073

    math operation problem

       Hello I made a code that use addition, subtraction and multiplication Here is the code




      PART 1:


      if (((atoi(anio)-1) % 4) == 0){


      unix_Time =(atoi(anio)+30)*365+1+11;






      unix_Time =((atoi(anio)+30)*365)+11;

      PART 2: 


      case 4:


      unix_Time +=120;






      PART 3:


      unix_Time += atoi(dia)-1;




      unix_Time is a  long variable to save te sum of the operation but, the operation  in part 3 the result is wrong because  don´t make the rest of one. I have tried using the next code:

      unixunix_Time += atoi(dia);


      unix_Time --;




      but it does not work, I have the same ressult. sombody help me ?


        • 1. Re: math operation problem

          "unix_Time is a  long variable to save te sum of the operation but, the operation  in part 3 the result is wrong because  don´t make the rest of one."




          I do not understand " because  don´t make the rest of one" ?




          Also not seeing the variable definitions, and a description of what the code is supposed to do


          difficult to help you.




          Regards, Dana.

          • 2. Re: math operation problem

             Im sorry for the bad explanation, Im trying to do a function that convert the time an date in unix time, first I convert the years,  months in days and then convert the days to second and then convert hour and minute in second the sum of all days in seconds and hour, minute in seconds give me the unix time. the hour, year, month are ASCII so I convert in integer using atoi, when I made the operation unix_Time +=atoi(dia)-1 the resul is wrong for example


            unix_Time = 16190
            dia = '5'  in ASCII 


            unix_Time += atoi(dia) - 1;


                        16190  +  5    -  1  = 16194   <------- this is the correct anwers

            I get 16195 as if he had not made the subtraction by least 1 then I multiply this value by 86400 but the result also is wrong 

            • 3. Re: math operation problem
                      I attached my code   
              • 4. Re: math operation problem

                Your error is not in the math, althoug I found an easier algorithm in the german-language wikipedia here: de.wikipedia.org/wiki/Unixzeit


                The function ultoa() expects at its first parameter a pointer to an array of characters where the resulting string has to be stored. You provide a pointer variable, but it is not initialized to point to a valid memory area. Better is to define your var as


                char unixTime[11]; // Allocates space for a string with 10 chars and a null at the end


                ultoa(unixTime,unix_Time,10); // conversion





                • 5. Re: math operation problem

                  Some additional comments -




                  1) You do not use logical pin names for the pins. You can type them/


                  rename them in pin editor. Makes it easier to understand functionality


                  of the pin. And in pin view understand whats going on.




                  2) For each module you have a number of parameters not set in its respective


                  global properties window view. Thats not good practice, could lead to unexpected behaviour


                  of module, especially if you do not set these with API code.




                  Regards, Dana.

                  • 6. Re: math operation problem

                     Thanks bob I use unixTime[11], but I have wrong result yet when I did math operation in this form: 

                    unix_Time +=atoi(hr)*3600; 




                    I could fix the problem, firts save the convertion in a variable and then multiplication:

                    vTemp= atoi(hr);


                    vTemp= vTemp *3600;


                    unix_Time += vTemp;




                    I have another question I read that the result of ftoa have a buffer of 15 bytes how can I do to only display for example 3 digits after of point (12.135 for example).


                    • 7. Re: math operation problem

                      Switch from atoi() / atof() etc to sprintf(). This gives you full control over formatting the output and leaves the result in a buffer you have to supply. Read this knowledgebase article www.cypress.com/  to see how to switch the on the needed libraries and check out the formatting strings at publications.gbdirect.co.uk/c_book/chapter9/formatted_io.html





                      • 8. Re: math operation problem

                        You might want to look at this -










                        Also you might want to file a CASE to see if the above still necessary in Designer


                        5.4 and that its complete in its functionality -








                        To create a technical case at Cypress -








                        “Technical Support”


                        “Create a Case”




                        You have to be registered on Cypress web site first.




                        Regards, Dana.

                        • 9. Re: math operation problem

                           Thanks bob and dana I will read it and thanks for your help.