Advertisement
R1bell

Untitled

Oct 5th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.97 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using System.Windows.Documents;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. using System.Windows.Navigation;
  13. using System.Windows.Shapes;
  14.  
  15. namespace SquareRoots
  16. {
  17.     /// <summary>
  18.     /// Interaction logic for MainWindow.xaml
  19.     /// </summary>
  20.     public partial class MainWindow : Window
  21.     {
  22.         public MainWindow()
  23.         {
  24.             InitializeComponent();
  25.         }
  26.  
  27.         private void calculateButton_Click(object sender, RoutedEventArgs e)
  28.         {
  29.             // Get a double from the TextBox
  30.             double numberDouble;
  31.             if (!double.TryParse(inputTextBox.Text, out numberDouble))
  32.             {
  33.                 MessageBox.Show("Please enter a double");
  34.                 return;
  35.             }
  36.            
  37.             // Check that the user has entered a positive number
  38.             if (numberDouble <= 0)
  39.             {
  40.                 MessageBox.Show("Please enter a positive number");
  41.                return;
  42.             }
  43.  
  44.             // Use the .NET Framework Math.Sqrt method
  45.             double squareRoot = Math.Sqrt(numberDouble);
  46.            
  47.             // Format the result and display it
  48.             frameworkLabel.Content = string.Format("{0} (Using the .NETFramework)", squareRoot);
  49.  
  50.             // Newton's method for calculating square roots
  51.             // Get user input as a decimal
  52.             decimal numberDecimal;
  53.             if (!decimal.TryParse(inputTextBox.Text, out numberDecimal))
  54.             {
  55.                 MessageBox.Show("Please enter a decimal");
  56.                 return;
  57.             }
  58.  
  59.             // Specify 10 to the power of -28 as the minimum delta between
  60.             // estimates. This is the minimum range supported by the decimal
  61.             // type. When the difference between 2 estimates is less than this
  62.             // value, then stop.
  63.             decimal delta = Convert.ToDecimal(Math.Pow(10, -28));
  64.  
  65.             // Take an initial guess at an answer to get started
  66.             decimal guess = numberDecimal / 2;
  67.  
  68.             // Estimate result for the first iteration
  69.             decimal result = ((numberDecimal / guess) + guess / 2);
  70.  
  71.             // While the difference between values for each current iteration
  72.             // is not less than delta, then perform another iteration to
  73.             // refine the answer.
  74.             while (Math.Abs(result - guess) > delta)
  75.             {
  76.                 // Use the result from the previous iteration
  77.                 // as the starting point
  78.                 guess = result;
  79.                 // Try again
  80.                 result = ((numberDecimal / guess) + guess) / 2;
  81.             }
  82.  
  83.             // Display the result
  84.             newtonLabel.Content = string.Format("{0} (Using Newton)", result);
  85.         }
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement