|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
|
|
Thread Tools | Rate Thread | Display Modes |
|
|
|
#1
|
|||
|
|||
|
Cannot access "double" values
We are having problems with using double or float values.
We have installed the new compiler(version 2.40). But still cannot use doubles in it. When we tried to write : double x = 0.75; it gives us compile error. Also we cannot print float or doubles through printf(). We dont know what to do. Any help is appreciated. Team 612 |
|
#2
|
||||
|
||||
|
Re: Cannot access "double" values
Could you give us the actual error when you try to compile? Also, I don't think that this printf function supports floating-point variables. I have never been able to print floating point variables (without scaling them and casting them as integers), and I seem to remember someone else saying it couldn't be done.
|
|
#3
|
|||||
|
|||||
|
Re: Cannot access "double" values
I can use
double f=9.9; without any trouble. This Microchip implementation of printf doesn't support the printing of floats, but you can easily print them on your own. Code:
#define ACCURACY 1000 //How many decimal places are important to you
void PrintFloat(float value)
{
long i, i2; // Don't need to be longs if you only need a few significant digits
/* Separate the whole number from the fraction and print each of them */
i = (long) value;
i2 = (long) ((value-i)*ACCURACY); // You can get fancy and round by adding .5
printf(" %d.%03d \r", (int)i, (int)i2); //e.g., "234.078"
}
Last edited by Mark McLeod : 05-02-2005 at 22:21. Reason: Loose use of the term "printf" caused confusion |
|
#4
|
|||||
|
|||||
|
Re: Cannot access "double" values
Unless I'm mistaken, u can print floats like this:
Quote:
Dan Team 375 |
|
#5
|
||||
|
||||
|
Re: Cannot access "double" values
Quote:
|
|
#6
|
||||
|
||||
|
Re: Cannot access "double" values
OK, let's be clear here. "printf" is not a C function. C does not define any I/O. It is written by you.
Now, IFI, Keven Watson and others have been gracious in providing you with a couple of examples as to how to accomplish this. You have the source code for whichever printf you are trying to use. If you feel the (IMHO: insane, stupid and ignorant) need to use a float or double, change printf to suit your own needs. Mike Post Script: Sorry for the rant but this stuff is getting really tiresome... |
|
#7
|
||||
|
||||
|
Re: Cannot access "double" values
Quote:
Quote:
|
|
#8
|
||||
|
||||
|
Re: Cannot access "double" values
Quote:
You are correct, of course. However, I must inquire: If someone does not read the information presented, should we help at all? But I must point out that printf is not a "library" function or inherent to C. It is defined (in the default code V2.4) in printf_lib.c (maybe a poor choice of file names) and supports %d, %x, %X, %u, %l and %b formats. All are integer formats (and, IMHO, for a reason, but I'll avoid the "INTEGERS FOREVER WOOO!!!1" rehash). My issue is that people are not even looking at the code they are given. I can understand (begrudgilngly) questioning libraries where you have no view of the source code. But to complain about source code that YOU have control over... I would ask you... How many threads have started with "The printf routine does not ..." or "the default code will not..." or My camera does not..."? Too many people are looking for "plug and play" code... They need to realize that, in the real world, it just does not exist. In the real world, we have no IFI or Kevin Watson to give us exquisite examples of embedded programming... What we have are spec sheets, manuals and application notes... And with this, we manage to send robots to distant planets and the bottom of the ocean... I'm sorry... I'm tired and... Maybe you are right.... Maybe I should not be posting in this state... Still, I hope that "the light" is going on in at least one mind out there... Regards, Mike |
|
#9
|
|||||
|
|||||
|
Re: Cannot access "double" values
There is some additional confusion here in that printf in the new 2.4 version of the compiler is now an available library function. It's provided in the mcc18 clib.lib. It's recommended by FIRST that programmers remove the old printf_lib files from their projects to take advantage of the more efficient implementation (fewer calories/less filling of program space).
That said, I have to agree with Mike that it's always a good idea to read the available documentation on any software/compiler/development environment/etc. You are only handicapping yourself by not doing so. There are always implementation oddities/quirks/limitations that programmers must make themselves aware of. How long did you beat your head against the wall trying to figure out why your code wasn't working? In the case of the new C18 printf that's to be found in the manual that came with the compiler describing the C18 libraries. New programmers have a particular challenge with the somewhat overabundence of software implementations available this year. We have three different default versions for the FRC released by FIRST this year. While that's great for the intermediate programmers and certainly helps to stretch their programming "muscle," it can make it more difficult for a newbie to sort through the documentation/headers/code for each of these versions to decide what to use/combine and what to ignore/disgard. It'll be much easier after all this has had time to settle a little bit and we all get through this somewhat transitional stage in the evolution of FIRST software. Last edited by Mark McLeod : 05-02-2005 at 23:36. |
|
#10
|
||||
|
||||
|
Re: Cannot access "double" values
Quote:
For embedded applications, I've written my own printf since I ported Unix V to a 68020 back in '86. I guess I'm a control freak... Regards, |
|
#11
|
||||
|
||||
|
Re: Cannot access "double" values
Quote:
I'd be very interested where you read/heard that... Mike |
|
#12
|
|||||
|
|||||
|
Re: Cannot access "double" values
Quote:
(need a sleepy emoticon) |
|
#13
|
|||
|
|||
|
Re: Cannot access "double" values
ok, We got the problem solved. We can now use double and float values. But they have to be declared on the top, that is, they should be global varibles and they dont work in the sub-routines. (thats what we discovered). We can now use tan functions and inverse tan functions. But the process is very very time-consuming and slow. We fear that the processor might just get crashed in real time. So instead we are converting everything into a pre-made table and use the table to get the tan and inverse tan values. That is a lot faster and we would recommend using it that way. Thanks for your help though.
Team 612. |
|
#14
|
|||||
|
|||||
|
Re: Cannot access "double" values
Quote:
Quote:
Quote:
Quote:
Quote:
Just the observations of a FIRSTer who has asked no shortage of stupid questions, but still ended up having my life changed by this program. |
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Acceleration Sensor Connected, but values are odd.... | Leav | Electrical | 7 | 04-02-2005 15:28 |
| trentonDrive.c: our joystick/wheel drive code | gnormhurst | Programming | 16 | 03-02-2005 14:40 |
| Good book to learn MS Access from? | Elgin Clock | Math and Science | 2 | 05-10-2004 20:20 |
| A testimony to my unique FIRST experience: | Ken Leung | General Forum | 22 | 14-04-2004 20:45 |
| Access Database help... | kacz100 | Scouting | 12 | 18-03-2004 19:18 |