g | x | w | all
Bytes Lang Time Link
026C gcc250219T030427Zceilingc
063APLNARS250215T102458ZRosario
139Axiom170425T093624Zuser5898
016Perl 6160309T230830ZHotkeys
021TI84 BASIC160309T224846ZSuperJed
103C#140307T211003Zdavidsbr
nan140307T162736ZMukul Ku
069Python140307T102854ZAbhijit
070Python140307T213445ZWrzlprmf
nan140307T120544ZChristop
081Java 108 /140307T195101ZMark Jer
070C140307T175559Zorion
056Javascript140307T144702ZMichael
077Javascript140307T104159ZJulien C
021APL140307T104412Zmniip
080R140307T105020Zplannapu
035MATLAB140307T104218Zmmumboss

C (gcc), 55 27 26 bytes

#define S(x)-1i*cexp(1i*x)

-28 thanks to @jdt

Try it online!

Permitting exp appears to be a very large loophole.

APL(NARS), 63 chars

r←s w;d;e;q;m
r←d←w⋄e←÷9*9⋄q←0,1⋄m←-w×w
r+←d×←m÷×/q+←2⋄→2×⍳e<∣d

//14+26+23=63

I think it is 8 significative digits...

test:

  s 1p1÷2
1
  1○ 1p1÷2
1
  s 1p1÷4
0.7071067812
  1○ 1p1÷4
0.7071067812

Axiom, 139 bytes

s(x:Float):Complex Float==(digits()>20=>%i;x>2*%pi=>s(x-2*%pi);x< -2*%pi=>s(x+2*%pi);imag(reduce(+,[(%i*x)^k/factorial(k)for k in 0..40])))

I see this formula in the other solutions and I inplemented it in Axiom... The formula i follow is this:

                               +oo
                               ----
                               \      (i*x)^k
sin(x)=ImPart(e^(i*x))=imPart( /     --------  )
                               ----k     k!
                                 0

ungolf

sin1(x:Float):Complex Float==
              digits()>20=>%i
              x>  2*%pi  =>sin1(x-2*%pi)
              x< -2*%pi  =>sin1(x+2*%pi)
              imag(reduce(+,[(%i*x)^k/factorial(k)for k in 0..40]))

there is some problem for the last 2 digits each number; some test cases:

(79) -> [-23, numeric(sin(-23)), s(-23) ]
   (79)  [- 23.0,0.8462204041 7517063524,0.8462204041 7517063514]
                                                 Type: List Complex Float
(80) -> [ 23, numeric(sin( 23)), s( 23) ]
   (80)  [23.0,- 0.8462204041 7517063524,- 0.8462204041 7517063536]
                                                 Type: List Complex Float
(81) -> [[x/2,numeric(sin(x/2)),s(x/2)] for x in 0..(7*2)]
   (81)
                  1
   [[0,0.0,0.0], [-,0.4794255386 0420300027,0.4794255386 0420300027],
                  2
    [1,0.8414709848 0789650665,0.8414709848 0789650665],
     3
    [-,0.9974949866 0405443094,0.9974949866 0405443093],
     2
    [2,0.9092974268 256816954,0.9092974268 256816954],
     5
    [-,0.5984721441 0395649405,0.5984721441 0395649405],
     2
    [3,0.1411200080 598672221,0.1411200080 5986722208],
     7
    [-,- 0.3507832276 8961984812,- 0.3507832276 8961984817],
     2
    [4,- 0.7568024953 0792825138,- 0.7568024953 0792825139],
     9
    [-,- 0.9775301176 6509705539,- 0.9775301176 6509705545],
     2
    [5,- 0.9589242746 6313846889,- 0.9589242746 6313846888],
     11
    [--,- 0.7055403255 7039190623,- 0.7055403255 7039190626],
      2
    [6,- 0.2794154981 9892587281,- 0.2794154981 9892587545],
     13
    [--,0.2151199880 878155243,0.2151199880 878155243],
      2
    [7,0.6569865987 187890904,0.6569865987 1878909041]]
                                                      Type: List List Any

Perl 6, 17 16 bytes

{(e**(i*$_)).im}

TI-84 BASIC, 21 bytes

(e^(iAns)-e^(⁻iAns))/2i

C# - 115 103

This code uses an approximation of Maclaurin series calculation of the sine of x, and is accurate to 7 decimal places.

