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

CCS C double variable
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
ilker07



Joined: 03 Jun 2022
Posts: 18

View user's profile Send private message

CCS C double variable
PostPosted: Wed Mar 20, 2024 1:30 am     Reply with quote

Hello friends, in CCS C double data=data/5.0; I saw that this process takes approximately 65 us. I use the system at 64 mhz with PLL. This 65 us is too much for the work I do. How can I reduce this time?
PrinceNai



Joined: 31 Oct 2016
Posts: 456
Location: Montenegro

View user's profile Send private message

PostPosted: Wed Mar 20, 2024 2:47 am     Reply with quote

It might help if you tell what you are doing. Maybe it can be done with integers. Integer division by 5 takes about 25us.
ilker07



Joined: 03 Jun 2022
Posts: 18

View user's profile Send private message

PostPosted: Wed Mar 20, 2024 2:55 am     Reply with quote

PrinceNai wrote:
It might help if you tell what you are doing. Maybe it can be done with integers. Integer division by 5 takes about 25us.


Unfortunately,it has to be double operation.
PrinceNai



Joined: 31 Oct 2016
Posts: 456
Location: Montenegro

View user's profile Send private message

PostPosted: Wed Mar 20, 2024 3:05 am     Reply with quote

data = 0.2*data;

This cuts time down a lot.
ilker07



Joined: 03 Jun 2022
Posts: 18

View user's profile Send private message

PostPosted: Wed Mar 20, 2024 3:20 am     Reply with quote

PrinceNai wrote:
data = 0.2*data;

This cuts time down a lot.


Yes,for value 5 you might be right.But divisor variable is not constant.In some cases variable is 5 in some cases variable is 10.What can I do for these situations?
PrinceNai



Joined: 31 Oct 2016
Posts: 456
Location: Montenegro

View user's profile Send private message

PostPosted: Wed Mar 20, 2024 3:29 am     Reply with quote

If you only have those two divisors, use an if - else to check which of the two it is and then multiply by the correct reciprocal value.
ilker07



Joined: 03 Jun 2022
Posts: 18

View user's profile Send private message

PostPosted: Wed Mar 20, 2024 3:50 am     Reply with quote

PrinceNai wrote:
If you only have those two divisors, use an if - else to check which of the two it is and then multiply by the correct reciprocal value.


No.I have other divisors which are unknown values
Ttelmah



Joined: 11 Mar 2010
Posts: 19238

View user's profile Send private message

PostPosted: Wed Mar 20, 2024 4:26 am     Reply with quote

Division is always the slowest basic arithmetic operation.
As has already been pointed out multiplication by the reciprocal is faster.

Some further comments:

First what makes you think you must use 'double'???
Do you understand just how unnecessary in general this is?.

Comment, unless you are Using a 16bit PIC, it will not actually support
a 'double'. The standard PIC maths library does not have a double
type. It'll 'accept' it, but does not have the functions to support it.
So your code is probably just using float.
No physical process actually 'needs' this. Using it is pretty much always
a sign that you do not understand the costs of this extra 'accuracy',
and how pointless this is. For 99.999% of applications this is a waste. No
measurement you can make actually 'needs' this accuracy. Read up
about precision and accuracy, and understand that this is a sign you
probably do not understand what you are really dealing with. You can
get a better result, and save a lot of processing time by using a scaled
integer representation.
Arithmetic is slow. Basic fact of life.

You are not using a double. Simple fact if you are using a 64MHz processor
you are certainly using a PIC18. This does not support 'double'. So you
are using a 32bit float. Even this is taking 75uSec for this division. Using
a PIC24, the division will go down to about 40uSec.
ilker07



Joined: 03 Jun 2022
Posts: 18

View user's profile Send private message

PostPosted: Wed Mar 20, 2024 4:34 am     Reply with quote

Ttelmah wrote:
Division is always the slowest basic arithmetic operation.
As has already been pointed out multiplication by the reciprocal is faster.

Some further comments:

First what makes you think you must use 'double'???
Do you understand just how unnecessary in general this is?.

