Results 1 to 12 of 12
  1. #1
    MSgt. Shooter Person
    Join Date
    Jul 2010
    Location
    Italy
    Posts
    136

    Default Problem with Tick

    Hi guys, I have the follow code that extends Actor :

    Code:
    var float temp;
    
    simulated State MyState
    {
    	function BeginState(Name PreviousNameState)
    	{
    		`log(" -------------------------------------- MyState ");
    	}
              
    	functiom Tick(float DeltaTime)
    	{
    		Super.Tick(DeltaTime);
    
    		temp += 0.4;
    
    		`log(" ------------------------- TEMP : "@temp);
    	}
    }
    The result log :

    Code:
    [0060.34] ScriptLog:  -------------------------  TEMP :  47.6000
    [0060.36] ScriptLog:  -------------------------  TEMP :  48.0000
    [0060.37] ScriptLog:  -------------------------  TEMP :  48.4000
    [0060.39] ScriptLog:  -------------------------  TEMP :  48.8000
    [0060.41] ScriptLog:  -------------------------  TEMP :  49.2000
    [0060.42] ScriptLog:  -------------------------  TEMP :  49.6000
    [0060.44] ScriptLog:  -------------------------  TEMP :  50.0000
    [0060.45] ScriptLog:  -------------------------  TEMP :  50.4000
    [0060.47] ScriptLog:  -------------------------  TEMP :  50.8000
    [0060.49] ScriptLog:  -------------------------  TEMP :  51.2001
    [0060.50] ScriptLog:  -------------------------  TEMP :  51.6001
    [0060.52] ScriptLog:  -------------------------  TEMP :  52.0001
    [0060.53] ScriptLog:  -------------------------  TEMP :  52.4001
    [0060.55] ScriptLog:  -------------------------  TEMP :  52.8001
    [0060.57] ScriptLog:  -------------------------  TEMP :  53.2001
    Why ???

  2. #2
    Last edited by Mooser; 04-10-2012 at 05:27 PM.

  3. #3
    MSgt. Shooter Person
    Join Date
    Jul 2010
    Location
    Italy
    Posts
    136

    Default

    solutions to fix it quickly?

  4. #4

    Default

    There's almost certainly a better way to do this, but this worked for me:

    Code:
    temp = float(int(10 * (temp + 0.4))) / 10;
    Last edited by Angel_Mapper; 04-10-2012 at 07:20 PM.

  5. #5

    Default

    Maybe just change temp to an Int, and divide before the log? This way the gradual rounding errors won't accumulate and you'll always start from a cleanly represented int.

    This may have other floating point issues though.

    (Untested code warning)

    Code:
    var Int temp; 
    
    simulated State MyState
    {
    	function BeginState(Name PreviousNameState)
    	{
    		`log(" -------------------------------------- MyState ");
    	}
              
    	functiom Tick(float DeltaTime)
    	{
    		local float floatTemp;
    
    		Super.Tick(DeltaTime);
    
    		temp += 4;
    		floatTemp = temp / 10;
    
    		`log(" ------------------------- TEMP : "@floatTemp );
    	}
    }

  6. #6
    MSgt. Shooter Person
    Join Date
    Jul 2010
    Location
    Italy
    Posts
    136

    Default

    Thankyou very much guys !!!

  7. #7
    MSgt. Shooter Person
    Join Date
    Jul 2010
    Location
    Italy
    Posts
    136

    Default

    WHY ???

    Code:
    var float temp;
    
    simulated State MyState
    {
    	function BeginState(Name PreviousNameState)
    	{
    		`log(" -------------------------------------- MyState ");
    	}
              
    	functiom Tick(float DeltaTime)
    	{
    		Super.Tick(DeltaTime);
    
    		temp = float(int(10*(temp + 0.4))) / 10;;
    
    		`log(" ------------------------- TEMP : "@temp);
    	}
    }
    Result :

    Code:
    [0038.31]   ------------- TEMP :  7.6000  
    [0038.32]   ------------- TEMP :  8.0000 
    [0038.33]   ------------- TEMP :  8.4000 
    [0038.34]   ------------- TEMP :  8.7000 
    [0038.35]   ------------- TEMP :  9.0000 
    [0038.35]   ------------- TEMP :  9.4000 
    [0038.36]   ------------- TEMP :  9.7000 
    [0038.37]   ------------- TEMP :  10.0000 
    [0038.38]   ------------- TEMP :  10.4000

  8. #8

    Default

    Wow, very interesting.

    Maybe the compiler is doing something weird behind the scenes? Or the multiple conversions is abusive on code? Not sure. Did you try the Integer approach?

  9. #9
    Banned
    Join Date
    Feb 2011
    Location
    BXL/Paris
    Posts
    2,169

    Default

    Try this one: TestFloat = (int(TestFloat*10)+4)*0.1;

  10. #10
    MSgt. Shooter Person
    Join Date
    Jul 2010
    Location
    Italy
    Posts
    136

    Default

    Quote Originally Posted by VendorX View Post
    Try this one: TestFloat = (int(TestFloat*10)+4)*0.1;
    VendorX Thank you as always for your great help! Thanks also to others for answers.
    Last edited by Chihastonick; 04-16-2012 at 11:43 AM.

  11. #11

    Default

    Ah yeah going that direction would be better, what was happening with mine was it was getting say, 8.79999 and truncating instead of rounding.

  12. #12

    Default

    Quote Originally Posted by Mooser View Post
    Wow, very interesting.

    Maybe the compiler is doing something weird behind the scenes? Or the multiple conversions is abusive on code? Not sure. Did you try the Integer approach?
    Read up on how floating-point numbers work internally. You'll see the compiler is not responsible for the rounding errors at all.

    The real reason is, that 0.4 does not have a precise float value representation. That's because the format defines a number to be of the sum of 1 and a possibly empty combination of 1/2, 1/4, 1/8, ..., 1/4194304, 1/8388608. That sum is multiplied by 2 to the power of -126 to +127 and the result's sign can be plus or minus.
    Like most fractional numbers, 0.4 lies between two actual floating point values. These are:
    +(1 + 1/2 + 1/16 + 1/32 + 1/256 + 1/512 + 1/4096 + 1/8192 + 1/65536 + 1/131072 + 1/1048576 + 1/2097152) * 2^-2, ~= 0.399999976158
    and
    +(1 + 1/2 + 1/16 + 1/32 + 1/256 + 1/512 + 1/4096 + 1/8192 + 1/65536 + 1/131072 + 1/1048576 + 1/2097152 + 1/8388608) * 2^-2 ~= 0.40000000596
    The latter is closer to 0.4, so it's the value used when you specify the literal "0.4" in your code.

    Except for performing integer arithmetics as long as possible before applying a float operation, there's nothing you could do about this.

    Personally I'd recommend:
    Code:
    var int counter;
    
    function Tick(float DeltaTime)
    {
      counter++;
      `log(counter * 0.4);
    }
    But I actually see another question arising here: Why add 0.4 in Tick and not DeltaTime? Or, if adding a constant value, why in Tick and not in a timer event?
    Last edited by Wormbo; 04-17-2012 at 12:55 PM.
    Wormbo's UT/UT2004/UT3 mods | PlanetJailbreak | Unreal Wiki | Liandri Archives

    <elmuerte> you shouldn't do all-nighters, it's a waste of time and effort
    <TNSe> nono
    <TNSe> its always funny to find code a week later you dont even remember writing
    <Pfhoenix> what's worse is when you have a Star Wars moment
    <Pfhoenix> "Luke! I am your code!" "No! Impossible! It can't be!"
    Note that your questions via PMs will be ignored if they actually belong in the forum.


 

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Copyright ©2009-2011 Epic Games, Inc. All Rights Reserved.
Digital Point modules: Sphinx-based search vBulletin skin by CompletevB.com.