float x=0;for(int i=1;i<96;i+=2){float n=i,d=1,p=a;while(n>1){d*=n;n--;p*=a;}x+=((i+1)%4==0?-1:1)*p/d;}

Where a is the input in radians.

Here's the code with whitespaces:

float x = 0; for (int i = 1; i < 96; i += 2)
{
    float n = i, d = 1, p = a;
    while (n > 1)
    {
        d *= n;
        n--;
        p *= a;
    }
    x += ((i+1)%4==0?-1:1)*p / d;
}

C++ 80 68(excluding white-spaces + edit)

GOLFED

float S(float x){float z=x*x,y=x*(5040-(840+(42-z)*z)*z)/5040;return y;}  

UNGOLFED

both versions are different so that, it is easy to get output.

#include<iostream>
int main()
{
    float x,y,z;
    for(;;)
    {
        std::cin>>x;
        z=x*x;
        y=(x*(5040-840*z+42*z-z*z*z))/5040;
        std::cout<<y<<'\n';
    }
}  

Output

1.047197 ((pie)/3)
0.865096 (sin((pie)/3) = 1.73201/2 = 0.8660)

Python 83 69

Based on Bhaskara I's sine approximation formula

Implementation

def f(x):p=3.14;x,s=x%p,(4,-4,4)[int(x/p)];return s/(12.34/x/(p-x)-1)

Ungolfed

def apxsine(radian):
    pi = 3.14 # Approximate to 2 decimal places
    sign = (4,-4,4)[radian % pi]
    return sign / (12.34/radian/(pi - radian) - 1))

Deduction

enter image description here

Demo

>>> from math import pi, sin
>>> any(round(abs(f(e) - sin(e)), 2) for e in (e/180.*pi for e in range(0,360)))
False

JUst realized that if I would have opted for Euler's formula route, the code would be shorter (49 characters). But then it uses Complex numbers and am not sure if the call to the real method would be valid in the current question context.

f=lambda x:((2.72**(x*1j)-2.72**(-x*1j))/2j).real

Python, 70 bytes

Using sin(x) = Im(exp(ix)) and the series for the exponential function:

def s(x):
    s,f=0,1
    for i in range(1,50):f*=1j*x/i;s+=f
    return s.imag

Note that imag is not a function here, strictly speaking.

Ungolfed:

def s(x):
    s = 0
    f = 1
    for i in range(1,50):
        f *= 1j*x/i
        s += f
    return s.imag

Haskell

import Prelude hiding (sin)
import Data.Complex
sin x=(\(_:+y) -> y) $ exp (0 :+ x)

Uses Euler's formula. (y :+ z)=y+zi I think.

Java - 108 / 81

float s(float p){for(float q=p-=3.14,d=p=-p,i=1,m=1;i<11;)p+=(q*=d*d)/(m*=++i*++i)*(((int)i&2)-1);return p;}

Without the function declaration, it's just doing p = sin(p):

for(float q=p-=3.14,d=p=-p,i=1,m=1;i<11;)p+=(q*=d*d)/(m*=++i*++i)*(((int)i&2)-1);

The error is max ±0.00201 in the range 0..2π, much worse in either direction.

Ungolfed:

float s(float p) {
    for (float q = p -= 3.14, d = p = -p, i = 1, m = 1; i < 11;)
        p += (q *= d * d) / (m *= ++i * ++i) * (((int)i & 2) - 1);
    return p;
}

Unobfuscated:

float sin(double ph) {
    float [] dividers = {-1, 6, -120, 5040, -362880, 39916800L};
    ph -= Math.PI;
    double d = 0;
    for (int i = 0; i < 6; i++)
        d += Math.pow(ph, i * 2 + 1) / dividers[i];
    return d;
}

C 70

This one is not only golf but also useful in microcontrollers without floating point instructions or math libraries:

float S(float a){float x,y=1,h=1e-4;while(x+=h*(y-=h*x),(a-=h)>0);return x;}

Of course, the majority of the characters is taken by these annoyingly long type declarations and return. If the function declaration doesn't count, it's much shorter. If we take the value by pointer and overwrite it (removing the return statement), we get 71 characters.

S(float*x){float a=*x,y=1,h=1e-4;*x=0;while(*x+=h*(y-=h**x),(a-=h)>0);}

However, it complains about default return value.

EDIT: 70, for is shorter:

