View previous topic :: View next topic |
Author |
Message |
ilker07
Joined: 03 Jun 2022 Posts: 32
|
CCS C double variable |
Posted: Wed Mar 20, 2024 1:30 am |
|
|
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: 478 Location: Montenegro
|
|
Posted: Wed Mar 20, 2024 2:47 am |
|
|
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: 32
|
|
Posted: Wed Mar 20, 2024 2:55 am |
|
|
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: 478 Location: Montenegro
|
|
Posted: Wed Mar 20, 2024 3:05 am |
|
|
data = 0.2*data;
This cuts time down a lot. |
|
|
ilker07
Joined: 03 Jun 2022 Posts: 32
|
|
Posted: Wed Mar 20, 2024 3:20 am |
|
|
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: 478 Location: Montenegro
|
|
Posted: Wed Mar 20, 2024 3:29 am |
|
|
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: 32
|
|
Posted: Wed Mar 20, 2024 3:50 am |
|
|
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: 19495
|
|
Posted: Wed Mar 20, 2024 4:26 am |
|
|
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: 32
|
|
Posted: Wed Mar 20, 2024 4:34 am |
|
|
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: 19495
|
|
Posted: Wed Mar 20, 2024 4:37 am |
|
|
Unless you are using PCD, you are not using a 'double'.
What PIC are you using??? |
|
|
ilker07
Joined: 03 Jun 2022 Posts: 32
|
|
Posted: Wed Mar 20, 2024 4:45 am |
|
|
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: 19495
|
|
Posted: Wed Mar 20, 2024 5:02 am |
|
|
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: 32
|
|
Posted: Wed Mar 20, 2024 5:25 am |
|
|
[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: 32
|
|
Posted: Wed Mar 20, 2024 5:36 am |
|
|
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: 478 Location: Montenegro
|
|
Posted: Wed Mar 20, 2024 5:46 am |
|
|
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 ) |
|
|
|