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

Rounding function does not return any value

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



Joined: 07 Dec 2016
Posts: 60
Location: Northeast USA

View user's profile Send private message

Rounding function does not return any value
PostPosted: Thu Aug 16, 2018 1:00 pm     Reply with quote

CCS compiler v5.074
PIC18F24K40

I found Ttelmah's post from 09/14/2006 on implementing a rounding function for floating point numbers. I tried to implement this code:

Code:

#include <18F24K40.h>
#device PIC18F24K40
#device ADC=10                                                   
#use delay(INTERNAL=2MHZ)                                         
#fuses NOWDT                                                     
#fuses NOPROTECT
#fuses NOBROWNOUT
#fuses NOPUT           

#include "math.h"
#define round(x,n) (floor(x*(10.0^n)+0.49)/(10.0^n))

float32 num1 = 65.12345;
float32 num1round;


void main(void)
{
   
    num1round = round(65.12345, 3);  // Does not matter if var 'num1' used here or explicit float used here
   
    while(1)
    {
       
       
       
    }
   
}


Code successfully compiles. I run a debug session, and pause the execution. num1round is always zero in the variable Watches tab ( I'm using MPLAB as the IDE, btw)

Any idea as to why this rounding routine is not working in my case?
Ttelmah



Joined: 11 Mar 2010
Posts: 19215

View user's profile Send private message

PostPosted: Thu Aug 16, 2018 1:18 pm     Reply with quote

Where are you putting the breakpoint to stop and view the variable?.
Remember if you put it on this line, it has not executed yet.
apakSeO



Joined: 07 Dec 2016
Posts: 60
Location: Northeast USA

View user's profile Send private message

PostPosted: Thu Aug 16, 2018 2:59 pm     Reply with quote

Ttelmah wrote:
Where are you putting the breakpoint to stop and view the variable?.
Remember if you put it on this line, it has not executed yet.


Not actually using a breakpoint. I start program execution in the debugger. Lines above the while(1) get executed, then I hang around forever in the while(1) loop. So at some point I hit pause in the debugger, and view the values displayed as in the graphic.
temtronic



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

View user's profile Send private message

PostPosted: Thu Aug 16, 2018 3:14 pm     Reply with quote

gee I have to ask, why not just send the result to a PC terminal program ?
I've always treated debuggers like simulators...prefer to see real results in the real World.
As an 'aside', are you really running that PIC at TWO MHZ ?Have to wonder how long that code takes to execute !!
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Aug 16, 2018 4:00 pm     Reply with quote

Try it with the macro for round() as shown below, with the pow() function.

I got the following result:
Quote:
65.123

Test program:
Code:

#include <18F46K22.h>
#fuses INTRC_IO, NOWDT, BROWNOUT, PUT, NOPBADEN
#use delay(clock=4M)
#use rs232(baud=9600, UART1, ERRORS)

#include "math.h"
#define round(x,n) (floor(x*(pow(10.0,n))+0.49)/(pow(10.0,n)))

float num1round;

//=================================
void main(void)
{

num1round = round(65.12345, 3); 
   
printf("%7.3f\r", num1round);

while(TRUE);
   
}
Ttelmah



Joined: 11 Mar 2010
Posts: 19215

View user's profile Send private message

PostPosted: Fri Aug 17, 2018 1:38 am     Reply with quote

Dead right. ^ is XOR in C.
Classic problem of using too many languages...
apakSeO



Joined: 07 Dec 2016
Posts: 60
Location: Northeast USA

View user's profile Send private message

PostPosted: Fri Aug 17, 2018 9:21 am     Reply with quote

temtronic wrote:
gee I have to ask, why not just send the result to a PC terminal program ?
I've always treated debuggers like simulators...prefer to see real results in the real World.
As an 'aside', are you really running that PIC at TWO MHZ ?Have to wonder how long that code takes to execute !!


Hahah, I know! 2Mhz actually gives me a fair compromise between power consumption and speed. This is not a time-sensitive application. Smile

For posterity, I timed the pulse output from the following lines. This takes approx. 35ms to execute when the PIC is set to 2MHz:


Code:

    output_high(PIN_C6);
    num1round = round(num1, 3);
    output_low(PIN_C6);


given that num1 is a float32 and num1 = 65.12345 and round() is defined as above in the correction.

Given the modification to using pow() instead of ^ the code now does what it should. Thanks again guys. You da real MVP.
Ttelmah



Joined: 11 Mar 2010
Posts: 19215

View user's profile Send private message

PostPosted: Fri Aug 17, 2018 12:07 pm     Reply with quote

Seriously all the normal complaints about using float do apply. If you only want fixed rounding at three digits, consider instead using an integer:
Code:

#include <18F46K22.h>
#fuses INTRC_IO, NOWDT, BROWNOUT, PUT, NOPBADEN
#use delay(clock=4M)
#use rs232(baud=9600, UART1, ERRORS)

#include "math.h"

int32 num1round;

//=================================
void main(void)
{

    num1round = (65.12345+0.00049)*1000;
   
    printf("%7.3lw\r", num1round);

    while(TRUE)
       ; 
}


This will take a lot less time and give the same result.
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