S(float*x){float a=*x,y=1,h=1e-4;*x=0;for(;(a-=h)>0;*x+=h*(y-=h**x));}

EDIT2: forgot ungolfed version

float fake_sin(float a){
   float x=0,y=1;
   float h=1e-4;//adjust for accuracy
   while(a>0){
      y-=h*x;
      x+=h*y;
      a-=h;
   }
   return x;//return y for cosine or y/x for tangent
}

It takes arbitrary positive angle, but doesn't work for negative.

Javascript (56)

j=x=+prompt(r=0);for(i=1;i<99;j*=x/++i)r+=i&1?i&2?-j:j:0

Taylor series used here.
x is the input value, r is the result, j contains x^i/i!
The result is output in console.

x=prompt();
r=0;
j=1;
for(i=1;i<99;i++) {
  j=j*x/i;
  if (i%2) {
    if (i%4 == 1) r+=j;
    else r-=j;
  }
}
alert(r);

If evaluation is needed for input (for example 3.141592653 / 2) :

j=x=eval(prompt(r=0));for(i=1;i<99;j*=x/++i)r+=i&1?i&2?-j:j:0

Javascript 114 77

Using Taylor Series expansion at 0:

function s(y){
    var p=2*Math.PI,
        // z = sign (y)
        z=(y>0?1:-1),
        // x = (abs(y) mod 2 PI - PI) * sign(y) => put x in [-PI, PI] range
        x=-z*((z*y)%p-p/2),
        // u = x^2
        u=x*x; 
        // return Taylor Expansion
        return x*(1-(1-(1-(1-u/72)*u/42)*u/20)*u/6);
}

Golfed (114):

function s(y){var z=(y>0?1:-1),p=2*Math.PI,x=-z*((z*y)%p-p/2),u=x*x; return x*(1-(1-(1-(1-u/72)*u/42)*u/20)*u/6);}

Edit

"Improved" for y in [0,2PI], golfed (77) :

function s(y){var x=3.14-y,u=x*x;return x*(1-(1-(1-(1-u/72)*u/42)*u/20)*u/6)}

Another version for more precision (varying k) :

function s(y){
    var p=2*Math.PI,
        // z = sign (y)
        z=(y>0?1:-1),
        // x = (abs(y) mod 2 PI - PI) * sign(y) => put x in [-PI, PI] range
        x=-z*((z*y)%p-p/2),
        // u = x^2
        u=x*x,
        // k = 2* expansion level +1
        k=9,
        // r = 1 initial value
        r = 1; 

        // Taylor Expansion
        // while k > 1
        for(;k>1;) { 
            r= 1 - r*u /k--/k--; 
        }
        return x*r;
 }

Golfed (114):

function s(y){var z=(y>0?1:-1),p=2*Math.PI,x=-z*((z*y)%p-p/2),u=x*x,k=9,r=1;for(;k>1;)r=1-r*u/k--/k--;return x*r;}

APL - 21

{|{.5×⍵-+⍵}0J1*⍵÷○.5}

This uses the fact that sin pi*x/2 = Im[(-1)^x].

Example test on ngn APL

Previous answer: 26

{-/((×\9⍴⍵)÷×\1+⍳9)[2×⍳5]}

This uses the Taylor/Maclaurin series up to 9th term.

Example test on ngn APL

Note about operators: only operators used were /\⍴⍳ which don't have anything to do with arithmetic, and are mostly list manipulation tools, which is multiply by pi, and +-×÷* which are permitted by the rules

R, 80 chars

f=function(x){a=seq(1,99,2);sum((-1)^(0:49)*(x^a)/sapply(mapply(`:`,1,a),prod))}

Uses Taylor approximation at an order of 99. sapply and mapply are just functions to vectorize the operation, while sum and prod are just vectorized + and *.

> b=seq(0,2*pi,1e5)
> all((f(b)-sin(b))<.01)
[1] TRUE

If the *apply functions are a problem, here is a solution at 91 characters:

f=function(x){a=0:100;b=c();for(i in 1:50)b[i]=(-1)^(i-1)*(x^a[2*i])/prod(1:a[2*i]);sum(b)}

MATLAB 35

Too bad e is not a constant in Matlab. That would have saved me 10 characters. I will see if I can come up with a solution for this.

@(x)(2.7183^(x*i)-2.7183^(-x*i))/2i