-
Notifications
You must be signed in to change notification settings - Fork 272
/
NewtR.cs
98 lines (83 loc) · 2.24 KB
/
NewtR.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
//
// Newton's method adapted from Conte and De Boor
using BenchmarkDotNet.Attributes;
using System.Runtime.CompilerServices;
using MicroBenchmarks;
namespace Benchstone.BenchF
{
[BenchmarkCategory(Categories.Runtime, Categories.Benchstones, Categories.JIT, Categories.BenchF)]
public class NewtR
{
public const int Iterations = 80000000;
[MethodImpl(MethodImplOptions.NoInlining)]
private static void Escape(object _) { }
[Benchmark(Description = nameof(NewtR))]
public bool Test()
{
int idbg, iflag;
double x0, fx0;
iflag = 0;
idbg = 0;
fx0 = 0.0;
x0 = 1.0;
for (int i = 1; i <= Iterations; i++)
{
Inner(ref x0, 0.0000001, 0.0000001, 10, out iflag);
if (iflag > 1)
{
goto L888;
}
fx0 = FF(x0);
if (idbg != 0)
{
System.Console.WriteLine(" THE ROOT IS {0:e} F(ROOT) := {1:E}\n", x0, fx0);
}
L888:
{
}
}
// Escape iflag, x0, and fx0 so that they appear live
Escape(iflag);
Escape(x0);
Escape(fx0);
return true;
}
private static double FF(double x)
{
return (-1.0 - ((x) * (1.0 - ((x) * (x)))));
}
private static double FFDer(double x)
{
return (3.0 * (x) * (x) - 1.0);
}
private static void Inner(ref double x0, double xtol, double ftol, int ntol, out int iflag)
{
double fx0, deriv, deltax;
iflag = 0;
for (int n = 1; n <= ntol; n++)
{
fx0 = FF(x0);
if (System.Math.Abs(fx0) < ftol)
{
goto L999;
}
deriv = FFDer(x0);
if (deriv == 0.0)
{
goto L999;
}
deltax = fx0 / deriv;
x0 = x0 - deltax;
if (System.Math.Abs(deltax) < xtol)
{
goto L999;
}
}
L999:
iflag = 2;
}
}
}