Unconfigured Ad Widget

Collapse

C# math question.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bob7122
    Calguns Addict
    • Jul 2010
    • 5090

    C# math question.

    I have an exam this week and the math concept specifically ROUNDING is being brought up by my classmates and I need some clarification.

    I am stating that if both operands are integers then my answer will be in integer form, BUT YOU ROUND DOWN ALWAYS.

    What would the answer be for a math problem like the following? we don't need decimals its just integers EXAMPLE: X = 5/2=2.5 but the integer is rounded down to the final answer of 2, NOT 3.


    problem being debated is :

    7/2=3.5 but written in integer form wouldn't it be 3 and not 4?

    I thought the computer would automatically round down? Am I off?

    we are not using double data type or decimal.round method. I read that there is something like the banking method where you round to the closest even number and another method where you round to the number furthest away from zero. BUT we did not cover any of that in class.
    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***
  • #2
    Woodythedog
    Member
    • Jun 2014
    • 167

    The answer is ....buy both.

    Comment

    • #3
      Pandanin
      • Jun 2011
      • 643

      It doesn't round; instead it drops the decimal portion of the number.

      So yes, 5/2 = 2.5

      (int)2.5 = 2

      Comment

      • #4
        bob7122
        Calguns Addict
        • Jul 2010
        • 5090

        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
        thank you.

        would it apply to 7/2 = 3?
        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

        • #5
          Pandanin
          • Jun 2011
          • 643

          Originally posted by bob7122
          thank you.

          would it apply to 7/2 = 3?
          Yes.

          The left side of the decimal is the 'whole number' and the right side is the 'decimal'.

          For programming languages, the INT function returns the whole number and drops the 'decimal' completely.

          (int)5.999999999 = 5
          (int)5.111111111 = 5

          Comment

          • #6
            bob7122
            Calguns Addict
            • Jul 2010
            • 5090

            Originally posted by Pandanin
            Yes.

            The left side of the decimal is the 'whole number' and the right side is the 'decimal'.

            For programming languages, the INT function returns the whole number and drops the 'decimal' completely.

            (int)5.999999999 = 5
            (int)5.111111111 = 5
            thank you so much!
            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

            • #7
              Robotron2k84
              Senior Member
              • Sep 2017
              • 2013

              Yes, division returns integers unless overloaded by casting the return value as a floating point or precision type. In this case it's up to the language to integrate the remainder and the simple division product.

              However, if you want the remainder, only, use the mod() function of your language.

              X = 7/2 [X will be 3]
              X = 7%2 [X will be 0.5]

              If X is cast as an int in the second case, the result is 0.

              Modulo functionally is one of the most basic compiler optimizations across platforms. Some chips operate on bitwise transforms faster than remainder calculations, and a compiler makes that choice to speed up program execution.

              Comment

              • #8
                sealocan
                Calguns Addict
                • Mar 2012
                • 9950

                5/7 ?

                Comment

                • #9
                  Deedle
                  Senior Member
                  • Jan 2018
                  • 1146

                  Originally posted by bob7122
                  I have an exam this week and the math concept specifically ROUNDING is being brought up by my classmates and I need some clarification.

                  I am stating that if both operands are integers then my answer will be in integer form, BUT YOU ROUND DOWN ALWAYS.
                  Conversion of a floating point type to integer type will truncate, which means it will always 'round toward zero', so for negative numbers it actually rounds up if you see what I mean.

                  Not sure about C#, but I assume it's the same as they tended to keep things the same as a C guy would expect. C has a function called floor() that always rounds down, maybe C# has this as well.
                  "No personal computer will ever have gigabytes of RAM" - Scott Nudds

                  Comment

                  • #10
                    kemasa
                    I need a LIFE!!
                    • Jun 2005
                    • 10706

                    It isn't rounding, it is truncation. Integers have no fractions.
                    Kemasa.
                    False signature edited by Paul: Banned from the FFL forum due to being rude and insulting. Doing this continues his abuse.

                    Don't tell someone to read the rules he wrote or tell him that he is wrong.

                    Never try to teach a pig to sing. You waste your time and you annoy the pig. - Robert A. Heinlein

                    Comment

                    • #11
                      Fastattack
                      Senior Member
                      • Mar 2008
                      • 1655

                      HELP is your friend.
                      Learn about C# operators that perform multiplication, division, remainder, addition, and subtraction operations with numeric types.

                      Comment

                      • #12
                        bob7122
                        Calguns Addict
                        • Jul 2010
                        • 5090

                        Originally posted by Robotron2k84
                        Yes, division returns integers unless overloaded by casting the return value as a floating point or precision type. In this case it's up to the language to integrate the remainder and the simple division product.

                        However, if you want the remainder, only, use the mod() function of your language.

                        X = 7/2 [X will be 3]
                        X = 7%2 [X will be 0.5]

                        If X is cast as an int in the second case, the result is 0.

                        Modulo functionally is one of the most basic compiler optimizations across platforms. Some chips operate on bitwise transforms faster than remainder calculations, and a compiler makes that choice to speed up program execution.

                        https://en.wikipedia.org/wiki/Modulo_operation
                        Originally posted by sealocan
                        5/7 ?
                        Originally posted by Deedle
                        Conversion of a floating point type to integer type will truncate, which means it will always 'round toward zero', so for negative numbers it actually rounds up if you see what I mean.

                        Not sure about C#, but I assume it's the same as they tended to keep things the same as a C guy would expect. C has a function called floor() that always rounds down, maybe C# has this as well.
                        Originally posted by kemasa
                        It isn't rounding, it is truncation. Integers have no fractions.
                        Originally posted by Fastattack
                        Thank you all so much!
                        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

                        • #13
                          AN40
                          Junior Member
                          • Jun 2014
                          • 50

                          Damn man, when you read the text and actually do the examples in a compiler. It helps build logical intuition. Most important skill for a programmer is logic.

                          Also, learn how to google. All the resources are out there. A non-resourceful programmer is an unemployed programmer.

                          Comment

                          • #14
                            Robotron2k84
                            Senior Member
                            • Sep 2017
                            • 2013

                            One thing:

                            I realized I made a mistake in my explanation of the mod function. Mod returns whole numbers as the difference of the dividend and the product multipled by the denominator.

                            You actually have to perform mod / denominator as a float to get the decimal representation of the fractional product.

                            Updated example:

                            X = (7%2)/2.0 [X will be 0.5, dividing by 2.0 casts return as float]

                            Sorry for the confusion.
                            Last edited by Robotron2k84; 10-16-2018, 9:22 PM.

                            Comment

                            • #15
                              BigFatGuy
                              Veteran Member
                              • Oct 2010
                              • 3176

                              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.
                              NRA Patron Member

                              I've written up my ongoing adventures as I learn to hunt.

                              Yes, you CAN fit a case of shotgun shells into a .50cal ammo can.

                              I think i found an optimal solution for ammo can labeling.


                              I made this target for the NRA's Marksman pistol test. I think it's a lot better than the paper plate they suggest.

                              Comment

                              Working...
                              UA-8071174-1