math operation problem

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
Anonymous
Not applicable

 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;

   

}

   

else 

   

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

PART 2: 
 

   

case 4:

   

unix_Time +=120;

   

break;

   

 

   

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 ?


 

0 Likes
9 Replies
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

"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.

0 Likes
Anonymous
Not applicable

 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 

0 Likes
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable
        I attached my code   
0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

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

   

 

   

Bob

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

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.

0 Likes
Anonymous
Not applicable

 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).

Regards

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

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

   

 

   

Bob

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

You might want to look at this -

   

 

   

    

   

          http://www.cypress.com/?app=forum&id=1573&rID=43329

   

 

   

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 -

   

 

   

www.cypress.com

   

“Support”

   

“Technical Support”

   

“Create a Case”

   

 

   

You have to be registered on Cypress web site first.

   

 

   

Regards, Dana.

0 Likes
Anonymous
Not applicable

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

0 Likes