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

float math operations problems

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



Joined: 04 Jan 2012
Posts: 18

View user's profile Send private message

float math operations problems
PostPosted: Sat Jan 28, 2012 4:10 pm     Reply with quote

Hi everyone,

I'm trying to operate with precision numbers (ex: 146516.728, 0.0.52118, etc) in a math operations, and with algorithm that use a data type "double".
Before to program the main algorithm, I have some troubles that must to solve.

1.- floor function problem,
Code:

#Include <18f2550.h>   
#use delay (clock=48000000)
#use Rs232( baud=9600,xmit=Pin_c6,rcv=Pin_c7,parity=n,bits=8)

#INCLUDE <math.h>
#Include <stdio.h>
#Include <stdlib.h>
#Include <float.h>

void main(){
float aux1;
year_aux=2011.0;
aux1= (365.25*(year_aux+2000.0));
printf("\r aux1:%8.3f",aux1);
}

When I simulate the answer was:
1465017.75 (calculator)
1465016.728 (response algorithm)
Why occurs this error, if i try with float numbers, and the representation are until 3.4*10^38 ?

Moreover, but when I used a floor function
1465017 (obviously)
1465016.960 (algorithm response, error)

Why???

2.- If I try to operate with a fixed point ex.
int32 _fixed(4) aux1, could be operate "fixed point" and float numbers simultaneously?? Operate with +,-,/,floor(), sin(), cos(),etc..

3.- When I can learn about float point operation with ccs??

Thanks for your time
RVR

I'm using a Pic18F2550, simulator proteus 7.6, PCWH 4.1.
Ttelmah



Joined: 11 Mar 2010
Posts: 19382

View user's profile Send private message

PostPosted: Sat Jan 28, 2012 4:42 pm     Reply with quote

Key thing, is that there _isn't_ a 'double' type. Double _only_ exists for things like DSP chips. In standard CCS C, the keyword is for 'compatibility only'. So you have a 6.5digit effective _single precision_ float only.

'Why occurs this error'. Simple you are trying to use precision that is not there in the available maths....

Best Wishes
RVR



Joined: 04 Jan 2012
Posts: 18

View user's profile Send private message

PostPosted: Sat Jan 28, 2012 5:39 pm     Reply with quote

Ttelmah wrote:
Key thing, is that there _isn't_ a 'double' type. Double _only_ exists for things like DSP chips. In standard CCS C, the keyword is for 'compatibility only'. So you have a 6.5digit effective _single precision_ float only.

'Why occurs this error'. Simple you are trying to use precision that is not there in the available maths....

Best Wishes


Hi Ttelmah,
I had some suspicions that the magnitude of the number was the error source (a few digits numbers had been tested ) but did not know the amount of numbers (precision) that supported the compiler (6.5), and so appreciate your help.

But i need to work at these conditions, even if it losing precision.
Any idea?

For example fixing a decimals at three (for ex) but i don't know if it is possible using a fixed point and if it works with the math.c library?
Douglas Kennedy



Joined: 07 Sep 2003
Posts: 755
Location: Florida

View user's profile Send private message AIM Address

PostPosted: Sun Jan 29, 2012 1:50 am     Reply with quote

Quote:
I'm using a Pic18F2550, simulator proteus 7.6, PCWH 4.1.

There is a lot that could happen here between a proteus simulation and calculations using real PIC registers. We all learned math using base ten notation and most calculators use BCD so they also use base ten notation. This makes us accept only decimal notation as accurate. The PIC uses binary notation accurately.
That means a PIC's accurate binary notation is faithfully translatable to decimal only for power series of 2 up to the bit precision used. It is not that the floating point binary gets an erroneous result ( it is accurate in binary notation ) its just that an accurate decimal translation is impossible. We encounter this all the time in decimal Ex. 1/3 0.3333... can't be accurately notated in decimal yet 1/2 0.5 can.
For the PIC translational issues may occur on the way into binary and on the way back to decimal. The simple solution is to stay with power series of 2 so use fixed point arithmetic. Otherwise use float and accept the accurate base 2 calculation and the translation error to decimal that shows in the last significant digits. Now the PIC is capable of doing binary floating point to any precision assuming there is sufficient RAM to hold the results. Since binary float math is CPU intensive the CCS compiler only does this for a 23 bit mantissa and an 8 bit exponent which in decimal means 6 or 7 significant digits. A PC might do 64 bit precision binary still with translation to decimal issues but only in numbers with many decimal digits of significance.
Ttelmah



Joined: 11 Mar 2010
Posts: 19382

View user's profile Send private message

PostPosted: Sun Jan 29, 2012 3:04 am     Reply with quote

float.h, tells you all the precision and range limits for the numeric types.
Double is shown here, because it is available in PCD. If you look at the PCH manual, you get:
"double
Is a reserved word but is not a supported data type."
The PCD manual, offers you float, float48, and float64 types.

Best Wishes
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