View previous topic :: View next topic |
Author |
Message |
frank0204
Joined: 01 Nov 2011 Posts: 3
|
Bug with math? |
Posted: Tue Nov 01, 2011 5:30 am |
|
|
Hi,
I downloaded the evaluation version of the compiler because at the university I would compare it with C18.
I made this simply test program:
Code: |
#include <18f46k22.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#device ANSI
#device PASS_STRINGS=IN_RAM
#use delay(clock=64000000, internal)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#include "math.h"
void printResult(const char* text, float number){
number=log10(number);
printf("%s: %10.7f\r\n",text,number);
}
void main()
{
printResult("Result",10);
while(1);
}
|
I simulated it in MPSIM but the result of log10(10) is different than 1! The result is 0.00232....
Where I'm wrong?
Thank you
F |
|
|
Battery David
Joined: 01 Feb 2010 Posts: 25
|
|
Posted: Tue Nov 01, 2011 8:02 am |
|
|
I tried this with your function but couldn't get it to work.
I popped the log10(xx) into a program I was working on and it gave me the correct result. I tried making xx an unsigned int8 and it still worked.
I'm using 4.125, what version are you using?
David |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19520
|
|
Posted: Tue Nov 01, 2011 8:20 am |
|
|
Are you looking at what is actually printed out of the serial, or a 'watch'?.
Prints 'Result: 1.0000000' for me.
Has the sound of something like a watch window set to the wrong type. Remember a float uses the MCHP float format, not IEEE formats.
What is the actual version number of the demo compiler?.
Best Wishes |
|
|
frank0204
Joined: 01 Nov 2011 Posts: 3
|
|
Posted: Tue Nov 01, 2011 12:07 pm |
|
|
Hi,
I'm using the evaluation version downloaded from the site.
From the "PIC version" window I see that the version is 4.122d "Limited evaluation".
I found what is the problem and it is related to the #device ANSI directive.
If delete this row and adjust the code the result of log10 is right.
Please use my code below to check the problem:
Code: |
#include <18f46k22.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
//Comment or Comment-out this define to see the problem.
//When _USE_ANSI_ defined the result of log10 is wrong
#define _USE_ANSI_
#ifdef _USE_ANSI_
#device ANSI
#endif
#device PASS_STRINGS=IN_RAM
#use delay(clock=64000000, internal)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#include "math.h"
#ifdef _USE_ANSI_
void printResult(const char* text, float number){
#else
void printResult(char* text, float number){
#endif
number=log10(number);
printf("%s: %10.7f\r\n",text,number);
}
void main()
{
printResult("Result",10);
while(1);
}
|
the #device ANSI directive cannot be used with math functions?
Note: I tested the code both on MPLab Sim and on a real pic18f46k22: same behaviour.
Thanks
F |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Nov 01, 2011 4:40 pm |
|
|
I got a similar failure in vs. 4.125. It appears that the math.h library has
not been tested in ANSI mode. Or at least the log10() function hasn't been
tested in that mode. However, most of us do not use ANSI mode so it's
not too much of an issue for us. |
|
|
frank0204
Joined: 01 Nov 2011 Posts: 3
|
|
Posted: Tue Nov 01, 2011 5:11 pm |
|
|
Yes,
Same problems with ln, exp and pow.
The math library does not work with ANSI mode or is bugged.
So a porting from C18 can be very annoying...
Hope in the future the ANSI mode will be supported.
Thank you
F |
|
|
|