By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,961 Members | 1,332 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,961 IT Pros & Developers. It's quick & easy.

best way to represent square root of 2

P: 48
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.
Sep 3 '16 #1

✓ answered by ADezii

  1. To Minimize/Eliminate Rounding Errors, have you considered coercing the Result of Sqr(2) to either a Decimal or Currency Data type?
    Expand|Select|Wrap|Line Numbers
    1. Dim varSQR2 As Variant
    2. Dim curSQR2 As Currency
    3.  
    4. Debug.Print "*********************************************************"
    5. '*************** As a Decimal Sub-Data Type Calculation **************
    6. Debug.Print "Sqr(2) Represented as Decimal Sub-Data Type"
    7. Debug.Print "Dim varSQR2 As Variant - convert to Sub-Type Decimal"
    8. varSQR2 = Sqr(2)
    9. Debug.Print "Square Root of 2        : " & CDec(Sqr(2))
    10. Debug.Print "Square Root of 2 squared: " & CDec(Sqr(2)) * CDec(Sqr(2))
    11. Debug.Print
    12. Debug.Print "*********************************************************"
    13.  
    14. Debug.Print
    15.  
    16. '***************** As a Currency Data Type Calculation ***************
    17. Debug.Print "Sqr(2) Represented as a Currency Data Type"
    18. Debug.Print "Dim curSQR2 As Currency"
    19. curSQR2 = CCur(Sqr(2))
    20. Debug.Print "Square Root of 2        : " & curSQR2
    21. Debug.Print "Square Root of 2 squared: " & curSQR2 * curSQR2
    22. Debug.Print
    23. Debug.Print "*********************************************************"
  2. OUTPUT:
    Expand|Select|Wrap|Line Numbers
    1. *********************************************************
    2. Sqr(2) Represented as Decimal Sub-Data Type
    3. Dim varSQR2 As Variant - convert to Sub-Type Decimal
    4. Square Root of 2        : 1.4142135623731
    5. Square Root of 2 squared: 2.00000000000001400410360361
    6.  
    7. *********************************************************
    8.  
    9. Sqr(2) Represented as a Currency Data Type
    10. Dim curSQR2 As Currency
    11. Square Root of 2        : 1.4142
    12. Square Root of 2 squared: 2
    13.  
    14. *********************************************************
    15.  

Share this Question
Share on Google+
4 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
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.
Sep 3 '16 #2

P: 48
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.
Sep 3 '16 #3

weaknessforcats
Expert Mod 5K+
P: 9,197
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.
Sep 3 '16 #4

ADezii
Expert 5K+
P: 8,619
  1. To Minimize/Eliminate Rounding Errors, have you considered coercing the Result of Sqr(2) to either a Decimal or Currency Data type?
    Expand|Select|Wrap|Line Numbers
    1. Dim varSQR2 As Variant
    2. Dim curSQR2 As Currency
    3.  
    4. Debug.Print "*********************************************************"
    5. '*************** As a Decimal Sub-Data Type Calculation **************
    6. Debug.Print "Sqr(2) Represented as Decimal Sub-Data Type"
    7. Debug.Print "Dim varSQR2 As Variant - convert to Sub-Type Decimal"
    8. varSQR2 = Sqr(2)
    9. Debug.Print "Square Root of 2        : " & CDec(Sqr(2))
    10. Debug.Print "Square Root of 2 squared: " & CDec(Sqr(2)) * CDec(Sqr(2))
    11. Debug.Print
    12. Debug.Print "*********************************************************"
    13.  
    14. Debug.Print
    15.  
    16. '***************** As a Currency Data Type Calculation ***************
    17. Debug.Print "Sqr(2) Represented as a Currency Data Type"
    18. Debug.Print "Dim curSQR2 As Currency"
    19. curSQR2 = CCur(Sqr(2))
    20. Debug.Print "Square Root of 2        : " & curSQR2
    21. Debug.Print "Square Root of 2 squared: " & curSQR2 * curSQR2
    22. Debug.Print
    23. Debug.Print "*********************************************************"
  2. OUTPUT:
    Expand|Select|Wrap|Line Numbers
    1. *********************************************************
    2. Sqr(2) Represented as Decimal Sub-Data Type
    3. Dim varSQR2 As Variant - convert to Sub-Type Decimal
    4. Square Root of 2        : 1.4142135623731
    5. Square Root of 2 squared: 2.00000000000001400410360361
    6.  
    7. *********************************************************
    8.  
    9. Sqr(2) Represented as a Currency Data Type
    10. Dim curSQR2 As Currency
    11. Square Root of 2        : 1.4142
    12. Square Root of 2 squared: 2
    13.  
    14. *********************************************************
    15.  
Sep 3 '16 #5

Post your reply

Sign in to post your reply or Sign up for a free account.