Jump to content

Numerical Input to Computer


khaled

Recommended Posts

when we want to tell the computer "for example" to do

calculation like calculating the circle's area,

 

we may give the following instruction in different versions,

 

A: Result = [math]r^2{}[/math] * 3.14

B: Result = [math]r^2{}[/math] * 3.1416

C: Result = [math]r^2{}[/math] * 3.14159265

 

example, input: r = 100000

 

A: Result = 31400000000

B: Result = 31416000000

C: Result = 31415926500

 

but yet when we go with real numbers, it start to be worse ...

so the best solution would to be,

let the machine shows you the best it can do !

this means, let the computer do the division ...

 

and this would be my version of Circle Area Calc .. in C,

long double Circle_Area ( double r )
{
     double pi = (double)22.0/(double)7.0;

     long double Result =  r * r * pi;

     return Result;
}

Link to comment
Share on other sites

when you're using 22/7 as an approximation of pi then there isn't much need to store the variable as a double.

 

most programming languages have some form of math library that will far exceed that apporximation.

 

alternatively, you could get the program to calculate pi to an arbitrary number of digits before performing the calculation, but a precomputed value for pi would be the most efficient.

Link to comment
Share on other sites

also, let's not forget the possibility of inserting the pi value as fraction into the formula,

 

for example,

 

formula: (X + 2[math]X^2{}[/math]) = r

 

Result = [math]r^2{}[/math] * pi

 

A: r^2 * (22/7)

 

B: (X + 2 X^2) * (X + 2 X^2) * (22/7) = (X^2 + 4 X^3 + 4 X^4) * (22/7)

....... = (22 X^2 / 7) + (88 X^3 / 7) + (88 X^4 / 7)

 

i guess that such process can give better solutions to particular formula

Link to comment
Share on other sites

If, for no apparent reason you set [imath]r=x+2x^2[/imath].

 

Then between [imath][ r^2 \pi ][/imath] and [imath][ x^2 \pi + 4x^3 \pi + 4 x^4 \pi ][/imath], the latter is obviously going to have a larger loss of accuracy due to involving at least 10 more FLOPs not to mention the additional loss from calculating [imath]x[/imath] in the first place.

 

As i_a mentioned, you can use arbitrary precision for a result at least as accurate as your initial value for the radius and there's certainly no need to use such terrible approximations.

Link to comment
Share on other sites

you may not have suggested it but it's what you're using and in your first post you definitely implied it was better than using say 3.14159265. despite it being significantly worse.

 

then you bring in some new X variable and fail to explain what it represents or even why it is useful.

Link to comment
Share on other sites

You'd be far better of defining pi as a constant at compile time. First of all you can get exact precision to the degree that 'double' (or long double if you prefer) offers.

Second, calculating a constant value every time the function is called, is just a waste of power. (Hopefully the compiler will fix this for you.)

Link to comment
Share on other sites

  • 2 months later...

i heared it's possible, given an expression, we can embed pi fraction 22/7,

i know if you write (22/7)(double) in C it will return 3.14.. approximation

but if you embed the fraction in a dynamic expression in a function,

it will give more flexibility ...

 

like this,

 

first version:: given X: return (22/7) * (1/X) * 100,000,000

X=3 .. = 3.14159 / 3 = 1.04719666 * 100,000,000

.. = 104,719,666

 

2nd version:: given X: return 22/(7 * X) * 100,000,000

X=3 .. = 22 / 7*3 = 22/21 = 1.04761904 * 100,000,000

.. = 104,761,904

 

difference = 42,238

 

if this was a Money Transfer, 42,238$ is gone because of a miscalculation!

Link to comment
Share on other sites

i heared it's possible, given an expression, we can embed pi fraction 22/7,

i know if you write (22/7)(double) in C it will return 3.14.. approximation

but if you embed the fraction in a dynamic expression in a function,

it will give more flexibility ...

 

You are right that using a fraction such as 22/7 dynamically inside your code can give you more felxibility and more accurate results. You are completely missing the point, however, which is that 22/7 is a very bad value for Pi. 22/7 = 3.14285... so you see it goes off at 4th decimal already. So if 22/7 was the value you wanted to use, you would be correct, but as you want to be using an accurate value of Pi, any use of 22/7 will screw you over.

 

first version:: given X: return (22/7) * (1/X) * 100,000,000

X=3 .. = 3.14159 / 3 = 1.04719666 * 100,000,000

.. = 104,719,666

 

This is simply wrong. 22/7 does not equate 3.14159, for some reason you seem to have replaced it with a 6 decimal approximation of Pi.

 

2nd version:: given X: return 22/(7 * X) * 100,000,000

X=3 .. = 22 / 7*3 = 22/21 = 1.04761904 * 100,000,000

.. = 104,761,904

 

This calculation is correct, and again it gives you a very bad answer, because you want to be using a more accurate approx of Pi than 22/7.

Link to comment
Share on other sites

I think we're all focussing a bit too much on Khaled's terrible choice of approximation. There is a valid point to be found in the fact that given finite precision, the accuracy of a calculation does depend on the way that calculation is performed as well as the inputs themselves.

Link to comment
Share on other sites

I think we're all focussing a bit too much on Khaled's terrible choice of approximation. There is a valid point to be found in the fact that given finite precision, the accuracy of a calculation does depend on the way that calculation is performed as well as the inputs themselves.

 

So the point is, if you can come up with an approximation that is accurate down to the precision of the computer, then it would be worth examining whether one calculation method would perform better than another.

Link to comment
Share on other sites

Very much so.

 

Say you needed to calculate x(y-z) or xy-xz, then one method will give a more accurate answer than the other, especially if y and z are close by.

 

With x=1.111, y=1.533, z=1.521, and a given accuracy of 4 significant figures for every FLOP (yeah okay that's a little contrived, but anyway) then you'll get relative errors of about 0.02% and 2.5% respectively. One of many rules of thumb is to avoid subtracting numbers that are close by, as that always results in a significant loss of accuracy.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.