C Programming: acos() not accepting variable

Click For Summary

Discussion Overview

The discussion revolves around the use of the acos() function in C programming, specifically addressing issues related to variable input and the resulting output. Participants explore the implications of integer division, data types, and the expected behavior of the function in different contexts.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Exploratory

Main Points Raised

  • Some participants question whether the variable 'i' is declared as an integer, noting that integer division results in an integer output, which may not be suitable for acos().
  • It is suggested that the expected output of acos(i/2) should be close to 1.047 radians, not 60 degrees, due to the nature of the input.
  • One participant provides a code example demonstrating the use of acos() with different double values, showing the expected outputs for 0.0, 0.5, and 1.0.
  • Some participants express frustration over the lack of specific error messages and emphasize the importance of providing detailed information about errors encountered.
  • There is a mention of the C++11 standard, which introduces additional overloads for integral types in acos(), but the relevance to the original question is debated.
  • Concerns are raised about using outdated compilers, with one participant suggesting that a newer compiler could resolve some issues.
  • Participants discuss the importance of understanding how integer expressions are evaluated before being converted to double, which could lead to unexpected results in function calls.

Areas of Agreement / Disagreement

Participants generally agree that integer division leads to unexpected results when used with acos(), but there is no consensus on the specific nature of the errors encountered or the best approach to resolve them. Multiple competing views remain regarding the handling of data types and the expected behavior of the function.

Contextual Notes

Limitations include potential misunderstandings about data types, the effects of integer division, and the specific behavior of different compilers. The discussion does not resolve these issues definitively.

angelspikes
Messages
10
Reaction score
0
The following function acos() does not accept any variables.
I have included the math.h header.

For example
i = 1;

acos(i/2); // This should be equal to 60
 
Technology news on Phys.org
I take it we're supposed to guess what kind of error message you get and whether you get it a compile time or run time?
 
Is i declared as an integer? Integer divided by integer gives an integer as its result. I'd be surprised if acos() accepts an integer as its argument.

Hmmm... according to http://www.cplusplus.com/reference/cmath/acos/, in C++11, acos() does have "additional overloads for integral types". Still, integer division of 1/2 gives 0 as its result, so even if you were using C++11 and the compiler accepted it, you might not like the result anyway!
 
angelspikes said:
acos(i/2); // This should be equal to 60

To add to what was already posted - it shouldn't return 60. It should return something close to 1.047.
 
Here's a refernce to the inverse trig functions:

http://www.gnu.org/software/libc/manual/html_node/Inverse-Trig-Functions.html#Inverse-Trig-Functions

from it you can see the input to acos() is a double and the output is a double value of the angle in radian measure

you intended to input 0.5 and expected to get 60 degrees. but 1/2 = 0 via integer division and so you should get a radian measure of pi/2 or 1.57 radians.

you should try coding it as acos(i/2.0) or something like that and see if you get the expected angle in radian measure.
 
Last edited by a moderator:
phinds said:
I take it we're supposed to guess what kind of error message you get and whether you get it a compile time or run time?

Yes, it's one of those days.
 
jtbell said:
Is i declared as an integer? Integer divided by integer gives an integer as its result. I'd be surprised if acos() accepts an integer as its argument.

Hmmm... according to http://www.cplusplus.com/reference/cmath/acos/, in C++11, acos() does have "additional overloads for integral types". Still, integer division of 1/2 gives 0 as its result, so even if you were using C++11 and the compiler accepted it, you might not like the result anyway!

No matter what data type I use, even float, will return acos(variable) as an error.
 
Perhaps we can uncover the problem by being more specific

Code:
% cat acos.c

#include <math.h>
double acos(double x);

main()
{
double y;
y=0.0;
printf("%f\n", acos(y));
y=0.5;
printf("%f\n", acos(y));
y=1.0;
printf("%f\n", acos(y));
}

% cc acos.c -lm

% a.out

1.570796
1.047198
0.000000

So can you reproduce exactly these steps and post exactly what you got out?
I did cheat slightly by inserting a blank line between each of my lines, but changed nothing else.
And that was run under FreeBSD with the supplied cc version 2.95.4.

With gcc I get

Code:
% gcc acos.c -lm
% a.out
1.570796
1.047198
0.000000

but the gcc also reports version 2.95.4 so they are probably aliased to be the same thing at some level.
 
Last edited:
angelspikes said:
No matter what data type I use, even float, will return acos(variable) as an error.

How about any other math functions?

Remember, the more information you provide, the quicker we can help resolve your problem, even if it is 'one of those days.'
 
  • #10
angelspikes said:
No matter what data type I use, even float, will return acos(variable) as an error.
Meaning what? You need to be specific when asking about programming errors. You also need to realize that the error is almost certainly yours. The number of times I thought I had run into a compiler or linker bug in my 40+ years programming: Countless. The number I've run into a true compiler or linker bug: Very, very few. The error is almost always mine.

Does your program fail to compile, fail to link, or fail at runtime? What exactly do you mean by "will return acos(variable) as an error"?
 
  • #11
Bill Simpson said:
And that was run under FreeBSD with the supplied cc version 2.95.4.
Oh. My.

Get a new compiler! gcc 2.95 is a compiler written in some previous millennium. There is *no* excuse to be using a buggy free compiler from some previous millennium when you can get a very good free compiler from this decade.
 
  • #12
My only purpose was to try to get the original poster to provide detailed information about exactly what he had done.
 
Last edited:
  • #13
jtbell said:
Is i declared as an integer? Integer divided by integer gives an integer as its result. I'd be surprised if acos() accepts an integer as its argument.

It doesn't accept an integer (assuming the OP included a header file that declared it properly, of course!)

But every version of C from the beginning knew how to convert integers to doubles. The problem is that it evaluates i/2 as an integer expression and THEN converts the result to double.

Hmmm... according to http://www.cplusplus.com/reference/cmath/acos/, in C++11, acos() does have "additional overloads for integral types".

That's irrelevant to the question. C++11 has some new features so you can tell the compiler that a user-defined type (class) behaves the same way as the built-in integer types, etc. The more the compiler "knows" about the semantics of the code, the more optimization it can do. But "a class that behaves like an integer" is not exactly the same as "an integer", so the "additional overloads" are giving the compiler yet more information.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 25 ·
Replies
25
Views
4K
  • · Replies 2 ·
Replies
2
Views
3K
Replies
4
Views
5K
Replies
65
Views
5K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
81
Views
8K
  • · Replies 14 ·
Replies
14
Views
35K
  • · Replies 17 ·
Replies
17
Views
2K