Announcement

Collapse
No announcement yet.

Float -> Int

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Float -> Int

    Ok im wondering if i do this, Int(MYFLOAT) does it Round the number to an int like Round() does or does it just remove the decimal? so if its 1.2 or 1.9 it returns 1?

    #2
    int() simply chops off the decimal part, so 1.9 would become 1.

    Here's an easy and documented way for you to check such things in the future; a commandlet.

    Code:
    //====================================================================
    // Generic Commandlet.
    //
    // Use as follows:
    //  - Open a DOS box.
    //  - Navigate to the UT2004\System folder.
    //  - Enter the following command:
    //
    //    UCC [package name].[commandlet name] [parameter]...
    //
    //    package name:
    //    The name of the package containing the commandlet.
    //
    //    commandlet name:
    //    The name of your commandlet class.  If the class name ends in
    //    "commandlet", you may leave that out.  The engine will add it.
    //
    //    parameter:
    //    Optional string parameter.
    //====================================================================
    
    class XS_Commandlet extends Commandlet;
    
    event int Main(string Parameter)
    {
        // Whitespace for readability;
        Log("");
    
        // Replace this line with whatever you wish to display in the DOS 
        // box.
        Log("Testing " $ Parameter);
    
        return 0;
    }
    
    /** Concatenates v to a string in (X,Y,Z) format. */
    static final operator(40) string $ (coerce string s, vector v)
    {
        local string NewString;
    
        NewString = s $ Round(v.X) $ "," $ Round(v.Y) $ "," $ Round(v.Z);
    
        return NewString;
    }
    
    /** Concatenates r to a string in (X,Y,Z) format. */
    static final operator(40) string $ (coerce string s, rotator r)
    {
        local string NewString;
    
        NewString = s $ r.Pitch $ "," $ r.Yaw $ "," $ r.Roll;
    
        return NewString;
    }
    I actually used this to confirm the result.

    Comment


      #3
      Thx a bunch but how do i use that c.l. to do anything? lol like tell me how u solve my question

      Comment


        #4
        Of course, if you want to be REALLY cheap, you could try
        int(MyFloat + 0.5), which gives you the equivalent of "rounding off"

        Comment


          #5
          well thats not wat i want, i want it so no matter what the Decimal is and the integer is 1 it will always return 1, 1.12058021 or 1.01 or 1.98 will come back 1, its for my award stats check:
          Code:
          function bool CheckAward(int Param1, int Param2)
          {
             if( Param1/Param2 == Int(Param1/Param2) )
                return true;
             else
                 return false;
          }

          Comment


            #6
            Put the commandlet in whatever package you like, replace the line marked with the "replace this line" comment with Log(int(1.9)); and compile it and follow the instructions in the comments at the top of the code. That's all there is to it.

            Comment


              #7
              oh lol "Biga: Biga your a dur. Biga: I KNO! THX FOR NOTICING"

              Comment


                #8
                I seem to be having a problem.
                Code:
                function bool CheckAward(int Param1, int Param2)
                {
                   local float f;
                   f = Param1/Param2;
                   if( f == Int(f) )
                      return true;
                   else
                       return false;
                }
                always returns true when Param1=1 and Param2=12?

                so wouldnt it be:
                Code:
                function bool CheckAward(int Param1, int Param2)
                {
                   local float f;
                   f = 1/12; // should be 0.0833 (repeated)
                   if( f == Int(f) ) // if( 0.0833 == 0 ) should be false right?
                      return true;
                   else
                       return false;
                }
                EDIT:

                i ran a test with logs:
                ScriptLog: Param1=1 Param2=12
                ScriptLog: f=0.00
                ScriptLog: if( 0.00 == 0 )

                it seems i need the float to be longer then 2 decimal places, more then .00 i need more like .000000

                Comment


                  #9
                  OK i fixed it, i timed Param1 and Param2 by 10 and it worked, im going to items by 100 just to be sure i passes over a int that will dived to 0

                  Comment


                    #10
                    I think the engine makes an integer division because you're dividing two integers, and only then decides to make it a float because you're storing the result in a float. You could probably get the engine to make a float right away like this: float(Param1) / Param2. Then it would no longer be an integer division.

                    Comment


                      #11
                      im sry lol i didnt understand that:noob:

                      Comment


                        #12
                        I'm not 100% sure, but I think this is how it works:

                        You try to divide two whole numbers, and the engine gives you a whole number as the result. You try to divide a whole number and a decimal number or two decimal numbrs, the engine gives you a decimal number as the result.

                        You want to divide two whole numbers, but still get a decimal number as the result. What should you do? Turn one of the whole numbers into a decimal number. You do that in the same way you turned the float into the int earlier on, only the other way around: float(Param1). Now it is treated as a decimal number, and when you divide it by Param2 you will get a decimal number as the result.

                        Comment


                          #13
                          INT/INT = INT

                          ALWAYS.

                          1/12 = 0.whatever. Everything after the . gets CHOPPED OFF because its INT/INT. Then the result gets casted to a float, 0.000000.

                          What you want is float(INT)/INT = float or float(INT)/float(INT) = float. The first way should be faster, and no loss of accuracy.

                          Comment


                            #14
                            what i found was, if you divide two ints, it becomes the max of 0.00 but if you divide floats its 0.000000 so i timed by 100 because the 0.000000 still exists but only giving 0.00 so i timed it by 100 to MAKE it show

                            Comment

                            Working...
                            X