C Programming: acos() not accepting variable

In summary: Integer divided by integer gives an integer as its result. I'd be surprised if acos() accepts an integer as its argument.No, it accepts a double.Integer divided by integer gives an integer as its result. I'd be surprised if acos() accepts an integer as its argument.
  • #1
angelspikes
10
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
  • #2
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?
 
  • #3
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!
 
  • #4
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.
 
  • #5
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:
  • #6
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.
 
  • #7
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.
 
  • #8
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:
  • #9
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.
 

FAQ: C Programming: acos() not accepting variable

1. Why is the acos() function not accepting my variable?

The acos() function in C is used to calculate the inverse cosine of a given value. It requires a float or double type variable as its argument. If the function is not accepting your variable, it may be because the variable is not of the correct type.

2. How can I fix the issue of acos() not accepting my variable?

To fix this issue, make sure that the variable you are passing to the acos() function is of type float or double. If the variable is of a different type, you can use a type casting function such as (float) or (double) to convert it to the correct type.

3. What is the range of values that can be passed to the acos() function?

The acos() function accepts values between -1 and 1. If a value outside of this range is passed, the function will return an error or undefined value.

4. Can I use a variable with a negative value in the acos() function?

Yes, the acos() function can accept negative values as long as they are within the range of -1 to 1. However, the output of the function may be different depending on the value of the variable.

5. Are there any special libraries or headers required for using the acos() function?

No, the acos() function is included in the standard C library and does not require any additional libraries or headers to be used. It can be used in any C program without any additional setup.

Similar threads

Replies
2
Views
1K
Replies
25
Views
1K
Replies
2
Views
1K
Replies
4
Views
1K
Replies
14
Views
32K
Replies
17
Views
640
Back
Top