Why is there a difference between starting with a “0” and “3” for approx












-1














I was attempting an approximated value of pi through the formula of
pi= 3 + (4/(2*3*4)) - (4/(4*5*6)) + (4/(6*7*8)) - … (and so on). However, my code (shown below) had 2 separate answers (3.1415926535900383 and 3.141592653590042) when:




  1. approx variable started with "0" and "3" respectively

  2. n=10000


Does anyone know why?



def approximate_pi(n):
approx=0
deno=2
if n == 1:
return 3
for x in range(n-1):
if x%2:
approx -= 4/((deno)*(deno+1)*(deno+2))
else:
approx += 4/((deno)*(deno+1)*(deno+2))
deno+=2
return approx+3


and



def approximate_pi(n):
approx=3
deno=2
if n == 1:
return 3
for x in range(n-1):
if x%2:
approx -= 4/((deno)*(deno+1)*(deno+2))
else:
approx += 4/((deno)*(deno+1)*(deno+2))
deno+=2
return approx









share|improve this question
























  • @tripleee - I thought this had some close votes due to duplicates. Did something happen to them?
    – jww
    Nov 12 '18 at 9:33
















-1














I was attempting an approximated value of pi through the formula of
pi= 3 + (4/(2*3*4)) - (4/(4*5*6)) + (4/(6*7*8)) - … (and so on). However, my code (shown below) had 2 separate answers (3.1415926535900383 and 3.141592653590042) when:




  1. approx variable started with "0" and "3" respectively

  2. n=10000


Does anyone know why?



def approximate_pi(n):
approx=0
deno=2
if n == 1:
return 3
for x in range(n-1):
if x%2:
approx -= 4/((deno)*(deno+1)*(deno+2))
else:
approx += 4/((deno)*(deno+1)*(deno+2))
deno+=2
return approx+3


and



def approximate_pi(n):
approx=3
deno=2
if n == 1:
return 3
for x in range(n-1):
if x%2:
approx -= 4/((deno)*(deno+1)*(deno+2))
else:
approx += 4/((deno)*(deno+1)*(deno+2))
deno+=2
return approx









share|improve this question
























  • @tripleee - I thought this had some close votes due to duplicates. Did something happen to them?
    – jww
    Nov 12 '18 at 9:33














-1












-1








-1







I was attempting an approximated value of pi through the formula of
pi= 3 + (4/(2*3*4)) - (4/(4*5*6)) + (4/(6*7*8)) - … (and so on). However, my code (shown below) had 2 separate answers (3.1415926535900383 and 3.141592653590042) when:




  1. approx variable started with "0" and "3" respectively

  2. n=10000


Does anyone know why?



def approximate_pi(n):
approx=0
deno=2
if n == 1:
return 3
for x in range(n-1):
if x%2:
approx -= 4/((deno)*(deno+1)*(deno+2))
else:
approx += 4/((deno)*(deno+1)*(deno+2))
deno+=2
return approx+3


and



def approximate_pi(n):
approx=3
deno=2
if n == 1:
return 3
for x in range(n-1):
if x%2:
approx -= 4/((deno)*(deno+1)*(deno+2))
else:
approx += 4/((deno)*(deno+1)*(deno+2))
deno+=2
return approx









share|improve this question















I was attempting an approximated value of pi through the formula of
pi= 3 + (4/(2*3*4)) - (4/(4*5*6)) + (4/(6*7*8)) - … (and so on). However, my code (shown below) had 2 separate answers (3.1415926535900383 and 3.141592653590042) when:




  1. approx variable started with "0" and "3" respectively

  2. n=10000


Does anyone know why?



def approximate_pi(n):
approx=0
deno=2
if n == 1:
return 3
for x in range(n-1):
if x%2:
approx -= 4/((deno)*(deno+1)*(deno+2))
else:
approx += 4/((deno)*(deno+1)*(deno+2))
deno+=2
return approx+3


and



def approximate_pi(n):
approx=3
deno=2
if n == 1:
return 3
for x in range(n-1):
if x%2:
approx -= 4/((deno)*(deno+1)*(deno+2))
else:
approx += 4/((deno)*(deno+1)*(deno+2))
deno+=2
return approx






python python-3.x






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 '18 at 8:28









jww

52.7k39221483




52.7k39221483










asked Nov 12 '18 at 4:48









Zi Yang Chow

1




1












  • @tripleee - I thought this had some close votes due to duplicates. Did something happen to them?
    – jww
    Nov 12 '18 at 9:33


















  • @tripleee - I thought this had some close votes due to duplicates. Did something happen to them?
    – jww
    Nov 12 '18 at 9:33
















