best way to represent square root of 2

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jinnejeevansai
    New Member
    • Nov 2014
    • 48

    best way to represent square root of 2

    I am writing a program which involves lot of computation and 10^-7 difference is also making solution highly different. Calculation mainly consists of multiplying number by squareroot of 2 and addition of some number. I want to represent squareroot of 2 in some efficient way than floating point representation as representing that way is making difference .Is there any other method to represent squareroot.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    All major calculators use either int or string. The problem with floating point is auto rounding and loss of accuracy. Take the square root of 2. As an int this could be 1414. Only when your user sees the value does it need to look like 1.414. Therefore when you display this value you format the int in your display function.

    Assuming three decimal places this int that has 1414 you could display by:

    int val = 1414;

    val / 1000 will get you the number of 1000's, which is 1
    val % 1000 will get the remainder of dividing by 1000, which is 414

    So your display is

    cout << val/1000 << "." << val % 1000 << endl;

    You should see 1.414.

    You would make your display logic such that you would see correct results for the range of your values.

    Comment

    • jinnejeevansai
      New Member
      • Nov 2014
      • 48

      #3
      But if i multiply sqrt(2) twice the answer is 2,in your case it is 1414*1414 = 1999396 which gives 1.999396 i want to have it exactly 2.000000 rounding off decreases precision as calculation increases and i don't want to round off.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        You can't do that. The square root of 2 is a real number. It will never be exactly 2.000000.

        The same applies to the square root of 3 and the square root of -1. You can't re[resent these exactly and in fact, you can't represent the square root of -1 at all.

        Remember your domains from transfinite arithmetic:

        counting numbers
        integers
        rational numbers of the form a/b
        real numbers with a zero imaginary component
        complex numbers.

        The square root of 2 fits in the real number set. It not a rational number because it can't by defined as an a/b value.

        Also keep in mind that 2.000000 is any real number whose value is greater than 1.999999 and less than 2.000001 so its not exact either. There may be more than one number in this range.


        Your solutions will require a pre-defined level of accuracy. That will require you to calculate even more decimal places until you are certain that any approximations will be beyond your published level of accuracy.

        A review of how to use significant figures will help. Adding decimal places does not make things more accurate. Accuracy cannot be more than the coarsest value in yur problem.

        Comment

        • ADezii
          Recognized Expert Expert
          • Apr 2006
          • 8834

          #5
          1. To Minimize/Eliminate Rounding Errors, have you considered coercing the Result of Sqr(2) to either a Decimal or Currency Data type?
            Code:
            Dim varSQR2 As Variant
            Dim curSQR2 As Currency
            
            Debug.Print "*********************************************************"
            '*************** As a Decimal Sub-Data Type Calculation **************
            Debug.Print "Sqr(2) Represented as Decimal Sub-Data Type"
            Debug.Print "Dim varSQR2 As Variant - convert to Sub-Type Decimal"
            varSQR2 = Sqr(2)
            Debug.Print "Square Root of 2        : " & CDec(Sqr(2))
            Debug.Print "Square Root of 2 squared: " & CDec(Sqr(2)) * CDec(Sqr(2))
            Debug.Print
            Debug.Print "*********************************************************"
            
            Debug.Print
            
            '***************** As a Currency Data Type Calculation ***************
            Debug.Print "Sqr(2) Represented as a Currency Data Type"
            Debug.Print "Dim curSQR2 As Currency"
            curSQR2 = CCur(Sqr(2))
            Debug.Print "Square Root of 2        : " & curSQR2
            Debug.Print "Square Root of 2 squared: " & curSQR2 * curSQR2
            Debug.Print
            Debug.Print "*********************************************************"
          2. OUTPUT:
            Code:
            *********************************************************
            Sqr(2) Represented as Decimal Sub-Data Type
            Dim varSQR2 As Variant - convert to Sub-Type Decimal
            Square Root of 2        : 1.4142135623731
            Square Root of 2 squared: 2.00000000000001400410360361
            
            *********************************************************
            
            Sqr(2) Represented as a Currency Data Type
            Dim curSQR2 As Currency
            Square Root of 2        : 1.4142
            Square Root of 2 squared: 2
            
            *********************************************************

          Comment

          Working...