Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to compare floats in a Job?

Discussion in 'Scripting' started by OJDee, Jan 29, 2021.

  1. OJDee

    OJDee

    Joined:
    Feb 11, 2014
    Posts:
    64
    if x == y is not reliable enough due to floating point differences

    Mathf.Approximately(x,y) is not available inside Job

    How would you compare two float values to 3 decimal places for example?
     
  2. OJDee

    OJDee

    Joined:
    Feb 11, 2014
    Posts:
    64
    What about if y is smaller than x?
    No Math(Abs) in Job
     
  3. HellGate94

    HellGate94

    Joined:
    Sep 21, 2017
    Posts:
    132
    there is math.abs in the mathematics package
     
    OJDee and Bunny83 like this.
  4. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,528
    No, this makes no sense. Your condition would always be true. if x is 5 and y is 4 your first condition would be true even though the values are not the same or close to each other. Same when the values are the other way round. In that case the second condition would be true. You would need an
    &&
    instead of an
    ||
    .

    Like HellGate94 said inside jobs you should use the math class instead of the Mathf class.
     
    JeffDUnity3D and khanism like this.
  5. OJDee

    OJDee

    Joined:
    Feb 11, 2014
    Posts:
    64
    Doh! Didn't see that, just tried Mathf and got errors. Thanks all!