Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Math bug, Ceil?

Discussion in 'Editor & General Support' started by Foxxis, Nov 26, 2007.

  1. Foxxis

    Foxxis

    Joined:
    Jun 27, 2006
    Posts:
    1,108
    Sorry if I'm simply ignorant, but Mathf.Ceil is described in the docs as:
    "Returns the smallest integer greater to or equal to f."
    Yet, Mathf.Ceil(2.0/2.0) will return 2. How come?
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,773
    Multiplying and dividing floats always has a good chance of producing a tiny error. In your case it's (presumably) getting a value of something like 1.00000001, which is being rounded up. So (assuming that's what's happening) Ceil is behaving properly, the error is with the division.

    The float error is something that's common across every programming language I've used. I'm not sure if there are any more-accurate data types out there. but at any rate you need to expect and compensate for errors like these.
     
  3. Foxxis

    Foxxis

    Joined:
    Jun 27, 2006
    Posts:
    1,108
    I'm aware that you often get tiny math errors (I've C++ background), but I was unsure whether Unity compensated for it or not in functions such as Ceil. The docs could be misleading, depending on how you decide to look on it. I mean, with the inevitable errors, you're very unlikely to get a float equal to an integer. ;-)

    Anyway, I'm working around it as usual, but found it curious. Thanks for the answer!
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,773
    According to the docs, it does seem to compensate for it in certain spots like Vector3's != operator (but curiously, it doesn't say so in ==). As a rule, I'd say it's safe to always expect the errors.
     
  5. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    2.0/2.0 should yield 1.0 exactly in any IEEE float representation. What does Ceil(1.0) give?