On Tue, 19 Oct 2004 21:26:51 -0700, Gianni Mariani wrote in

<news:PZ********************@speakeasy.net>:

[snip!]

When exp is at the limits of what the double can store, the mantissa is

not normalized, so you can simply call frexp twice and compare the

exponents to be equal both times. Then you can subtract the 2 resulting

mantissas and call std::frexp on the result and the exponent on the

third call will be the negative of the number of significant binary

digits which you can convert to decimal by the siggestion I made earlier.

I hadn't thought of using stuff like that, but I have found a way which

works great and is about as simple as you can get.

Okay, the variables I'm comparing are a constant which in my program I call

cons_pi and it equals 3.14159265358979. The user enters an integer number

for how many times they want to do that calculation to figure out pi (4/1 -

4/3 + 4/5 - 4/7 etc.). That calculation is placed into calc_pi. Both

these numbers are long doubles to get as many decimal places as I can.

Anyway, the teacher wants us to figure the accuracy of our calculated value

which is where I was stuck trying to figure out a way to compare the two

numeric values.

Well, I decided to multiply each value starting with 10 and then comparing

the int() of each of the variables. If the variables are equal then that

is one position of accuracy and the do-while loop continues while

incrementing the accuracy variable by 1 and incrementing the multiply

variable times 10. If the variables are not equal then there is no more

accuracy possible with the calculated value so flag is set to on and the

do-while loop ends returning the accuracy variable to main().

I just ran from 1 to 50 one-by-one and then various values up to

100,000,000 and it works great. After I clean up the code and give it the

once over again I'll set the program to run through 2,147,483,647 and head

off to bed. Maybe it will be done in the morning when I get up to go to

class. ;-)

It seems I was trying a bit too hard looking for a difficult method to get

the job done with it really ended up being a heck of a lot easier.

Dave

Now I can get to sleep. :-)

--

You can talk about us, but you can't talk without us!

US Army Signal Corps!!

http://www.geocities.com/davidcasey98
Remove IH8SPAM to reply by email!