what is too much for float

General discussion on mikroC PRO for PIC.
Post Reply
Author
Message
Bushman
Posts: 27
Joined: 11 Apr 2006 16:55
Location: Canada

what is too much for float

#1 Post by Bushman » 11 Nov 2010 10:27

Hi, is this number (131159024.164) too big to be fully encoded in float type? I am not getting fract part of the number. Only after I cut first several numbers off like fNumber=59024.164 the function modf() gives correct fractional part, where is the problem ?

Code: Select all

float fNumber=131159024.164;
float wholePart=0;
float fractionalPart=0;

void main() 
{
  fractionalPart=modf(fNumber,&wholePart);
  // I am getting fractionalPart = 0; is 131159024.164 too big ?
}

KaranSoin
Posts: 130
Joined: 07 May 2010 22:27
Location: Melbourne, Australia

Re: what is too much for float

#2 Post by KaranSoin » 11 Nov 2010 16:21

Number is not too big, its being rounded off. There are too many digits to in ur number to fit in a 4 byte float.


Regards

Bushman
Posts: 27
Joined: 11 Apr 2006 16:55
Location: Canada

Re: what is too much for float

#3 Post by Bushman » 11 Nov 2010 18:25

is there a way of storing numbers like that in uC?, may be splitting and keeping in separate variables... but than multiplying dividing ect. such pairs will be a nightmare...

thanks

KaranSoin
Posts: 130
Joined: 07 May 2010 22:27
Location: Melbourne, Australia

Re: what is too much for float

#4 Post by KaranSoin » 12 Nov 2010 02:05

I think using "double" may have solved ur issue, but in MikroC both float and double are 4 bytes, so that wont do it. I know there are third party libraries available which allow you to do IEEE double and long double, but it will induce a huge overhead on ur processor and take a fair bit of program memory. What I have experienced, usually the easiest way is to change the approach of the problem itself. In your example you have'nt mentioned what exactly are you using the big numbers for but usually, there is a way around it.
Splitting the number can work, make a structure with a float and an int, where float always represents the lower part of the number (say less than 10,000) and int represents the higher part of the number. But life is gng to become very difficult if you plan to use a lot of library functions with floats (like modf() ).


Regards

Bushman
Posts: 27
Joined: 11 Apr 2006 16:55
Location: Canada

Re: what is too much for float

#5 Post by Bushman » 12 Nov 2010 19:31

I am reading binary messages from GPS, and this is the number range that I want to process in PIC18F452. It is sent as 64 bit ieee double. I wrote a function which transforms ieee 64 bit pattern into Microchip 32bit pattern (which is different from ieee 32 bit float pattern). and everything seemed like fine till I realized that I am loosing accuracy.

Thanks for helping me to distinguish between float range and float accuracy, for some reason I thought these are the same.

Now I am going to try to read the whole part from 64 bit pattern, save it in a variable, and than read fractional part from that pattern and keep it in a second variable, and see If I can go around this problem this way.

KaranSoin
Posts: 130
Joined: 07 May 2010 22:27
Location: Melbourne, Australia

Re: what is too much for float

#6 Post by KaranSoin » 13 Nov 2010 08:04

look for some 64 bit C libraries, since its a standard 64 bit double, good chance you may find something and could use some selected functions out of it.

Regards

p.erasmus
Posts: 3391
Joined: 05 Mar 2009 10:28

Re: what is too much for float

#7 Post by p.erasmus » 13 Nov 2010 09:33

Just my penny's worth

If you are using such large floating point numbers in an application,using a 8 bit micro
is certianly the wrong choice ,
why dont you use an floating point processor connected to the serial port of the micro
and let the floating point processor handle the large floating point calculations and
numbers.
P.Erasmus
Saratov,Russia
--------------------------------------------------------------

Post Reply

Return to “mikroC PRO for PIC General”