Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. [Chortle]

[Chortle]

Scheduled Pinned Locked Moved The Lounge
c++comoopquestion
16 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H honey the codewitch

    I just got my compiler to compute a sine table using a taylor series expansion.

    Real programmers use butterflies

    CPalliniC Offline
    CPalliniC Offline
    CPallini
    wrote on last edited by
    #3

    You can do better: Fixed-Point Sine (and Cosine) for Embedded Systems | Null Hardware[^].

    "In testa che avete, Signor di Ceprano?" -- Rigoletto

    In testa che avete, signor di Ceprano?

    D 1 Reply Last reply
    0
    • H honey the codewitch

      I just got my compiler to compute a sine table using a taylor series expansion.

      Real programmers use butterflies

      D Offline
      D Offline
      Daniel Pfeffer
      wrote on last edited by
      #4

      The biggest problem with accurate computation of trig. functions is not computation of the result from the reduced argument, but reducing the argument to the range [-Pi, Pi). For example, 10^22 is accurately representable as a double. sin(10^22) requires finding the exact remainder of division by 2*Pi, which is non-trivial. See the following article for details: [https://www.csee.umbc.edu/~phatak/645/supl/Ng-ArgReduction.pdf\](https://www.csee.umbc.edu/~phatak/645/supl/Ng-ArgReduction.pdf)

      Freedom is the freedom to say that two plus two make four. If that is granted, all else follows. -- 6079 Smith W.

      D 1 Reply Last reply
      0
      • CPalliniC CPallini

        You can do better: Fixed-Point Sine (and Cosine) for Embedded Systems | Null Hardware[^].

        "In testa che avete, Signor di Ceprano?" -- Rigoletto

        D Offline
        D Offline
        den2k88
        wrote on last edited by
        #5

        ... In Q15.

        GCS d--(d-) s-/++ a C++++ U+++ P- L+@ E-- W++ N+ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t+ 5? X R+++ tv-- b+(+++) DI+++ D++ G e++ h--- r+++ y+++*      Weapons extension: ma- k++ F+2 X

        CPalliniC 1 Reply Last reply
        0
        • D den2k88

          ... In Q15.

          GCS d--(d-) s-/++ a C++++ U+++ P- L+@ E-- W++ N+ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t+ 5? X R+++ tv-- b+(+++) DI+++ D++ G e++ h--- r+++ y+++*      Weapons extension: ma- k++ F+2 X

          CPalliniC Offline
          CPalliniC Offline
          CPallini
          wrote on last edited by
          #6

          ... Fine for Cortex-M0. :-D

          "In testa che avete, Signor di Ceprano?" -- Rigoletto

          In testa che avete, signor di Ceprano?

          D 1 Reply Last reply
          0
          • CPalliniC CPallini

            ... Fine for Cortex-M0. :-D

            "In testa che avete, Signor di Ceprano?" -- Rigoletto

            D Offline
            D Offline
            den2k88
            wrote on last edited by
            #7

            And M3, and M4 :D depensing on the system on chip RAM can be expensive real estate.

            GCS d--(d-) s-/++ a C++++ U+++ P- L+@ E-- W++ N+ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t+ 5? X R+++ tv-- b+(+++) DI+++ D++ G e++ h--- r+++ y+++*      Weapons extension: ma- k++ F+2 X

            CPalliniC 1 Reply Last reply
            0
            • D Daniel Pfeffer

              The biggest problem with accurate computation of trig. functions is not computation of the result from the reduced argument, but reducing the argument to the range [-Pi, Pi). For example, 10^22 is accurately representable as a double. sin(10^22) requires finding the exact remainder of division by 2*Pi, which is non-trivial. See the following article for details: [https://www.csee.umbc.edu/~phatak/645/supl/Ng-ArgReduction.pdf\](https://www.csee.umbc.edu/~phatak/645/supl/Ng-ArgReduction.pdf)

              Freedom is the freedom to say that two plus two make four. If that is granted, all else follows. -- 6079 Smith W.

              D Offline
              D Offline
              den2k88
              wrote on last edited by
              #8

              Restrict to [0 - Pi/2] and then flip the result depending on the argument. Either you get 1/4 of the memory usage or 4 times the interpolation points.

              GCS d--(d-) s-/++ a C++++ U+++ P- L+@ E-- W++ N+ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t+ 5? X R+++ tv-- b+(+++) DI+++ D++ G e++ h--- r+++ y+++*      Weapons extension: ma- k++ F+2 X

              D 1 Reply Last reply
              0
              • D den2k88

                And M3, and M4 :D depensing on the system on chip RAM can be expensive real estate.

                GCS d--(d-) s-/++ a C++++ U+++ P- L+@ E-- W++ N+ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t+ 5? X R+++ tv-- b+(+++) DI+++ D++ G e++ h--- r+++ y+++*      Weapons extension: ma- k++ F+2 X

                CPalliniC Offline
                CPalliniC Offline
                CPallini
                wrote on last edited by
                #9

                Quote:

                and M4

                What a damned luxury thingy you are messing with! :-D

                "In testa che avete, Signor di Ceprano?" -- Rigoletto

                In testa che avete, signor di Ceprano?

                D 1 Reply Last reply
                0
                • CPalliniC CPallini

                  Quote:

                  and M4

                  What a damned luxury thingy you are messing with! :-D

                  "In testa che avete, Signor di Ceprano?" -- Rigoletto

                  D Offline
                  D Offline
                  den2k88
                  wrote on last edited by
                  #10

                  The one we're probably about to use is awesome, it has independent UARTs with internal self managed FIFOs for reading and writing, a Timer Capture interface that almost makes coffee... With respect to my experience with M0 and the current M3 it's truly luxury.

                  GCS d--(d-) s-/++ a C++++ U+++ P- L+@ E-- W++ N+ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t+ 5? X R+++ tv-- b+(+++) DI+++ D++ G e++ h--- r+++ y+++*      Weapons extension: ma- k++ F+2 X

                  1 Reply Last reply
                  0
                  • D den2k88

                    Restrict to [0 - Pi/2] and then flip the result depending on the argument. Either you get 1/4 of the memory usage or 4 times the interpolation points.

                    GCS d--(d-) s-/++ a C++++ U+++ P- L+@ E-- W++ N+ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t+ 5? X R+++ tv-- b+(+++) DI+++ D++ G e++ h--- r+++ y+++*      Weapons extension: ma- k++ F+2 X

                    D Offline
                    D Offline
                    Daniel Pfeffer
                    wrote on last edited by
                    #11

                    You still need to reduce the argument to that range, which is a major source of inaccuracy. That is what I pointed out.

                    Freedom is the freedom to say that two plus two make four. If that is granted, all else follows. -- 6079 Smith W.

                    D P 2 Replies Last reply
                    0
                    • D Daniel Pfeffer

                      You still need to reduce the argument to that range, which is a major source of inaccuracy. That is what I pointed out.

                      Freedom is the freedom to say that two plus two make four. If that is granted, all else follows. -- 6079 Smith W.

                      D Offline
                      D Offline
                      den2k88
                      wrote on last edited by
                      #12

                      Yeah I actually thought in fixed point, where accuracy is lost at the very beginning (but it's easier to calculate).

                      GCS d--(d-) s-/++ a C++++ U+++ P- L+@ E-- W++ N+ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t+ 5? X R+++ tv-- b+(+++) DI+++ D++ G e++ h--- r+++ y+++*      Weapons extension: ma- k++ F+2 X

                      1 Reply Last reply
                      0
                      • D Daniel Pfeffer

                        You still need to reduce the argument to that range, which is a major source of inaccuracy. That is what I pointed out.

                        Freedom is the freedom to say that two plus two make four. If that is granted, all else follows. -- 6079 Smith W.

                        P Offline
                        P Offline
                        Peter_in_2780
                        wrote on last edited by
                        #13

                        For a theoretical math library function, true. But for practical applications, who uses angles beyond maybe a few tens of radians?

                        Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012

                        D 1 Reply Last reply
                        0
                        • P Peter_in_2780

                          For a theoretical math library function, true. But for practical applications, who uses angles beyond maybe a few tens of radians?

                          Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012

                          D Offline
                          D Offline
                          Daniel Pfeffer
                          wrote on last edited by
                          #14

                          In order to calculate sin(x), you must first reduce the argument by calculating y = fmod(x, Pi/2). This entails subtraction of two numbers that may be very close to each other, i.e. you may get catastrophic cancellation and loss of accuracy. In order to avoid this (even for relatively small numbers), you must use a more accurate version of Pi than can be contained in a double. A high-quality floating-point library will perform the reduction at the highest accuracy for all finite values representable by double. This may be overkill for some embedded software (where, as you say, the angles may be small), but in that case there should be a way of indicating whether the result has any significance. Failure to do so can lead to some rather odd results... (The article I referred to in my earlier post ([https://www.csee.umbc.edu/~phatak/645/supl/Ng-ArgReduction.pdf\](https://www.csee.umbc.edu/~phatak/645/supl/Ng-ArgReduction.pdf)) has a table showing how different compilers miscalculated sin(10^22) and cos(10^22))

                          Freedom is the freedom to say that two plus two make four. If that is granted, all else follows. -- 6079 Smith W.

                          H 1 Reply Last reply
                          0
                          • D Daniel Pfeffer

                            In order to calculate sin(x), you must first reduce the argument by calculating y = fmod(x, Pi/2). This entails subtraction of two numbers that may be very close to each other, i.e. you may get catastrophic cancellation and loss of accuracy. In order to avoid this (even for relatively small numbers), you must use a more accurate version of Pi than can be contained in a double. A high-quality floating-point library will perform the reduction at the highest accuracy for all finite values representable by double. This may be overkill for some embedded software (where, as you say, the angles may be small), but in that case there should be a way of indicating whether the result has any significance. Failure to do so can lead to some rather odd results... (The article I referred to in my earlier post ([https://www.csee.umbc.edu/~phatak/645/supl/Ng-ArgReduction.pdf\](https://www.csee.umbc.edu/~phatak/645/supl/Ng-ArgReduction.pdf)) has a table showing how different compilers miscalculated sin(10^22) and cos(10^22))

                            Freedom is the freedom to say that two plus two make four. If that is granted, all else follows. -- 6079 Smith W.

                            H Offline
                            H Offline
                            honey the codewitch
                            wrote on last edited by
                            #15

                            Now y'all are talking python. I was over here talking C++. :laugh:

                            Real programmers use butterflies

                            1 Reply Last reply
                            0
                            • H honey the codewitch

                              I just got my compiler to compute a sine table using a taylor series expansion.

                              Real programmers use butterflies

                              D Offline
                              D Offline
                              DRHuff
                              wrote on last edited by
                              #16

                              That was one of my first Fortran assignments back up in 82 or 83! Back then their approach to teaching software development was sort of like “Here’s a dictionary- now write a novel!” Don’t know that it has changed much (aside from the Fortran part!).

                              If you can't laugh at yourself - ask me and I will do it for you.

                              1 Reply Last reply
                              0
                              Reply
                              • Reply as topic
                              Log in to reply
                              • Oldest to Newest
                              • Newest to Oldest
                              • Most Votes


                              • Login

                              • Don't have an account? Register

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • World
                              • Users
                              • Groups