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. MILLISEC_PER_SEC is never needed

MILLISEC_PER_SEC is never needed

Scheduled Pinned Locked Moved The Lounge
76 Posts 31 Posters 4 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.
  • L Lost User

    That's what I would think, as the SI-system is fairly logical. Then you meet someone from the UK and they tell you there's x feet in an inch. It's also a nice shortcut, to change the display from msec to sec, just by setting this constant to "0" instead of "1000" :p

    Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

    U Offline
    U Offline
    User 10683712
    wrote on last edited by
    #65

    Surely the constant should change to 1 not 0? It's a scale factor not an offset

    Ken

    L 1 Reply Last reply
    0
    • C Chris Maunder

      I put it to you that a variable, constant or define named "MILLISEC_PER_SEC" is never, ever, ever needed.

      cheers Chris Maunder

      D Offline
      D Offline
      Dar Brett 0
      wrote on last edited by
      #66

      If I'm analysing a time based data stream I may wish to record how many milliseconds worth of a data I am processing per second. This information would be crucial for scaling the services doing the processing.

      1 Reply Last reply
      0
      • U User 10683712

        Surely the constant should change to 1 not 0? It's a scale factor not an offset

        Ken

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #67

        Let's save that modification for version 1.1, and focus on releasing 1.0, oke? :cool:

        Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

        1 Reply Last reply
        0
        • C Chris Maunder

          I put it to you that a variable, constant or define named "MILLISEC_PER_SEC" is never, ever, ever needed.

          cheers Chris Maunder

          D Offline
          D Offline
          ddyer
          wrote on last edited by
          #68

          MILLISEC_PER_SEC is actually a conversion factor between some underlying time measurement and seconds - the name suggests it is milliseconds, but it might not be, and it might change. Some old computer hardware only kept time in power line frequency of 50 or 60 hz, and some future hardware (or OS) might keep time intrinsically in nanoseconds. Having a symbolic constant in this conversion, instead of a magic number 1000, makes it perfectly clear what the semantics of the conversion are, which is a good thing.

          1 Reply Last reply
          0
          • C Chris Maunder

            I put it to you that a variable, constant or define named "MILLISEC_PER_SEC" is never, ever, ever needed.

            cheers Chris Maunder

            R Offline
            R Offline
            Robert Tax
            wrote on last edited by
            #69

            I disagree - I have done such things often. If you are writing software for an embedded system, it will often happen that the basic time tick is close to but not exactly a millisecond: for instance, with a 1MHz clock and a clock divider of 1024 you get 1.024 msec. You can still think of this as a millisecond, which is close enough for some purposes. But if you want to scale to a longer time period, you get errors. For instance, there are about 977 of your "milliseconds" in a second. So defining MILLISEC_PER_SEC as 977 is quite reasonable. Then 60 "seconds" is only off by .027 seconds. If you used the nominal 1000 "msec" per "sec", you'd be off by 1.44 seconds.

            1 Reply Last reply
            0
            • C Chris Maunder

              I put it to you that a variable, constant or define named "MILLISEC_PER_SEC" is never, ever, ever needed.

              cheers Chris Maunder

              D Offline
              D Offline
              Davyd McColl
              wrote on last edited by
              #70

              It may never be actually needed, but it's better than having a lurking special value, repeated throughout the code, of 1000. It communicates something about wherever it's being used -- probably to convert a seconds value to / from milliseconds. I'm for the constant - for easy-to-grok code.

              1 Reply Last reply
              0
              • K kalberts

                Eeeeh... kW per hour per hour? In which situations does that unit occur? When does kW per hour occur? A constantly rising (or falling) power, at a rate of x kW/h? And then you want to a unit for how much, in kW, the power has risen in one hour. You don't want to simply call it kW, these are not 'absolute' kilowatts, but a change in power, and that is a change per hour over a period of an hour, which is a different kind of kW unit. Then you want the unit of the increase in power for each minute, right? You come across the strangest units if you look around :-) When I was a student, we did some filter calculations where I (after years of wondering) saw how a frequency correction could be given by a time value (European FM pre-emphashis is 50 us, US radios use 75 us - or is it the other way around?), but I never got it under my skin; it is just a strange artifact of unit arithmetics! If you change that 60 to NumberOfMinutesPerHour, the question is: Are these really the same MinutesPerHour as when you measure 'absolute' time progression? Or, do these MinutesPerHour have a slightly different semantics from the wall clock's minutes per hour (similar to a kW value indicating a change in kiloWattPerHourPerHour is different from an instantaneous, or constant, power kilowatt value)? Maybe it should be called NumberOfMinutesPerHourWhenCalculatingChangesInPowerOverTime? When we updated our programming guidelines, the project leader of our project immediately granted an exception from the 80 char maximum line length: Our rules for how to construct 'const' names (this was K&R C) led to serveral cases of identifiers of length >80. Finally, there is the famous Xerox Fortran manual quote:

                "The primary purpose of the DATA statement is to give names to constants; instead of referring to pi as 3.141592653589793 at every appearance, the variable pi can be given that value with a DATA statement and used instead of the longer form of the constant. This also simplifies modifying the program, should the value of pi change."

                C Offline
                C Offline
                Chris Maunder
                wrote on last edited by
                #71

                Member 7989122 wrote:

                should the value of pi change

                Yep - one of my all time favourites that one :)

                cheers Chris Maunder

                1 Reply Last reply
                0
                • Richard DeemingR Richard Deeming

                  Well, obviously; they're measured in barleycorns[^]. Everybody knows that! :laugh:


                  "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                  P Offline
                  P Offline
                  Plamen Dragiyski
                  wrote on last edited by
                  #72

                  I learned not only a valuable unit today. This link enriched my view of the metric/non-metric systems. Very useful graphic[^]. Now I realize why it is so for a person familiar with all this to discover the metric system.

                  1 Reply Last reply
                  0
                  • C Chris Maunder

                    I put it to you that a variable, constant or define named "MILLISEC_PER_SEC" is never, ever, ever needed.

                    cheers Chris Maunder

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #73

                    Yes; but otherwise "1000" becomes a "magic number". Perhaps a little anal, but if one's intention is to be consistent... Some (scientific) apps treat seconds as a "double"; how else does one convert from milliseconds?

                    "(I) am amazed to see myself here rather than there ... now rather than then". ― Blaise Pascal

                    1 Reply Last reply
                    0
                    • C Chris Maunder

                      I put it to you that a variable, constant or define named "MILLISEC_PER_SEC" is never, ever, ever needed.

                      cheers Chris Maunder

                      M Offline
                      M Offline
                      Member 12150993
                      wrote on last edited by
                      #74

                      Sure it is. Imagine a clock that is gradually slowing down. Its rate of slowing down could be described in milliseconds per second.

                      1 Reply Last reply
                      0
                      • C Chris Maunder

                        I put it to you that a variable, constant or define named "MILLISEC_PER_SEC" is never, ever, ever needed.

                        cheers Chris Maunder

                        J Offline
                        J Offline
                        jschell
                        wrote on last edited by
                        #75

                        Documentation wise it isn't totally ridiculous. So something like the following is more obvious than with the real value. timeToProcess = runTime/MILLISEC_PER_SEC To be fair, myself I would probably have coded the above like the following timeToProcessSeconds = runTimeMillis/MILLISEC_PER_SEC But lots of times others do not. And I would rather see the hint.

                        1 Reply Last reply
                        0
                        • C Chris Maunder

                          I put it to you that a variable, constant or define named "MILLISEC_PER_SEC" is never, ever, ever needed.

                          cheers Chris Maunder

                          K Offline
                          K Offline
                          Kirill Illenseer
                          wrote on last edited by
                          #76

                          If you have measurement-illeterate members on your team, it's rather needed indeed. Sure, to everyone with even a bit education in engineering, "milli" is clearly E-3. But to everyone else, not so much. Heck, I've seen people stumbling over "1,2 k€" which is still rather clear to me.

                          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