g | x | w | all
Bytes Lang Time Link
042JavaScript Node.js240724T031644Zl4m2
056Setanta240724T000137Zbb94
033Raku Perl 6 rakudo240723T235309Zbb94
019JCram240723T133842ZKamila S
068Scala230603T063056Z138 Aspe
050Haskell161214T043851Ztheideas
028ised161129T200225Zorion
364Axiom161128T234451Zuser5898
091C++ 11161027T102812ZKarl Nap
028Mathematica161027T132041Zuser5863
017MATL161027T125217ZLuis Men
052Mathematica161027T112552ZLegionMa
099C161027T100031ZKarl Nap
078Python 2161027T093649ZKarl Nap
034Haskell161027T052945Zxnor

JavaScript (Node.js), 42 bytes

m=>(g=x=>Math.sin(x+=5e-4)<-m*x?x:g(x))(0)

Try it online!

Notice that in (0,inf), sin(x)/x starts from 1 to minimum, then oscillate weaker, so searching will only happen in its decreasing range

Setanta, 58 56 bytes

gniomh(m){x:=1le i idir(0,99)x+=m*x+sin@mata(x)toradh x}

Try it on try-setanta.ie

Raku (Perl 6) (rakudo), 33 bytes

Port of xnor’s Haskell answer.

{(0,1e-3...{-$^a*$_>$a.sin})/1e3}

Attempt This Online!

JCram, 19 bytes (SBCS)

≥MA⍡t⍙ⒹⒶ⍫π⁶2⌹Ⓕ∄⍶⌿⍡⏚

Encodes the following ES6 program:

f=(n,k=1)=>Math.abs(Math.sin(k)+n*k)>1e-5?f(n,Math.sin(k)+n*k+k):k

Inspired by the recursive Python answer.

Scala, 68 bytes

Modified from @xnor's Haskell answer.


Golfed version. Try it online!

m=>Stream.iterate(0.0)(_+1e-3).dropWhile(x=>math.sin(x)>= -x*m).head

Ungolfed version. Try it online!

object Main {
  def main(args: Array[String]): Unit = {
    println(f(-0.2))
    println(f(-0.1))
    println(f(0.0))
    println(f(0.1))
    println(f(0.2))
  }

  def f(m: Double): Double = {
    def until(condition: Double => Boolean, action: Double => Double, x: Double): Double = {
      if (condition(x)) x
      else until(condition, action, action(x))
    }

    until(x => Math.sin(x) < -m * x, _ + 1e-3, 0)
  }
}

Haskell, 50 bytes

I just learned about newton's method in my calc class, so here goes in haskell using newton's method.

f m=foldl(\x _->x-(sin x+m*x)/(cos x+m))0[1..10]

ised: 32 28 bytes

Using Newton's iteration starting from π:

{:x-{sinx+$1*x}/{cosx+$1}:}:::pi

The argument is passed in $1, which can be taken from a file, like this:

ised --l inputfile.txt 'code'

A bit less stable, but shorter version:

{:{x-tanx}/{1+$1/cosx}:}:::pi

Sometimes it throws iteration limit warnings but the accuracy seems fine considering the conditions.

Unicode version (same bytecount):

{λ{x-tanx}/{1+$1/cosx}}∙π

Starting from 4 cuts another byte and seems to converge to the same values

{λ{x-tanx}/{1+$1/cosx}}∙4

Axiom, 364 bytes

bisezione(f,a,b)==(fa:=f(a);fb:=f(b);a>b or fa*fb>0=>"fail";e:=1/(10**(digits()-3));x1:=a;v:=x2:=b;i:=1;y:=f(v);if(abs(y)>e)then repeat(t:=(x2-x1)/2.0;v:=x1+t;y:=f(v);i:=i+1;if i>999 or t<=e or abs(y)<e then break;if fb*y<0 then(x1:=v;fa:=y)else if fa*y<0 then(x2:=v;fb:=y)else break);i>999 or abs(y)>e=>"fail";v)
macro g(m) == bisezione(x+->(sin(x)+m*x), 0.1, 4.3)

ungolf

bisezione(f,a,b)==
    fa:=f(a);fb:=f(b)
    a>b or fa*fb>0=>"fail"
    e:=1/(10**(digits()-3))
    x1:=a;v:=x2:=b;i:=1;y:=f(v)
    if(abs(y)>e) then
      repeat
        t:=(x2-x1)/2.0;v:=x1+t;y:=f(v);i:=i+1
        if i>999 or t<=e or abs(y)<e then break
        if      fb*y<0 then(x1:=v;fa:=y)
        else if fa*y<0 then(x2:=v;fb:=y)
        else break
    i>999 or abs(y)>e=>"fail"
    v

macro g(m) == bisezione(x+->(sin(x)+m*x), 0.1, 4.3)

results

(3) -> g(0.2)
   AXIOM will attempt to step through and interpret the code.
   (3)  4.1046198505 579058527
                                                              Type: Float
(4) -> g(-0.1)
   (4)  2.8523418944 500916556
                                                              Type: Float
(5) -> g(-0.25)
   (5)  2.4745767873 698290098
                                                              Type: Float

C++ 11, 92 91 bytes

-1 byte for using #import

#import<cmath>
using F=float;F f(F m,F x=1){F y=sin(x)+m*x;return fabs(y)>1e-4?f(m,x+y):x;}

Mathematica, 28 bytes

x/.FindRoot[Sinc@x+#,{x,1}]&

Searches for a numerical root from the initial guess x=1. Test cases:

% /@ {-0.25, -0.1, 0.2}
(* {2.47458, 2.85234, 4.10462} *)

MATL, 17 bytes

`@2e3/tY,wG_*>}4M

This uses linear search on the positive real axis, so it is slow. All test cases end within 1 minute in the online compiler.

Try it online!

Explanation

`         % Do...while
  @       %   Push iteration index, starting at 1
  2e3/    %   Divide by 2000
  t       %   Duplicate
  Y,      %   Sine
  w       %   Swap
  G_*     %   Multiply by minus the input
  >       %   Does the sine exceed that? If so, next iteration
}         % Finally (execute after last iteration, before exiting loop)
   4M     %   Push input of sine function again
          % Implicit end
          % Implicit display

Mathematica, 52 bytes

NSolve[Sin@x==-x#,x,Reals][[;;,1,2]]~DeleteCases~0.&

Anonymous function. Takes a number as input, and returns a list of numbers as output. Just uses NSolve to solve the approximate equation.

C, 99 bytes

#include<math.h>
float f(float m){float x=1,y;do{x=(y=sin(x)+m*x)+x;}while(fabs(y)>1e-4);return x;}

ungolfed:

#include<math.h>
float f(float m){
 float x=1,y;
 do{x=(y=sin(x)+m*x)+x;}while(fabs(y)>1e-4);
 return x;
}

Python 2, 81 78 bytes

Fixpoint iteration

As recursive lambda

from math import*
f=lambda m,x=1:abs(sin(x)+m*x)>1e-4and f(m,sin(x)+m*x+x)or x

As loop (81 bytes):

from math import*
m=input()
x=1
while abs(sin(x)+m*x)>1e-4:x=sin(x)+m*x+x
print x

Haskell, 34 bytes

f m=until(\x->sin x< -m*x)(+1e-3)0

Counts x up from 0 by 0.001 until sin(x)< -m*x.

Ouput examples

f -0.2 ->   2.595999999999825
f -0.1 ->   2.852999999999797
f  0.0 ->   3.141999999999765
f  0.1 ->   3.4999999999997256
f  0.2 ->   4.1049999999997056