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

Reciprocal help required before computer goes out the window

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



Joined: 15 Jun 2016
Posts: 33

View user's profile Send private message

Reciprocal help required before computer goes out the window
PostPosted: Thu Apr 20, 2017 9:01 am     Reply with quote

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: 1899

View user's profile Send private message

PostPosted: Thu Apr 20, 2017 9:30 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Apr 20, 2017 9:44 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Apr 20, 2017 9:54 am     Reply with quote

Compiler version: 5.067
ccsfred



Joined: 15 Jun 2016
Posts: 33

View user's profile Send private message

PostPosted: Thu Apr 20, 2017 10:06 am     Reply with quote

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

View user's profile Send private message AIM Address

PostPosted: Thu Apr 20, 2017 10:28 am     Reply with quote

Code:

fprintf(USB,"Output: %1.4f \n\r", output);

or just %g 

Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Thu Apr 20, 2017 10:47 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Apr 25, 2017 1:10 am     Reply with quote

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

View user's profile Send private message AIM Address

PostPosted: Tue Apr 25, 2017 6:46 am     Reply with quote

.099 - the limit of the short -float format CCS uses .
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Tue Apr 25, 2017 11:30 am     Reply with quote

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