Unconfigured Ad Widget

Collapse

C# math question.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • #16
    the86d
    Calguns Addict
    • Jul 2011
    • 9587

    I haven't coded since Borland C++ for DOS, and Quickbasic for DOS, but this might be what you are looking for to round?:
    ANSWER1=INT(7/2+.5)
    ANSWER2=INT(5/7+.5)

    Add .5, then truncate as an INT.

    Old-school rounding.
    Last edited by the86d; 10-17-2018, 5:32 AM.

    Comment

    • #17
      njineermike
      Calguns Addict
      • Dec 2010
      • 9784

      Originally posted by Pandanin
      It doesn't round; instead it drops the decimal portion of the number.

      So yes, 5/2 = 2.5

      (int)2.5 = 2
      This is what I always thought was correct.
      Originally posted by Kestryll
      Dude went full CNN...
      Peace, love, and heavy weapons. Sometimes you have to be insistent." - David Lee Roth

      Comment

      • #18
        Deedle
        Senior Member
        • Jan 2018
        • 1146

        Originally posted by the86d
        I haven't coded since Borland C++ for DOS, and Quickbasic for DOS, but this might be what you are looking for to round?:
        ANSWER1=INT(7/2+.5)
        ANSWER2=INT(5/7+.5)

        Add .5, then truncate as an INT.

        Old-school rounding.
        Might not give the result you're expecting in all cases, sadly.

        INT(-3.9 + 0.5) ...... gives 3, not 4

        There are many types of rounding, so it's crucial to know what is expected at the design/requirements level. A short and incomplete list:

        Round away from zero
        Round toward zero
        Round to nearest even (AKA bankers rounding)
        Round to nearest odd
        Round up
        Round down
        ....
        "No personal computer will ever have gigabytes of RAM" - Scott Nudds

        Comment

        • #19
          bob7122
          Calguns Addict
          • Jul 2010
          • 5090

          Thank you all for the help, I really appreciate it.
          Originally posted by 2761377
          man's greatest accomplishments have been achieved in the face of futility.
          it's a piss poor excuse to quit.
          PSN name= entwie_dumayla
          "I came into this world with someone else's blood on me and I don't mind leaving the same way..."
          ***looking to buy in great condition yugo sks***

          Comment

          • #20
            HUTCH 7.62
            In Memoriam
            • Aug 2006
            • 11298

            Some say that he once mooned two prostitutes just for a round of drinks, but wasn't surprised by the reply......They call him, the Hutch
            Some say that he rode a dirtbike 7k miles across the country and that he once applied Bengay to his own testicles for a mere $50............They call him, the Hutch -Top Gear

            http://media.liveauctiongroup.net/i/...CCAB7CE8D70F60

            Comment

            • #21
              smashycrashy
              Veteran Member
              • Sep 2011
              • 2999

              Originally posted by BigFatGuy
              If you don't mind a bit of free advice from someone who flunked out of school, then became an honors student, a teacher, and is now writing (vhdl) code for a satellite project I can't talk about in public:

              if you wonder "what will happen with this code", write the code and run it to find out.

              "Why does this strange thing happen" is a much better question than "what will happen". Your job, basically, for the rest of your career, is to impress your (teachers/bosses/coworkers/lovers) with how hard working, curious, and resourceful you are.
              Agree 100%,the code in question is trivial and would have been faster to write then the original post.

              Comment

              • #22
                Deedle
                Senior Member
                • Jan 2018
                • 1146

                Originally posted by BigFatGuy
                if you wonder "what will happen with this code", write the code and run it to find out.
                Originally posted by smashycrashy
                Agree 100%,the code in question is trivial and would have been faster to write then the original post.
                This can be useful as a learning exercise but IMO it's far far better to understand the language and other tools in use and write code that's correct and THEN verify it rather than determine by experiment what some code does until it seems to work. The latter can work in a pinch but if you're in a situation where there are a wide variety of possibly hostile inputs to your function, a variety of platforms it might find itself running on, and so on then you have a problem. All you've proved is it works for your test inputs on your specific platform.

                The above discussion where no one considered rounding float values less than zero is a classic and extremely simplistic example.
                "No personal computer will ever have gigabytes of RAM" - Scott Nudds

                Comment

                • #23
                  njineermike
                  Calguns Addict
                  • Dec 2010
                  • 9784

                  Just throwing this out there, but if forced to work in all INT, (and if the values allow it) you can multiply all values by 10 to get an implied decimal. Just bear in mind a signed 16 bit integer (WORD) limits you to 16383 and an unsigned to 32767, which imply 1638.3 and 3276.7. Multiply by 100 for 2 implied decimal places where values allow. Unfortunately, division and INT units are not friendly. You will always have the rounding error issue. But it's a great tool for addition, subtraction and multiplication with all INT.

                  I have to do that all the time when I'm stuck dealing with ancient technology. I then either shift the value on another function somehow or do the decimal point shift in a software format that allows it, like a graphics package. For example, a lot of analog input cards are INT only. Koyo cards are 12 bit, so I get 0-4096. An implied decimal gives me .1 unit resolution instead of INT unit resolution when measuring something that the span allows.
                  Originally posted by Kestryll
                  Dude went full CNN...
                  Peace, love, and heavy weapons. Sometimes you have to be insistent." - David Lee Roth

                  Comment

                  Working...
                  UA-8071174-1