Why is printf showing -1.#IND for FPTAN results?

By : jheriko
Source: Stackoverflow.com
Question!

I am working on a program which produces assembler code from expressions. One of the functions required is tan(x) which currently works using the following sequence of code (the addresses are filled in at run time):

fld [0x00C01288];
fld st(0);
fsin;
fld st(1);
fcos;
fdivp;
fst [0x0030FA8C];

However, I would like to use the FPTAN opcode instead, so I tried using the following code:

fld [0x00C01288];
fptan;
fincstp;
fst [0x0030FA8C];

The test program uses printf to show the result stored at 0x30FA8C, but for the second sequence the result is displayed as -1.#IND (the first using cos and sin works fine). If I try examining the value at the memory address, or on top of the floating point stack, in the debugger, it appears as the correct number.

So, my question is: Why is printf showing -1.#IND and how can I fix it?

The value at 0x00C01288 is double precision 0.5 The result in both cases is ~0.5463024898

My first thought was that the value being stored was a different representation of the same number, but inspecting the value stored at 0x0030FA8C shows it to be 0x3FE17B4F5BF3474A in both cases.

I do not understand why identical inputs to the printf function could produce different output...

Any help or suggestions would be greatly appreciated.

Edit: Source where printf is called:

#include "FridgeScript.h"
#include <stdio.h>
#include <math.h>

char test[] = "a=tan(0.5);";

int main(int c, char** s)
{
    unsigned int SC = FSCreateContext();

    double a = 0.0;
    FSRegisterVariable(SC, "a", &a);

    unsigned int CH = FSCompile(SC, test);
    if(CH) FSExecute(SC, CH);

    printf("a: %.10g\r\n", a);
    printf("hex a: %I64X", a);

    FSDestroyContext(SC);
    return 0;
}
By : jheriko


Answers

Let me just throw something out there: how about using

fstp st(0);

instead of

fincstp;

The docs on fincstp say it's not equivalent to popping the item from the stack because it leaves that spot tagged as filled - perhaps this is messing up the float handling inside of printf?

(You may be able to guess I don't know what I'm talking about. But maybe this will give you an idea?)



Maybe your FST instruction store just the float result, and the second dword is still uninitalized?

I don't see any size prefix in the assembly code. Usually you see something like:

  FST [dword ptr some_address]

  or 

  FS [qword ptr some_address]


fld [0x00C01288];
fptan;
fincstp;
fst [0x0030FA8C];

To me it looks like after this code executes, the tangent is still at st0. Shouldn't you use fstp instead of fst?



This video can help you solving your question :)
By: admin