473,699 Members | 2,717 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

best way to represent square root of 2

48 New Member
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
4 1282
weaknessforcats
9,208 Recognized Expert Moderator Expert
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
jinnejeevansai
48 New Member
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
9,208 Recognized Expert Moderator Expert
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
8,834 Recognized Expert Expert
  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

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

Similar topics

4
8687
by: cplusplus | last post by:
Hello, I have newbie question. I'm stuck on this current assignment. Write a program that prompts the user for two integer values, passes the values to a function where they are multiplied together and the square root of the product is returned and displayed for the user. The function should return a double. Hint: If you multiply an integer by 1.0 the result will be a double. For example:
2
3185
by: Protoman | last post by:
Can you help me? For 4, my square root funct gives 4 instead of 2; here's the code: #include <iostream> #include <cstdlib> using namespace std; template<class T> T Abs(T Nbr) {
2
3140
by: Clint Olsen | last post by:
Hello: I posted a thread on comp.programming awhile back asking about an algorithm I implemented on square root. The idea was to use the square root of a prime number as a convenient way to get a pseudorandom number generator. So, rather than calculate the square root to try to get a particular precision answer, you would calculate it to x number of bits. This particular function takes a radicand and the number of iterations as...
32
4986
by: priyam.trivedi | last post by:
Hi! Could anyone tell me how to find the square root of a number without using the sqrt function. I did it by using Newton's Formula. How can it be done by using the Binomial Theorem/Taylor Series? Is there any other way of doing it rather than using series? Thank you, Priyam
4
8481
by: sathyashrayan | last post by:
(This is not a home work question) Dear group, I want a program to find one number between a set of natural number.A program to guess a number in between a Natural number set.This should be a simple task but my mind suddenly got stuck. I am trying to implement a square root function as a practice. I am able to code for the perfect square
10
13633
by: socondc22 | last post by:
my program is trying to use the babylonian algorithm in order to find the square root... i have a number for the number to have the square root taken of and also a number to run the loop... Whenever i go to print out the answer its rounding instead of giving me the answer i need... any help?? double num1, num2, the_root; cout<< "Enter number to have square root taken of it\n"; cin>> num1; cout<< "Enter number for how...
4
8091
by: krishnai888 | last post by:
I had already asked this question long back but no one has replied to me..I hope someone replies to me because its very important for me as I am doing my internship. I am currently writing a code involving lot of matrices. At one point I need to calculate the square root of a matrix e.g. A which contains non-zero off-diagonal elements. I searched for a lot of info on net but no algorithm worked. My best bet for finding square root was to find...
3
4538
by: game2d | last post by:
how to test if a num is a square root? ex: 9 is square root 3 is not square root ///////////////////////////////////////////// x = 0; //always start at 0 y = 101010; //is this a square root? square(x, y){ if((x*x) >= y){ //not a square root return false; }
10
4854
by: Dashmi | last post by:
I tried to check the rational or irrational numbers after we take the square root. i dont get the formula. for eg. squareroot of 64 is 8 and it is rational. square root of 2 is irrational. how to check it in c++..
0
8705
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8623
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9196
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9054
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8941
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
4390
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4637
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2362
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.