Skip to main content
replaced http://chart.googleapis.com/ with https://chart.googleapis.com/
Source Link
added 218 characters in body
Source Link
primo
  • 33.5k
  • 5
  • 61
  • 139

Perl - 7472 bytes

$~=<>=~$"+$'*2;$_=1-$_*$`/$~--/$~*$`for($,,$;)x$';printf'%.8fx$';printf'%f
'x2%f',$`*$,,$;

Or, counting command line options as 1 byte each, in 7270 bytes:

#!perl -n
$-=/ /+$'*2;$_=1-$_*$`/$---/$-*$`for($,,$;)x$';printf'%.8fx$';printf'%f
'x2%f',$`*$,,$;

Or, if you'll allow me Perl 5.8, in 6563 bytes:

#!perl -p
$.+=$'<</ /;$_=1-$_*$`/$.--/$.*$`for($_=$#='%.8f$_=$#='%f
',$\)x$';$_*=$`

but why would you.

Edit: Compliance with the new rules. %f rounds to 6 places by default, how convenient!

<> =~ m/ /;          # read one line from stdin, match a space
                     # prematch ($`) is now x, postmatch ($') is now n
($x, $n) = ($`, $'); # reassign, for clarity
$i = 2*$n + 1;       # counting variable (denominators)

for (($s, $c)x$n) {  # iterate over $s and $c, n times each
  # compute the next term of the recursive expression
  # note: inside this loop $_ is not the _value_
  # of $s and $c alternately, it _is_ $s and $c

  $_ = 1 - $_ * $x**2 / $i-- / $i;
}

# formated output
printf("%.8f\n%.8f""%f\n%f", $x*$s, $c);
$ echo 5 3 | perl sin-cos.pl
10.20833333208333
14.54166667541667

$ echo 98.26555 5013 | perl sin-cos.pl
0.16403330765431
-0.98645480641092

$ echo 9.26 10 | perl sin-cos.pl
-3.154677
-8.404354

$ echo 6.54 12 | perl sin-cos.pl
0.25398611253986
0.96714730967147

$ echo 5 1 | perl sin-cos.pl
5.00000000000000
1.00000000000000

$ echo 20 20 | perl sin-cos.pl
-5364.411846
-10898.499385

Perl - 74 bytes

$~=<>=~$"+$'*2;$_=1-$_*$`/$~--/$~*$`for($,,$;)x$';printf'%.8f
'x2,$`*$,,$;

Or, counting command line options as 1 byte each, in 72 bytes:

#!perl -n
$-=/ /+$'*2;$_=1-$_*$`/$---/$-*$`for($,,$;)x$';printf'%.8f
'x2,$`*$,,$;

Or, if you'll allow me Perl 5.8, in 65 bytes:

#!perl -p
$.+=$'<</ /;$_=1-$_*$`/$.--/$.*$`for($_=$#='%.8f
',$\)x$';$_*=$`

but why would you.

<> =~ m/ /;          # read one line from stdin, match a space
                     # prematch ($`) is now x, postmatch ($') is now n
($x, $n) = ($`, $'); # reassign, for clarity
$i = 2*$n + 1;       # counting variable (denominators)

for (($s, $c)x$n) {  # iterate over $s and $c, n times each
  # compute the next term of the recursive expression
  # note: inside this loop $_ is not the _value_
  # of $s and $c alternately, it _is_ $s and $c

  $_ = 1 - $_ * $x**2 / $i-- / $i;
}

# formated output
printf("%.8f\n%.8f", $x*$s, $c);
$ echo 5 3 | perl sin-cos.pl
10.20833333
14.54166667

$ echo 9.26 50 | perl sin-cos.pl
0.16403330
-0.98645480

$ echo 6.54 12 | perl sin-cos.pl
0.25398611
0.96714730

$ echo 5 1 | perl sin-cos.pl
5.00000000
1.00000000

Perl - 72 bytes

$~=<>=~$"+$'*2;$_=1-$_*$`/$~--/$~*$`for($,,$;)x$';printf'%f
%f',$`*$,,$;

Or, counting command line options as 1 byte each, in 70 bytes:

#!perl -n
$-=/ /+$'*2;$_=1-$_*$`/$---/$-*$`for($,,$;)x$';printf'%f
%f',$`*$,,$;

Or, if you'll allow me Perl 5.8, in 63 bytes:

#!perl -p
$.+=$'<</ /;$_=1-$_*$`/$.--/$.*$`for($_=$#='%f
',$\)x$';$_*=$`

but why would you.

Edit: Compliance with the new rules. %f rounds to 6 places by default, how convenient!

<> =~ m/ /;          # read one line from stdin, match a space
                     # prematch ($`) is now x, postmatch ($') is now n
($x, $n) = ($`, $'); # reassign, for clarity
$i = 2*$n + 1;       # counting variable (denominators)

for (($s, $c)x$n) {  # iterate over $s and $c, n times each
  # compute the next term of the recursive expression
  # note: inside this loop $_ is not the _value_
  # of $s and $c alternately, it _is_ $s and $c

  $_ = 1 - $_ * $x**2 / $i-- / $i;
}

# formated output
printf("%f\n%f", $x*$s, $c);
$ echo 5 3 | perl sin-cos.pl
10.208333
14.541667

$ echo 8.555 13 | perl sin-cos.pl
0.765431
-0.641092

$ echo 9.26 10 | perl sin-cos.pl
-3.154677
-8.404354

$ echo 6.54 12 | perl sin-cos.pl
0.253986
0.967147

$ echo 5 1 | perl sin-cos.pl
5.000000
1.000000

$ echo 20 20 | perl sin-cos.pl
-5364.411846
-10898.499385
obscurity
Source Link
primo
  • 33.5k
  • 5
  • 61
  • 139
$~=<>=~$"+$'*2;$_=1-$_*$`/$~/--/$~*$`for($,,$;)x$';printf'%.8f
'x2,$`*$,,$;
#!perl -n
$~=$-=/ /+$'*2;$_=1-$_*$`/$~/$--$~*$`for-/$-*$`for($,,$;)x$';printf'%.8f
'x2,$`*$,,$;
#!perl -p
$%=$.+=$'<</ /+$'*2;$_=1;$_=1-$_*$`/$%$.--/$%*$`for$.*$`for($_=$#='%.8f
',$\)x$';$_*=$`
$_ = <>;<> =~ m/ /;          # read one line from stdin
m/ /;                #, match a space
                     # prematch ($`) is now x, postmatch ($') is now n
($x, $n) = ($`, $'); # reassign, for clarity
$i = 2*$n + 1;       # counting variable (denominators)

for (($s, $c)x$n) {  # iterate over $s and $c, n times each
  # compute the next term of the recursive expression
  # note: inside this loop $_ is not the _value_
  # of $s and $c alternately, it _is_ $s and $c

  $_ = 1 - $_ * $x**2 / $i-- / --$i;
}

# formated output
printf("%.8f\n%.8f", $x*$s, $c);
$~=<>=~$"+$'*2;$_=1-$_*$`/$~/--$~*$`for($,,$;)x$';printf'%.8f
'x2,$`*$,,$;
#!perl -n
$~=/ /+$'*2;$_=1-$_*$`/$~/--$~*$`for($,,$;)x$';printf'%.8f
'x2,$`*$,,$;
#!perl -p
$%=/ /+$'*2;$_=1-$_*$`/$%--/$%*$`for($_=$#='%.8f
',$\)x$';$_*=$`
$_ = <>;             # read one line from stdin
m/ /;                # match a space
                     # prematch ($`) is now x, postmatch ($') is now n
($x, $n) = ($`, $'); # reassign, for clarity
$i = 2*$n + 1;       # counting variable (denominators)

for (($s, $c)x$n) {  # iterate over $s and $c, n times each
  # compute the next term of the recursive expression
  # note: inside this loop $_ is not the _value_
  # of $s and $c alternately, it _is_ $s and $c

  $_ = 1 - $_ * $x**2 / $i / --$i;
}

# formated output
printf("%.8f\n%.8f", $x*$s, $c);
$~=<>=~$"+$'*2;$_=1-$_*$`/$~--/$~*$`for($,,$;)x$';printf'%.8f
'x2,$`*$,,$;
#!perl -n
$-=/ /+$'*2;$_=1-$_*$`/$---/$-*$`for($,,$;)x$';printf'%.8f
'x2,$`*$,,$;
#!perl -p
$.+=$'<</ /;$_=1-$_*$`/$.--/$.*$`for($_=$#='%.8f
',$\)x$';$_*=$`
<> =~ m/ /;          # read one line from stdin, match a space
                     # prematch ($`) is now x, postmatch ($') is now n
($x, $n) = ($`, $'); # reassign, for clarity
$i = 2*$n + 1;       # counting variable (denominators)

for (($s, $c)x$n) {  # iterate over $s and $c, n times each
  # compute the next term of the recursive expression
  # note: inside this loop $_ is not the _value_
  # of $s and $c alternately, it _is_ $s and $c

  $_ = 1 - $_ * $x**2 / $i-- / $i;
}

# formated output
printf("%.8f\n%.8f", $x*$s, $c);
added 170 characters in body
Source Link
primo
  • 33.5k
  • 5
  • 61
  • 139
Loading
added 233 characters in body
Source Link
primo
  • 33.5k
  • 5
  • 61
  • 139
Loading
get my maths straight
Source Link
primo
  • 33.5k
  • 5
  • 61
  • 139
Loading
added 1937 characters in body
Source Link
primo
  • 33.5k
  • 5
  • 61
  • 139
Loading
deleted 6 characters in body
Source Link
primo
  • 33.5k
  • 5
  • 61
  • 139
Loading
$~=<>=~$
Source Link
primo
  • 33.5k
  • 5
  • 61
  • 139
Loading
deleted 3 characters in body
Source Link
primo
  • 33.5k
  • 5
  • 61
  • 139
Loading
added 170 characters in body
Source Link
primo
  • 33.5k
  • 5
  • 61
  • 139
Loading
deleted 9 characters in body
Source Link
primo
  • 33.5k
  • 5
  • 61
  • 139
Loading
Source Link
primo
  • 33.5k
  • 5
  • 61
  • 139
Loading