Comment, unless you are Using a 16bit PIC, it will not actually support
a 'double'. The standard PIC maths library does not have a double
type. It'll 'accept' it, but does not have the functions to support it.
So your code is probably just using float.
No physical process actually 'needs' this. Using it is pretty much always
a sign that you do not understand the costs of this extra 'accuracy',
and how pointless this is. For 99.999% of applications this is a waste. No
measurement you can make actually 'needs' this accuracy. Read up
about precision and accuracy, and understand that this is a sign you
probably do not understand what you are really dealing with. You can
get a better result, and save a lot of processing time by using a scaled
integer representation.
Arithmetic is slow. Basic fact of life.

You are not using a double. Simple fact if you are using a 64MHz processor
you are certainly using a PIC18. This does not support 'double'. So you
are using a 32bit float. Even this is taking 75uSec for this division. Using
a PIC24, the division will go down to about 40uSec.


Float data type was causing errors when dividing/multiplication large values.This is why I use double
Ttelmah



Joined: 11 Mar 2010
Posts: 19238

View user's profile Send private message

PostPosted: Wed Mar 20, 2024 4:37 am     Reply with quote

Unless you are using PCD, you are not using a 'double'.
What PIC are you using???
ilker07



Joined: 03 Jun 2022
Posts: 18

View user's profile Send private message

PostPosted: Wed Mar 20, 2024 4:45 am     Reply with quote

Ttelmah wrote:
Unless you are using PCD, you are not using a 'double'.
What PIC are you using???



PIC18F67K22
Ttelmah



Joined: 11 Mar 2010
Posts: 19238

View user's profile Send private message

PostPosted: Wed Mar 20, 2024 5:02 am     Reply with quote

So, you are not using a double.

Read the manual:

"double - A reserved word but is not a supported data type."

Your chip _does not_ support a 'double'.

You must have changed something about how you were using the value
when you switched to using double instead of float, which made it work.

Can't you simply reciprocate your division factor when you read it in, and
then the divisions become multiplications throughout. Multiplication is
about twice as fast as division.
ilker07



Joined: 03 Jun 2022
Posts: 18

View user's profile Send private message

PostPosted: Wed Mar 20, 2024 5:25 am     Reply with quote

[quote="Ttelmah"]So, you are not using a double.

Read the manual:

"double - A reserved word but is not a supported data type."

Your chip _does not_ support a 'double'.

You must have changed something about how you were using the value
when you switched to using double instead of float, which made it work.



I didn't do any change.I just wrote double.

I just wrote simple code

unsigned int16 i=0;
for(i=0;i<30000;i++)
{
total += i;
}

when I specified total variable as float ,it gave me wrong value but when I specified as double , it gave me right value.
ilker07



Joined: 03 Jun 2022
Posts: 18

View user's profile Send private message

PostPosted: Wed Mar 20, 2024 5:36 am     Reply with quote

Ttelmah wrote:
So, you are not using a double.

Read the manual:

"double - A reserved word but is not a supported data type."

Your chip _does not_ support a 'double'.

You must have changed something about how you were using the value
when you switched to using double instead of float, which made it work.

Can't you simply reciprocate your division factor when you read it in, and
then the divisions become multiplications throughout. Multiplication is
about twice as fast as division.





I didn't do any change.I just wrote double.

I just wrote simple code

unsigned int16 i=0;
for(i=0;i<30000;i++)
{
total += i;
}

when I specified total variable as float ,it gave me wrong value but when I specified as double , it gave me right value.
PrinceNai



Joined: 31 Oct 2016
Posts: 456
Location: Montenegro

View user's profile Send private message

PostPosted: Wed Mar 20, 2024 5:46 am     Reply with quote

My version of compiler swallows double and it treats it as a float. The result of the calculation is the same in both cases, almost 450 millions. I checked the result here:

https://miniwebtool.com/sum-of-positive-integers-calculator/?n1=1&n2=29999

and it is correct for the code sample provided. It adds all the consecutive numbers from 1 to 29.999 (mind the meaning of the dot here Very Happy )
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
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