This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

log10(...) prob

hi,
i've got a problem when using log10(...) or log(...) from math.h .
e.g. log10(10) doesn't return 1 but something like 3.1... e+015.
does anyone have an idea what could be the reason for this?

thx
j.w.

  • I tried this in the simulator and it works fine.

    -Walt

  • It must have something to do with the fact that the local variable that holds the return value is not used anywhere. Probably the compiler simply optimizes it out (discards the return value.) See the disassembly of the code.
    Try making the variable global, or reduce optimization level, or use the result of log10 somehow in your code to prevent the compiler from discarding it.

    - mike

  • I'd suggest running the program on the target rather than the simulator and printf() the value out of the serial port. I tried this with your program and it gave the correct result, but the simulator gave 3.5xxxx e-43. I've been using Keil for years and still can't get the simulator to work. But then, I haven't read the manual.

    Stefan

  • If you are having problems on the simulator it may be for reasons that Mike points out. The log10 function works for me in the sumulator but I made sure I stored the result in a volatile floating point variable so I knew it would not get optimized out. Using the variable in a printf statement after calling log10 should also do the trick.

    -Walt

  • Walt,

    I even tried this:

    float x,y;
    x=log10(10);
    printf("%f",x);
    y=x;
    printf("%f",x);

    breaking on the y=x line still gave garbage for x, however the printf() statements worked fine.

    Stefan

  • Hi Stefan,

    Very strange, I copied and pasted your code into a file and simulated it and it works fine for me. The line y=x; gets optimized out but x contains the value 1 and both printf's output 1.00000.

    -Walt

  • Uhh,

    You still don't use y in your program. The C51 IS an optimizing compiler.

    Jon

  • Well, I've no idea what's going on now. I created a new project, pasted the code into it and it now works fine in the simulator.

    An odd thing though, I have optimisation level 11 selected and in my case y doesn't seem to be optimised out:

    .cod:

    ----- FUNCTION main (BEGIN) -----
    FILE: 'junk.c'
    27: void main(void)
    28: {
    29: float x,y;
    30:
    31: Setup();
    000C48 9132 ACALL Setup
    32:
    33: x=log10(10);
    000C4A 7F00 MOV R7,#00H
    000C4C 7E00 MOV R6,#00H
    000C4E 7D20 MOV R5,#020H
    000C50 7C41 MOV R4,#041H
    000C52 5173 ACALL _log10
    000C54 8F25 MOV x+03H,R7
    000C56 8E24 MOV x+02H,R6
    000C58 8D23 MOV x+01H,R5
    000C5A 8C22 MOV x,R4
    34: printf("%f",x);
    000C5C 7BFF MOV R3,#0FFH
    000C5E 7A0C MOV R2,#HIGH 01000C2FH
    000C60 792F MOV R1,#LOW 01000C2FH
    000C62 8F30 MOV ?_printf?BYTE+06H,R7
    000C64 8E2F MOV ?_printf?BYTE+05H,R6
    000C66 8D2E MOV ?_printf?BYTE+04H,R5
    000C68 8C2D MOV ?_printf?BYTE+03H,R4
    000C6A 120636 LCALL _printf
    35: y=x;
    000C6D 852529 MOV y+03H,x+03H
    000C70 852428 MOV y+02H,x+02H
    000C73 852327 MOV y+01H,x+01H
    000C76 852226 MOV y,x
    36: printf("%f",x);
    000C79 7BFF MOV R3,#0FFH
    000C7B 7A0C MOV R2,#HIGH 01000C2FH
    000C7D 792F MOV R1,#LOW 01000C2FH
    000C7F 852530 MOV ?_printf?BYTE+06H,x+03H
    000C82 85242F MOV ?_printf?BYTE+05H,x+02H
    000C85 85232E MOV ?_printf?BYTE+04H,x+01H
    000C88 85222D MOV ?_printf?BYTE+03H,x
    000C8B 020636 LJMP _printf
    ----- FUNCTION main (END) -------

    Stefan