@tripleee - I thought this had some close votes due to duplicates. Did something happen to them?
– jww
Nov 12 '18 at 9:33




@tripleee - I thought this had some close votes due to duplicates. Did something happen to them?
– jww
Nov 12 '18 at 9:33












3 Answers
3






active

oldest

votes


















0














I think is because you can't have exact float numbers in pc. More info you can get here: Why can't decimal numbers be represented exactly in binary?






share|improve this answer





























    0














    It is because of the way float is in python. If you do not have the digit before the decimal it gives 3 extra precision digits(from my trial). This changes the answer because when you start with 0, you get a different calculation altogether.






    share|improve this answer





























      0














      An approximation algorithm approximates. Neither number is the true value of π. Your two versions start at different starting points, so why are you surprised that they give you slightly different approximations? What matters is that the longer you run them, the further they will both converge to the true value.



      Note that this is not an artifact of the finite-precision representation of floats. While floating point rounding will affect your results, you would see differences even with unlimited precision arithmetic.






      share|improve this answer





















        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53256113%2fwhy-is-there-a-difference-between-starting-with-a-0-and-3-for-approx%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        0














        I think is because you can't have exact float numbers in pc. More info you can get here: Why can't decimal numbers be represented exactly in binary?






        share|improve this answer


























          0














          I think is because you can't have exact float numbers in pc. More info you can get here: Why can't decimal numbers be represented exactly in binary?






          share|improve this answer
























            0












            0








            0






            I think is because you can't have exact float numbers in pc. More info you can get here: Why can't decimal numbers be represented exactly in binary?






            share|improve this answer












            I think is because you can't have exact float numbers in pc. More info you can get here: Why can't decimal numbers be represented exactly in binary?







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 12 '18 at 5:16









            ChaosPredictor

            1,91311624




            1,91311624

























                0














                It is because of the way float is in python. If you do not have the digit before the decimal it gives 3 extra precision digits(from my trial). This changes the answer because when you start with 0, you get a different calculation altogether.






                share|improve this answer


























                  0














                  It is because of the way float is in python. If you do not have the digit before the decimal it gives 3 extra precision digits(from my trial). This changes the answer because when you start with 0, you get a different calculation altogether.






                  share|improve this answer
























                    0












                    0








                    0






                    It is because of the way float is in python. If you do not have the digit before the decimal it gives 3 extra precision digits(from my trial). This changes the answer because when you start with 0, you get a different calculation altogether.






                    share|improve this answer












                    It is because of the way float is in python. If you do not have the digit before the decimal it gives 3 extra precision digits(from my trial). This changes the answer because when you start with 0, you get a different calculation altogether.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 12 '18 at 5:51









                    Knl_Kolhe

                    113




                    113























                        0














                        An approximation algorithm approximates. Neither number is the true value of π. Your two versions start at different starting points, so why are you surprised that they give you slightly different approximations? What matters is that the longer you run them, the further they will both converge to the true value.



                        Note that this is not an artifact of the finite-precision representation of floats. While floating point rounding will affect your results, you would see differences even with unlimited precision arithmetic.






                        share|improve this answer


























                          0














                          An approximation algorithm approximates. Neither number is the true value of π. Your two versions start at different starting points, so why are you surprised that they give you slightly different approximations? What matters is that the longer you run them, the further they will both converge to the true value.



                          Note that this is not an artifact of the finite-precision representation of floats. While floating point rounding will affect your results, you would see differences even with unlimited precision arithmetic.






                          share|improve this answer
























                            0












                            0








                            0






                            An approximation algorithm approximates. Neither number is the true value of π. Your two versions start at different starting points, so why are you surprised that they give you slightly different approximations? What matters is that the longer you run them, the further they will both converge to the true value.



                            Note that this is not an artifact of the finite-precision representation of floats. While floating point rounding will affect your results, you would see differences even with unlimited precision arithmetic.






                            share|improve this answer












                            An approximation algorithm approximates. Neither number is the true value of π. Your two versions start at different starting points, so why are you surprised that they give you slightly different approximations? What matters is that the longer you run them, the further they will both converge to the true value.



                            Note that this is not an artifact of the finite-precision representation of floats. While floating point rounding will affect your results, you would see differences even with unlimited precision arithmetic.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 12 '18 at 9:29









                            alexis

                            33.5k954114




                            33.5k954114






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.





                                Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                Please pay close attention to the following guidance:


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53256113%2fwhy-is-there-a-difference-between-starting-with-a-0-and-3-for-approx%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                這個網誌中的熱門文章

                                Xamarin.form Move up view when keyboard appear

                                Post-Redirect-Get with Spring WebFlux and Thymeleaf

                                Anylogic : not able to use stopDelay()