Skip to content

Commit f7591f7

Browse files
committed
Add "cancellation of significant digits" example
1 parent 4b5de64 commit f7591f7

File tree

7 files changed

+109
-3
lines changed

7 files changed

+109
-3
lines changed

NumericalCalculation/BasicWithCSharp/BasicWithCSharp.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "MachineEpsilon", "MachineEp
77
EndProject
88
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "LossOfTrailingDigits", "LossOfTrailingDigits\LossOfTrailingDigits.xproj", "{528DBCCD-69CC-4599-B0F6-3CF203FC098A}"
99
EndProject
10+
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "CancellationOfSignificantDigits", "CancellationOfSignificantDigits\CancellationOfSignificantDigits.xproj", "{29AECCDE-8736-4B28-A0E3-5B3F36751283}"
11+
EndProject
1012
Global
1113
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1214
Debug|Any CPU = Debug|Any CPU
@@ -21,6 +23,10 @@ Global
2123
{528DBCCD-69CC-4599-B0F6-3CF203FC098A}.Debug|Any CPU.Build.0 = Debug|Any CPU
2224
{528DBCCD-69CC-4599-B0F6-3CF203FC098A}.Release|Any CPU.ActiveCfg = Release|Any CPU
2325
{528DBCCD-69CC-4599-B0F6-3CF203FC098A}.Release|Any CPU.Build.0 = Release|Any CPU
26+
{29AECCDE-8736-4B28-A0E3-5B3F36751283}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
27+
{29AECCDE-8736-4B28-A0E3-5B3F36751283}.Debug|Any CPU.Build.0 = Debug|Any CPU
28+
{29AECCDE-8736-4B28-A0E3-5B3F36751283}.Release|Any CPU.ActiveCfg = Release|Any CPU
29+
{29AECCDE-8736-4B28-A0E3-5B3F36751283}.Release|Any CPU.Build.0 = Release|Any CPU
2430
EndGlobalSection
2531
GlobalSection(SolutionProperties) = preSolution
2632
HideSolutionNode = FALSE
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using System;
2+
3+
namespace CancellationOfSignificantDigits
4+
{
5+
public class CancellationOfSignificantDigits
6+
{
7+
public static void Main(string[] args)
8+
{
9+
CheckCancellation();
10+
}
11+
12+
static Tuple<float, float> NormalSolver(float a, float b, float c)
13+
{
14+
float det = (float)Math.Sqrt(b * b - 4 * a * c);
15+
float x_1 = (-b + det) / 2.0f / a;
16+
float x_2 = (-b - det) / 2.0f / a;
17+
return Tuple.Create(x_1, x_2);
18+
}
19+
20+
static Tuple<float, float> CaredSolver(float a, float b, float c)
21+
{
22+
float det = (float)Math.Sqrt(b * b - 4 * a * c);
23+
float x_1, x_2;
24+
if (b > 0)
25+
{
26+
x_2 = (-b - det) / 2.0f / a;
27+
x_1 = c / (a * x_2);
28+
}
29+
else
30+
{
31+
x_1 = (-b + det) / 2.0f / a;
32+
x_2 = c / (a * x_1);
33+
}
34+
return Tuple.Create(x_1, x_2);
35+
}
36+
37+
static void CheckCancellation()
38+
{
39+
float a = 1.0f, b = -1000.0f, c = 1.0f; // x^2 - 1000x + c = 0
40+
41+
var normal = NormalSolver(a, b, c);
42+
var cared = CaredSolver(a, b, c);
43+
44+
Console.WriteLine($"normal: x_1= {normal.Item1:F8}, x_2= {normal.Item2:F8}");
45+
Console.WriteLine($"x_2 -> {(a * normal.Item2 * normal.Item2 + b * normal.Item2 + c):F8}");
46+
47+
Console.WriteLine($"cared : x_1= {cared.Item1:F8}, x_2= {cared.Item2:F8}");
48+
Console.WriteLine($"x_2 -> {(a * cared.Item2 * cared.Item2 + b * cared.Item2 + c):F8}");
49+
}
50+
}
51+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
5+
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
6+
</PropertyGroup>
7+
8+
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.Props" Condition="'$(VSToolsPath)' != ''" />
9+
<PropertyGroup Label="Globals">
10+
<ProjectGuid>29aeccde-8736-4b28-a0e3-5b3f36751283</ProjectGuid>
11+
<RootNamespace>CancellationOfSignificantDigits</RootNamespace>
12+
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">.\obj</BaseIntermediateOutputPath>
13+
<OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath>
14+
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
15+
</PropertyGroup>
16+
17+
<PropertyGroup>
18+
<SchemaVersion>2.0</SchemaVersion>
19+
</PropertyGroup>
20+
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.targets" Condition="'$(VSToolsPath)' != ''" />
21+
</Project>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"profiles": {
3+
"CancellationOfSignificantDigits": {
4+
"commandName": "Project",
5+
"commandLineArgs": "> result.txt"
6+
}
7+
}
8+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"version": "1.0.0-*",
3+
"buildOptions": {
4+
"emitEntryPoint": true
5+
},
6+
7+
"dependencies": {
8+
"Microsoft.NETCore.App": {
9+
"type": "platform",
10+
"version": "1.0.0"
11+
}
12+
},
13+
14+
"frameworks": {
15+
"netcoreapp1.0": {
16+
"imports": "dnxcore50"
17+
}
18+
}
19+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
normal: x_1= 999.99900000, x_2= 0.00100708
2+
x_2 -> -0.00707901
3+
cared : x_1= 999.99900000, x_2= 0.00100000
4+
x_2 -> 0.00000006

NumericalCalculation/BasicWithCSharp/LossOfTrailingDigits/LossOfTrailingDigits.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Threading.Tasks;
52

63
namespace LossOfTrailingDigits
74
{

0 commit comments

Comments
 (0)