| Bytes | Lang | Time | Link |
|---|---|---|---|
| nan | 230831T143247Z | bsoelch | |
| 149 | APLNARS | 250314T223028Z | Rosario |
| 027 | BQN | 250314T115333Z | panadest |
| 125 | Charcoal | 230901T164451Z | Neil |
| 172 | Perl 5 | 230901T172943Z | Kjetil S |
| 045 | R | 230831T151126Z | Dominic |
| 094 | JavaScript ES7 | 230831T170153Z | Arnauld |
| 194 | Pascal | 230831T150017Z | Kai Burg |
Built-in solutions
APL(Dyalog Unicode), 1 bytes SBCS
○
Left argument chooses function:
¯1○⍵ ⍝ asin
¯2○⍵ ⍝ acos
¯3○⍵ ⍝ atan
C, 31 Bytes
#include<math.h>
asin
acos
atan
C++, 30 Bytes
#include<cmath>
asin
acos
atan
Go
28 bytes (reals only)
import."math"
Asin
Acos
Atan
34 bytes (includes complex numbers)
import."math/cmplx"
Asin
Acos
Atan
JavaScript, 29 bytes
Math.asin
Math.acos
Math.atan
Julia, 14 bytes
asin
acos
atan
Pascal, 6 bytes (only arctan)
arctan
Unfortunately, there is no built-in function to inverse the operations of sin and cos. See the Pascal submission for a full implementation of the task.
Python, 32 bytes (reals only)
from math import*
asin
acos
atan
Python, 33 bytes (includes complex numbers)
from cmath import*
asin
acos
atan
Rust, 29 bytes
f64::asin
f64::acos
f64::atan
sclin, 14 bytes
sin_
cos_
tan_
Thunno 2, 2 bytes each
Æs # arcsin
Æc # arccos
Æt # arctan
Vyxal, 2 bytes each
∆S # Arcsine
∆C # Arccosine
∆T # Arctangent
PHP, 14 bytes
asin
acos
atan
Zsh, 36 bytes
zmodload zsh/mathfunc
asin
acos
atan
APL(NARS), 149 chars
r←a phi b;e
r←b⋄e←÷10v*⌊⎕FPC÷3.322
b←r⋄a←2÷⍨a+b⋄r←√a×b⋄→2×⍳e<∣r-b
acos←{¯1=⍵:○1⋄(√1-⍵*2)÷⍵ phi 1}
atan←{r←acos 1÷√1+⍵*2⋄⍵<0:-r⋄r}
asin←{0.5p1-acos⍵}
//12+23+31+32+32+19 =149
It use the function phi (see this link https://codegolf.stackexchange.com/a/278555/120560 for reference) for calculate acos, and from acos find atan and asin
Test:
array←0 1 ¯1 0.2 0.4 0.6 ¯0.5 ¯0.7
arr ←2 5 10 15 20 100
asin¨array
2.220446049E¯16 1.570796327 ¯1.570796327 0.2013579208 0.4115168461 0.6435011088 ¯0.5235987756 ¯0.7753974966
acos¨array
1.570796327 0 3.141592654 1.369438406 1.159279481 0.927295218 2.094395102 2.346193823
atan¨array
0 0.7853981634 ¯0.7853981634 0.1973955598 0.3805063771 0.5404195003 ¯0.463647609 ¯0.6107259644
asin¨arr
1.570796327J¯1.316957897 1.570796327J¯2.29243167 1.570796327J¯2.993222846 1.570796327J¯3.400084414 1.570796327J¯3.688253867 1.570796327J¯5.298292366
acos¨arr
0J1.316957897 0J2.29243167 0J2.993222846 0J3.400084414 0J3.688253867 0J5.298292366
atan¨arr
1.107148718 1.373400767 1.471127674 1.504228163 1.520837931 1.56079666
Note that the function acos of sys APL is agree with me
¯2○¨arr
0J1.316957897 0J2.29243167 0J2.993222846 0J3.400084414 0J3.688253867 0J5.298292366
it seems even wolframalpha https://www.wolframalpha.com/input?i=arccos%282%29
BQN, 27 bytes
•math{𝕗.Sin⁼∾𝕗.Cos⁼∾𝕗.Tan⁼}
This program returns the three inverses, using the ⁼ undo modifier, which inverts functions. One could also use the built-ins •math.Asin etc, for the same number of bytes.
Charcoal, 132 125 bytes
NθNη⊞υ¹⊞υ∧›⁰ηθ≔⁰ζ≔²εF⁵⁰«≔₂⊕Xε²δ≔¹ιFχ«≔⊘⁺ιδι≔₂×ιδδ»≔∨⎇⊕η⁼›⁰§υη⎇η›θ§υ⁰›§υ¹θ›⁰§υ¹±¹δUMυ⁺κ×××δε§υ¬λ∨λ±¹≧⁺×∨η¹×δ∕ειζ≧×₂⊕×εεθ≦⊘ε»Iζ
Try it online! Link is to verbose version of code. Takes the function as the second argument where -1=atan, 0=asin and 1=acos. Explanation: Uses CORDIC to calculate the inverse functions. Edit: Switched to single-angle CORDIC for asin and acos to save 7 bytes.
Nθ
Input the real value.
Nη
Input the function code.
⊞υ¹
Start with z[0]=1.
⊞υ∧›⁰ηθ
Start with z[1]=0, except for atan, where the input is used.
≔⁰ζ
Start with a resulting angle of 0.
≔²ε
Start with 2 as the first power of 0.5. This is needed for acos to work with negative inputs, but doesn't affect asin or atan.
F⁵⁰«
Loop 50 times, as unfortunately 10 isn't accurate enough.
≔₂⊕Xε²δ≔¹ιFχ«≔⊘⁺ιδι≔₂×ιδδ»
Start calculating the atan of the power of 0.5. (Yes, I could do the atan separately, but that would duplicate code, increasing my byte count.)
≔∨⎇⊕η⁼›⁰§υη⎇η›θ§υ⁰›§υ¹θ›⁰§υ¹±¹δ
Determine whether we are increasing or decreasing the angle.
UMυ⁺κ×××δε§υ¬λ∨λ±¹
Rotate z by the atan of the power of 0.5.
≧⁺×∨η¹×δ∕ειζ
Update the angle.
≧×₂⊕×εεθ
Compensate for rotation's scaling factor (no effect for atan). Note that in a real FPGA you would approximate the square root but that doesn't help me here.
≦⊘ε
Get the next power of 0.5.
»Iζ
Output the final angle.
Perl 5, 172 bytes
sub asin{n(@_,\&CORE::sin)}
sub acos{n(@_,\&CORE::cos)}
sub atan{n(@_,sub{sin(@_)/cos(pop)})}
sub n{$f=pop;$x=1;$x+=(&$f($x)-$_[0])/((&$f($x-1e-6)-&$f($x))*1e6)for 1..9;$x}
Uses Perl's builtin trig functions sin and cos. Since asin, acos and atan aren't builtin, they're rather found in the core lib Math::Trig, but here they're instead written in a Newton's method-ish solver in sub n.
Or 259 bytes:
sub si{my$x=pop;abs$x<1e-8?$x:2*si($x/2)*co($x/2)}
sub co{my$x=pop;abs$x<1e-8?1:co($x/2)**2-si($x/2)**2}
sub ta{&si/&co}
sub asi{n(@_,'si')}
sub aco{n(@_,'co')}
sub ata{n(@_,'ta')}
sub n{$f=pop;$x=1;$x+=(&$f($x)-$_[0])/((&$f($x-1e-6)-&$f($x))*1e6)for 1..9;$x}
...which replaces Perl's sin and cos with si and co that uses the property that sin(x) = x and cos(x) = 1 for small x and the formulas for half angles.
R, 45 bytes
\(x,f,y=0:4e8/1e8)y[match(T,(f(y)-x)^2<1e-7)]
Attempt This Online! with slightly reduced precision to enable it to run with ATO's reduced memory allowance.
Single function with arguments x=input number, f=trigonometric function to inverse (one of sin, cos or tan). Just within the 3rd-decimal place precision limit, but precision can be increased by increasing the e8 & e-7 in the code to e9 & e-8.
R, 67 bytes
\(x,f){while((f(F/T)-x)^2>1e-8)T=`if`(T-1,T+F-(F=F+1),F+(F=1));F/T}
Generalized "inverse" function, which, with arguments x=input number and f=any function, will output a non-negative real number (if it exists) corresponding to the inverse of f applied to x, within a precision of 1e-4 (in other words, the difference between f(inv(x,f)) and x is less than 1e-4).
JavaScript (ES7), 94 bytes
Probably not the golfiest way, especially for edge cases.
Returns [atan(x), acos(x), asin(x)].
x=>[(p=1.571,g=x=>1/x?x/(h=k=>k+++k+(k>>9?0:k*k*x*x/h(k)))``:p)(x),c=2*g((1-x*x)**.5/++x),p-c]
Formulas
The arctangent is approximated with the continued fractions:
$$\arctan(x)=\dfrac{x}{1+\dfrac{(1x)^2}{3+\dfrac{(2x)^2}{5+\dfrac{(3x)^2}{7+\ddots}}}}$$
We then use:
$$\arccos(-1)=\pi\\\arccos(x)=2\arctan\left(\frac{\sqrt{1-x^2}}{1+x}\right),\:-1<x\le1$$
and:
$$\arcsin(x)=\frac{\pi}{2}-\arccos(x)$$
(source: Wikipedia)
Pascal, 162 194 bytes
“Extended Pascal” (ISO standard 10206) defines that complex numbers are part of the language.
When calling one of these functions, a real argument is automatically promoted to a complex value (because the formal parameter list says the function requires a complex number).
const i=cmplx(0,1);type r=complex;function s(x:r):r;begin s:=−i*ln(sqrt(1−x*x)+i*x);end;function c(x:r):r;begin c:=−i*ln(sqrt(x*x−1)+x);end;function t(x:r):r;begin t:=i/2*ln((1−i*x)/(1+i*x));end
Ungolfed:
const
i = cmplx(0, 1);
function arcsin(protected x: complex): complex;
begin
arcsin := −i * ln(sqrt(1 − sqr(x)) + i * x);
end;
function arccos(protected x: complex): complex;
begin
arccos := −i * ln(sqrt(sqr(x) − 1) + x);
end;
function arctan(protected x: complex): complex;
begin
arctan := i / 2 * ln((1 − i * x) / (1 + i * x));
end;
Arc tangent is actually already part of the language.
Both in ISO standard 7185 (“Standard Pascal”) and ISO standard 10206 (“Extended Pascal”) the built-in function arctan is available.
Since complex numbers are defined by EP, only the latter accepts and can return complex values.