CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

calculation

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
aaronik19



Joined: 25 Apr 2011
Posts: 296

View user's profile Send private message

calculation
PostPosted: Wed Apr 01, 2020 6:42 am     Reply with quote

Dear All,

I have a small issue. This is part of the code:
Code:

unsigned int Alarm_1[9] = {15,35,23,23,12,00,9,00,20};  //stores 5 Alarms in steps of 3
unsigned int32 total_seconds;
unsigned int32 AlarmSecondsSort[3];

unsigned int32 timetodec(int Al_hour, int Al_minut, int Al_sec, int temp_array_pos)
{
   unsigned int32 totaldecimal = 0;
   totaldecimal = ((Al_hour*3600) + (Al_minut*60) + Al_sec);
   AlarmSecondsSort[temp_array_pos] = totaldecimal;
   return(0);
}

The Al_hour is 15, Al_minut = 35 and Al_sec = 23. For some reason the totaldecimal is coming 54075 instead of 56123. What could be the problem?
temtronic



Joined: 01 Jul 2010
Posts: 9097
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed Apr 01, 2020 7:26 am     Reply with quote

hmm.. the difference is a 'magical' 2048......
recode and display the calculate intermediate terms..
al_hour*3600, alminut*60

to see if they are incorrect.

hmm... another idea while I'm typing...

this...
Quote:
unsigned int32 timetodec(int Al_hour, int Al_minut, int Al_sec, int temp_array_pos)
{

says to use Al-hour as an integer NOT an unsigned int. That may affect how the compiler 'sees' the numbers....
Ttelmah



Joined: 11 Mar 2010
Posts: 19215

View user's profile Send private message

PostPosted: Wed Apr 01, 2020 7:58 am     Reply with quote

Change this:

(Al_minut*60)

To:

(Al_minut*60L)

Otherwise this multiplication will be performed using int arithmetic (255
max), so will overflow almost every time....
aaronik19



Joined: 25 Apr 2011
Posts: 296

View user's profile Send private message

PostPosted: Wed Apr 01, 2020 7:59 am     Reply with quote

ok...I found the mistake.. I need to declare the Al_hour and Al_minut as int32. now it worked good.
Ttelmah



Joined: 11 Mar 2010
Posts: 19215

View user's profile Send private message

PostPosted: Wed Apr 01, 2020 8:03 am     Reply with quote

No you don't.
Means wasted space for these values.
Code the line as:

totaldecimal = ((Al_hour*3600LL) + (Al_minut*60L) + Al_sec);

This forces the constants in the multiplications to be larger (int16 for the
minute multiplication - which is large enough to hold any value here), and
int32 for the hour multiplication.. Result you can keep the variables the
smaller size, but the compiler 'knows' to use the larger sizes for the
maths.
aaronik19



Joined: 25 Apr 2011
Posts: 296

View user's profile Send private message

PostPosted: Wed Apr 01, 2020 12:22 pm     Reply with quote

Yes it worked perfectly as well. Structly speak this was new for me. Why one is LL and the other one is L only?
Ttelmah



Joined: 11 Mar 2010
Posts: 19215

View user's profile Send private message

PostPosted: Wed Apr 01, 2020 12:38 pm     Reply with quote

L makes it a 'long' int16. So only int16 arithmetic is used for thsi part.
This is quicker and smaller than int32, and minutes*60 is always small
enough to fit into an int16.
LL makes it a 'long long' int32. Houir*3600 can be large enough to exceed
an int16. 23*3600 = 86800. So here the int32 arithmetic must be used.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group