How do I avoid round-off error in this list?












0















This list should have x[50] as zero and both sides to be symmetrical, but it is slightly off centre because of what I assume is roundoff error. How can I modify my code to avoid this?



Thanks!



import numpy as np
L=2*np.pi
s=101
ds=L/(s-1)
svals=np.arange(1,101)
x=[0]
x[0:s]=((svals-1)*ds)-L/2
print(x)









share|improve this question


















  • 1





    I'd suggest you do these calculations one at a time in an interactive Python session, and look at the result from each. You seem to be confusing numpy arrays and lists. The x=[0] followed by x[0:s] looks particularly suspicious.

    – hpaulj
    Nov 25 '18 at 21:29


















0















This list should have x[50] as zero and both sides to be symmetrical, but it is slightly off centre because of what I assume is roundoff error. How can I modify my code to avoid this?



Thanks!



import numpy as np
L=2*np.pi
s=101
ds=L/(s-1)
svals=np.arange(1,101)
x=[0]
x[0:s]=((svals-1)*ds)-L/2
print(x)









share|improve this question


















  • 1





    I'd suggest you do these calculations one at a time in an interactive Python session, and look at the result from each. You seem to be confusing numpy arrays and lists. The x=[0] followed by x[0:s] looks particularly suspicious.

    – hpaulj
    Nov 25 '18 at 21:29
















0












0








0








This list should have x[50] as zero and both sides to be symmetrical, but it is slightly off centre because of what I assume is roundoff error. How can I modify my code to avoid this?



Thanks!



import numpy as np
L=2*np.pi
s=101
ds=L/(s-1)
svals=np.arange(1,101)
x=[0]
x[0:s]=((svals-1)*ds)-L/2
print(x)









share|improve this question














This list should have x[50] as zero and both sides to be symmetrical, but it is slightly off centre because of what I assume is roundoff error. How can I modify my code to avoid this?



Thanks!



import numpy as np
L=2*np.pi
s=101
ds=L/(s-1)
svals=np.arange(1,101)
x=[0]
x[0:s]=((svals-1)*ds)-L/2
print(x)






python numpy floating-point rounding






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 25 '18 at 21:23









T. LT. L

465




465








  • 1





    I'd suggest you do these calculations one at a time in an interactive Python session, and look at the result from each. You seem to be confusing numpy arrays and lists. The x=[0] followed by x[0:s] looks particularly suspicious.

    – hpaulj
    Nov 25 '18 at 21:29
















  • 1





    I'd suggest you do these calculations one at a time in an interactive Python session, and look at the result from each. You seem to be confusing numpy arrays and lists. The x=[0] followed by x[0:s] looks particularly suspicious.

    – hpaulj
    Nov 25 '18 at 21:29










1




1





I'd suggest you do these calculations one at a time in an interactive Python session, and look at the result from each. You seem to be confusing numpy arrays and lists. The x=[0] followed by x[0:s] looks particularly suspicious.

– hpaulj
Nov 25 '18 at 21:29







I'd suggest you do these calculations one at a time in an interactive Python session, and look at the result from each. You seem to be confusing numpy arrays and lists. The x=[0] followed by x[0:s] looks particularly suspicious.

– hpaulj
Nov 25 '18 at 21:29














1 Answer
1






active

oldest

votes


















1














Getting precise outputs from floating point operations can be tricky and fiddly. You can get the list you want using np.linspace:



x = np.linspace(-np.pi, 0, num=51)
x = np.concatenate([x, np.linspace(x[-1] - x[-2], np.pi, num=50)])
print(x)


Output:



[-3.14159265 -3.0787608  -3.01592895 -2.95309709 -2.89026524 -2.82743339
-2.76460154 -2.70176968 -2.63893783 -2.57610598 -2.51327412 -2.45044227
-2.38761042 -2.32477856 -2.26194671 -2.19911486 -2.136283 -2.07345115
-2.0106193 -1.94778745 -1.88495559 -1.82212374 -1.75929189 -1.69646003
-1.63362818 -1.57079633 -1.50796447 -1.44513262 -1.38230077 -1.31946891
-1.25663706 -1.19380521 -1.13097336 -1.0681415 -1.00530965 -0.9424778
-0.87964594 -0.81681409 -0.75398224 -0.69115038 -0.62831853 -0.56548668
-0.50265482 -0.43982297 -0.37699112 -0.31415927 -0.25132741 -0.18849556
-0.12566371 -0.06283185 0. 0.06283185 0.12566371 0.18849556
0.25132741 0.31415927 0.37699112 0.43982297 0.50265482 0.56548668
0.62831853 0.69115038 0.75398224 0.81681409 0.87964594 0.9424778
1.00530965 1.0681415 1.13097336 1.19380521 1.25663706 1.31946891
1.38230077 1.44513262 1.50796447 1.57079633 1.63362818 1.69646003
1.75929189 1.82212374 1.88495559 1.94778745 2.0106193 2.07345115
2.136283 2.19911486 2.26194671 2.32477856 2.38761042 2.45044227
2.51327412 2.57610598 2.63893783 2.70176968 2.76460154 2.82743339
2.89026524 2.95309709 3.01592895 3.0787608 3.14159265]


