`acos` returning `-nan`?

Jonathan Levi catanscout at gmail.com
Sat Nov 23 17:31:22 UTC 2019


I have a `double` (from a math equation) which when logged is 
`-1` but when a `acos` it it returns `-nan`?

The `double` has different bit representation than a normal `-1` 
but I do not see how that is messing up its `acos`.

The `double`'s representation is: true,1023,58720256.

This will reproduce the problem:

```
import std;

void dWrite(double d) {
     import std.stdio;
     import std.conv;
     import std.bitmanip;
     auto dr = DoubleRep(d);
     writeln(d,"\t",dr.sign,"\t", dr.exponent,"\t", dr.fraction);
}
double fromRep(bool sign, ushort exponent, ulong fraction) {
     DoubleRep r;
     r.sign = sign;
     r.exponent = exponent;
     r.fraction = fraction;
     return r.value;
}

void main() {
     double failing = fromRep(true,1023,58720256);
     double lookalike = -1;

     failing.dWrite;
     lookalike.dWrite;

     failing.acos.dWrite;
     lookalike.acos.dWrite;
}
```

Any idea why that is and how I could solve it?

BTW this is the how that number is created: 
`cos(a.angle)*cos(b.angle) - 
dot(a.axis*sin(a.angle),(b.axis*sin(b.angle)))`




More information about the Digitalmars-d mailing list