|
|
View previous topic :: View next topic |
Author |
Message |
ccsfred
Joined: 15 Jun 2016 Posts: 33
|
Reciprocal help required before computer goes out the window |
Posted: Thu Apr 20, 2017 9:01 am |
|
|
Hi,
I'm doing something stupid, but can't see it, all I'm trying to do is get the period of a frequency, i.e. recipricol, but it's not working, I've stripped the code down to a min, but still not working.
Code: |
#include <33FJ256MC710A.h>
#include <CD91492.h>
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#fuses NOWDT, NOPROTECT
float delay=0;
float recipricol(float value)
{
float output = 0;
fprintf(USB,"Value:%f\n\r", value);
output = 1/value;
fprintf(USB,"Output:%f\n\r", output);
return output;
}
void delayCalc()
{
delay = recipricol(100);
fprintf(USB,"Delay:%d\n\r", delay);
}
void main()
{
delay_ms(1000);
setup_uart(921600, USB);
delayCalc();
}
|
the output I get in serial terminal is:
Value:100.00
Output:0.00
Delay:1008981770
Help please?
Thanks! |
|
|
newguy
Joined: 24 Jun 2004 Posts: 1907
|
|
Posted: Thu Apr 20, 2017 9:30 am |
|
|
This:
Code: | fprintf(USB,"Delay:%d\n\r", delay); |
You're telling it to format the number as an unsigned int. Use %f. One of those mistakes that takes a second pair of eyes to see. Hope your computer hasn't been defenestrated yet. |
|
|
ccsfred
Joined: 15 Jun 2016 Posts: 33
|
|
Posted: Thu Apr 20, 2017 9:44 am |
|
|
Hi Newguy,
PC is still ok, I've moved onto another bit of code now as I can't see the wood for the trees.
Thanks for the reply, well spotted, however in the:
Code: |
fprintf(USB,"Output:%f\n\r", output);
|
part this output's 0.00.
I've corrected the %d in delayCalc(), now it outputs:
Value:100.00
Output:0.00
Delay:0.00
Thanks |
|
|
ccsfred
Joined: 15 Jun 2016 Posts: 33
|
|
Posted: Thu Apr 20, 2017 9:54 am |
|
|
Compiler version: 5.067 |
|
|
ccsfred
Joined: 15 Jun 2016 Posts: 33
|
|
Posted: Thu Apr 20, 2017 10:06 am |
|
|
I'm out of office until Tuesday, just so you don't think I'm rude note replying.. |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Thu Apr 20, 2017 10:28 am |
|
|
Code: |
fprintf(USB,"Output: %1.4f \n\r", output);
or just %g
|
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19498
|
|
Posted: Thu Apr 20, 2017 10:47 am |
|
|
As some general comments:
Fuses should be before the code includes.
Sequence should always be:
Processor include
fuses
delay statement
#pin selects if needed
UART & other setups (#use rs232 etc.)
Code includes
main code
This can be modified by putting sections into their own includes, but the compiler must hit these in this order, otherwise things may not execute as expected...
Generally, be very careful using reserved words as variable names. 'output' and 'delay' are both reserved words. |
|
|
ccsfred
Joined: 15 Jun 2016 Posts: 33
|
|
Posted: Tue Apr 25, 2017 1:10 am |
|
|
Thanks asmboy, changing the formatting with %1.4f then displayed the number, %g didn't work just displayed 0.00, however the output reads 0.0099, not 0.01, why would this be please?
Thanks Ttelmah too, I've corrected the sequence and good point about the reserved words, it wasn't the case this time, but definitely worth remembering not to use these in the future!
Code: |
#include <33FJ256MC710A.h>
#fuses NOWDT, NOPROTECT
#device ICD=TRUE
#device ICSP=1
#use delay(clock=80MHz,crystal=20MHz)
#use rs232(baud=921600, parity=N, bits=8, UART1, STREAM=USB, ERRORS, NOINIT)
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
float dly=0;
float recipricol(float value)
{
float op = 0;
fprintf(USB,"Value:%f \n\r", value);
op = 1/value;
fprintf(USB,"Output:%1.4g \n\r", op);
return op;
}
void delayCalc()
{
dly = recipricol(100);
fprintf(USB,"Delay:%1.4g \n\r", dly);
}
void main()
{
delay_ms(1000);
setup_uart(921600, USB);
delayCalc();
}
|
|
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Tue Apr 25, 2017 6:46 am |
|
|
.099 - the limit of the short -float format CCS uses . |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19498
|
|
Posted: Tue Apr 25, 2017 11:30 am |
|
|
Just to elucidate slightly more.
This is a limitation of _all_ standard floating point representations. Not just CCS....
Have a look at:
<http://www.exploringbinary.com/why-0-point-1-does-not-exist-in-floating-point/>
It's just like 1/3rd in decimal. 0.333333333333333...... You can never represent it exactly.
0.1, is one of those numbers needs an infinite number of binary 'digits' to represent in float form.
Now as the thread I point to shows, in a higher float precision, you can get a value that will show 0.1000.. if printed to only a few digits. In fact if you add 0.00005 to your value (assuming you are only going to print it as x.xxxx), you will 'see' 0.1000', but it is never actually going to be 0.1.... |
|
|
|
|
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
|