# Acos apparently inconsistent

I am having a nightmare with my application. I put together a test case with what I think is the root of my problems:

With a initial value of 45 acos (of -1.0) works (it does not give 180 though).
Change the value to 50 and acos of the same value (-1.0) gives NaN now.

Any advice would be very much appreciated.
Thanks & regards.

Where did you find these formulas?

Using the Companion, apply Do It to each block starting from the top down, working from right to left, to trace all the values going into each operation, to isolate where the NaN occurs.
I see 2 possibilities:

• division by 0, or
• acos of a number outside the range (-1 to 1).

P.S.

Please download and post each of those event block(s)/procedures here ...
(sample video)

P.P.S.

Trashing the input to a block destroys the debugging trail. It's like muddying the footprints at a crime scene. Use separate variable for input and output at each stage, so they will be untouched for later examination.

Also, switching your formula blocks from inline to external sockets will reveal their nesting, in case you made a mistake assembling your formula.

P.P.P.S
By the way, you are dealing with floating point numbers, which are inexact. So even if your formulas are perfect, they might be producing an input to acos() (which you don't show) with an absolute value a tiny amount over 1, say -1.000000000001, beyond what AI2 might be rounding. That would not be the fault of the acos() function.

Hello,

"Where did you find these formulas?"

I do not understand this question.

"division by 0" no because neither sin(100) nor cos(50) are 0.
"acos of a number outside the range (-1 to 1)" as I said, the value showed in label1 was -1.0.
For your answer I did a formatting as decimal number with 20 decimal places, and I saw some extra figures at the end, that caused the acos NaN response.

"switching your formula blocks from inline to external sockets" I do not know what external sockets are.

I wonder why a simple school calculator does not have this problem. I would like to read about floating point precission in AI2 but I did not find documentation.

Thanks a lot for providing this explanation to the issue.
Regards.

P.S.
The results of Do-it did not show in the png. The number was: "-1.00000000000000022204"

See https://www.exploringbinary.com/numbers-in-app-inventor-are-stored-as-floating-point/
for a very good explanation.

Always learning!

"floating point precision in AI2"

Really useful.

Thank you again.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.