Search Unity

Floating point division remainder function?

Discussion in 'Scripting' started by CoffeeConundrum, Mar 5, 2014.

  1. CoffeeConundrum

    CoffeeConundrum

    Joined:
    Dec 30, 2013
    Posts:
    46
    Hi there,

    Been trying to find a way of having floating point division remainders and haven't had any luck in doing so.

    All I require to do is change a variable every time 3 seconds go by. After some research online, I noticed for normal c# that the fmod function was used however I cannot seem to find this within Unity.

    Any help or suggestions would be welcomed.

    Thanks in advance!
     
  2. jgodfrey

    jgodfrey

    Joined:
    Nov 14, 2009
    Posts:
    564
    While I don't know what "floating point division remainders" and "change a variable every 3 seconds" have to do with each other...

    The standard modulus operator in C# can be applied to any numeric type, including floats. So...

    Code (csharp):
    1.  
    2. 9.1f % 3
    3.  
    Will yield 0.1, or something very close to 0.1 (as is the case with almost any floating point math).

    Jeff
     
    Last edited: Mar 5, 2014
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    %

    Use InvokeRepeating.

    Unity uses normal C#; there isn't any fmod function, but just use % like I mentioned.

    --Eric
     
  4. Paragon99

    Paragon99

    Joined:
    Nov 23, 2013
    Posts:
    54
    I doubt this will do what you want it to.
    I am assuming you are going to try to increment a counter everytime Time.time % 3==0? If this is so, then it will be quite unreliable as time.deltaTime is greater than float.minDelta... I.E Your just as likely for iteration x to find Time.time%3==2.98 and iteration x+1 to find Time.time%3==0.01 or something similiar..
     
  5. AlpacaMaster

    AlpacaMaster

    Joined:
    Jan 30, 2014
    Posts:
    32
    how do i do this in unityscript? the % doesn't work...
     
  6. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    Yes it does.
     
  7. landon912

    landon912

    Joined:
    Nov 8, 2011
    Posts:
    1,579
    Are you using it right? You might want to use %= if your trying to assign it.