It's made in two steps to avoid a numerical error that crops up when linspace ranges across 0. If x is made in one step as



x = np.linspace(-np.pi, np.pi, 101)


then the value at x[50] is 4.440892098500626e-16, instead of x[50] being 0 as expected.






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%2f53472132%2fhow-do-i-avoid-round-off-error-in-this-list%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Getting precise outputs from floating point operations can be tricky and fiddly. You can get the list you want using np.linspace:



    x = np.linspace(-np.pi, 0, num=51)
    x = np.concatenate([x, np.linspace(x[-1] - x[-2], np.pi, num=50)])
    print(x)


    Output:



    [-3.14159265 -3.0787608  -3.01592895 -2.95309709 -2.89026524 -2.82743339
    -2.76460154 -2.70176968 -2.63893783 -2.57610598 -2.51327412 -2.45044227
    -2.38761042 -2.32477856 -2.26194671 -2.19911486 -2.136283 -2.07345115
    -2.0106193 -1.94778745 -1.88495559 -1.82212374 -1.75929189 -1.69646003
    -1.63362818 -1.57079633 -1.50796447 -1.44513262 -1.38230077 -1.31946891
    -1.25663706 -1.19380521 -1.13097336 -1.0681415 -1.00530965 -0.9424778
    -0.87964594 -0.81681409 -0.75398224 -0.69115038 -0.62831853 -0.56548668
    -0.50265482 -0.43982297 -0.37699112 -0.31415927 -0.25132741 -0.18849556
    -0.12566371 -0.06283185 0. 0.06283185 0.12566371 0.18849556
    0.25132741 0.31415927 0.37699112 0.43982297 0.50265482 0.56548668
    0.62831853 0.69115038 0.75398224 0.81681409 0.87964594 0.9424778
    1.00530965 1.0681415 1.13097336 1.19380521 1.25663706 1.31946891
    1.38230077 1.44513262 1.50796447 1.57079633 1.63362818 1.69646003
    1.75929189 1.82212374 1.88495559 1.94778745 2.0106193 2.07345115
    2.136283 2.19911486 2.26194671 2.32477856 2.38761042 2.45044227
    2.51327412 2.57610598 2.63893783 2.70176968 2.76460154 2.82743339
    2.89026524 2.95309709 3.01592895 3.0787608 3.14159265]


    It's made in two steps to avoid a numerical error that crops up when linspace ranges across 0. If x is made in one step as



    x = np.linspace(-np.pi, np.pi, 101)


    then the value at x[50] is 4.440892098500626e-16, instead of x[50] being 0 as expected.






    share|improve this answer




























      1














      Getting precise outputs from floating point operations can be tricky and fiddly. You can get the list you want using np.linspace:



      x = np.linspace(-np.pi, 0, num=51)
      x = np.concatenate([x, np.linspace(x[-1] - x[-2], np.pi, num=50)])
      print(x)


      Output:



      [-3.14159265 -3.0787608  -3.01592895 -2.95309709 -2.89026524 -2.82743339
      -2.76460154 -2.70176968 -2.63893783 -2.57610598 -2.51327412 -2.45044227
      -2.38761042 -2.32477856 -2.26194671 -2.19911486 -2.136283 -2.07345115
      -2.0106193 -1.94778745 -1.88495559 -1.82212374 -1.75929189 -1.69646003
      -1.63362818 -1.57079633 -1.50796447 -1.44513262 -1.38230077 -1.31946891
      -1.25663706 -1.19380521 -1.13097336 -1.0681415 -1.00530965 -0.9424778
      -0.87964594 -0.81681409 -0.75398224 -0.69115038 -0.62831853 -0.56548668
      -0.50265482 -0.43982297 -0.37699112 -0.31415927 -0.25132741 -0.18849556
      -0.12566371 -0.06283185 0. 0.06283185 0.12566371 0.18849556
      0.25132741 0.31415927 0.37699112 0.43982297 0.50265482 0.56548668
      0.62831853 0.69115038 0.75398224 0.81681409 0.87964594 0.9424778
      1.00530965 1.0681415 1.13097336 1.19380521 1.25663706 1.31946891
      1.38230077 1.44513262 1.50796447 1.57079633 1.63362818 1.69646003
      1.75929189 1.82212374 1.88495559 1.94778745 2.0106193 2.07345115
      2.136283 2.19911486 2.26194671 2.32477856 2.38761042 2.45044227
      2.51327412 2.57610598 2.63893783 2.70176968 2.76460154 2.82743339
      2.89026524 2.95309709 3.01592895 3.0787608 3.14159265]


      It's made in two steps to avoid a numerical error that crops up when linspace ranges across 0. If x is made in one step as



      x = np.linspace(-np.pi, np.pi, 101)


      then the value at x[50] is 4.440892098500626e-16, instead of x[50] being 0 as expected.






      share|improve this answer


























        1












        1








        1







        Getting precise outputs from floating point operations can be tricky and fiddly. You can get the list you want using np.linspace:



        x = np.linspace(-np.pi, 0, num=51)
        x = np.concatenate([x, np.linspace(x[-1] - x[-2], np.pi, num=50)])
        print(x)


        Output:



        [-3.14159265 -3.0787608  -3.01592895 -2.95309709 -2.89026524 -2.82743339
        -2.76460154 -2.70176968 -2.63893783 -2.57610598 -2.51327412 -2.45044227
        -2.38761042 -2.32477856 -2.26194671 -2.19911486 -2.136283 -2.07345115
        -2.0106193 -1.94778745 -1.88495559 -1.82212374 -1.75929189 -1.69646003
        -1.63362818 -1.57079633 -1.50796447 -1.44513262 -1.38230077 -1.31946891
        -1.25663706 -1.19380521 -1.13097336 -1.0681415 -1.00530965 -0.9424778
        -0.87964594 -0.81681409 -0.75398224 -0.69115038 -0.62831853 -0.56548668
        -0.50265482 -0.43982297 -0.37699112 -0.31415927 -0.25132741 -0.18849556
        -0.12566371 -0.06283185 0. 0.06283185 0.12566371 0.18849556
        0.25132741 0.31415927 0.37699112 0.43982297 0.50265482 0.56548668
        0.62831853 0.69115038 0.75398224 0.81681409 0.87964594 0.9424778
        1.00530965 1.0681415 1.13097336 1.19380521 1.25663706 1.31946891
        1.38230077 1.44513262 1.50796447 1.57079633 1.63362818 1.69646003
        1.75929189 1.82212374 1.88495559 1.94778745 2.0106193 2.07345115
        2.136283 2.19911486 2.26194671 2.32477856 2.38761042 2.45044227
        2.51327412 2.57610598 2.63893783 2.70176968 2.76460154 2.82743339
        2.89026524 2.95309709 3.01592895 3.0787608 3.14159265]


        It's made in two steps to avoid a numerical error that crops up when linspace ranges across 0. If x is made in one step as



        x = np.linspace(-np.pi, np.pi, 101)


        then the value at x[50] is 4.440892098500626e-16, instead of x[50] being 0 as expected.






        share|improve this answer













        Getting precise outputs from floating point operations can be tricky and fiddly. You can get the list you want using np.linspace:



        x = np.linspace(-np.pi, 0, num=51)
        x = np.concatenate([x, np.linspace(x[-1] - x[-2], np.pi, num=50)])
        print(x)


        Output:



        [-3.14159265 -3.0787608  -3.01592895 -2.95309709 -2.89026524 -2.82743339
        -2.76460154 -2.70176968 -2.63893783 -2.57610598 -2.51327412 -2.45044227
        -2.38761042 -2.32477856 -2.26194671 -2.19911486 -2.136283 -2.07345115
        -2.0106193 -1.94778745 -1.88495559 -1.82212374 -1.75929189 -1.69646003
        -1.63362818 -1.57079633 -1.50796447 -1.44513262 -1.38230077 -1.31946891
        -1.25663706 -1.19380521 -1.13097336 -1.0681415 -1.00530965 -0.9424778
        -0.87964594 -0.81681409 -0.75398224 -0.69115038 -0.62831853 -0.56548668
        -0.50265482 -0.43982297 -0.37699112 -0.31415927 -0.25132741 -0.18849556
        -0.12566371 -0.06283185 0. 0.06283185 0.12566371 0.18849556
        0.25132741 0.31415927 0.37699112 0.43982297 0.50265482 0.56548668
        0.62831853 0.69115038 0.75398224 0.81681409 0.87964594 0.9424778
        1.00530965 1.0681415 1.13097336 1.19380521 1.25663706 1.31946891
        1.38230077 1.44513262 1.50796447 1.57079633 1.63362818 1.69646003
        1.75929189 1.82212374 1.88495559 1.94778745 2.0106193 2.07345115
        2.136283 2.19911486 2.26194671 2.32477856 2.38761042 2.45044227
        2.51327412 2.57610598 2.63893783 2.70176968 2.76460154 2.82743339
        2.89026524 2.95309709 3.01592895 3.0787608 3.14159265]


        It's made in two steps to avoid a numerical error that crops up when linspace ranges across 0. If x is made in one step as



        x = np.linspace(-np.pi, np.pi, 101)


        then the value at x[50] is 4.440892098500626e-16, instead of x[50] being 0 as expected.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 25 '18 at 21:32









        teltel

        7,43621431




        7,43621431
































            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.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53472132%2fhow-do-i-avoid-round-off-error-in-this-list%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







            Popular posts from this blog

            To store a contact into the json file from server.js file using a class in NodeJS

            Redirect URL with Chrome Remote Debugging Android Devices

            Dieringhausen